{-# LANGUAGE ConstraintKinds      #-}
{-# LANGUAGE FlexibleContexts     #-}
{-# LANGUAGE FlexibleInstances    #-}
{-# LANGUAGE TypeFamilies         #-}
{-# LANGUAGE UndecidableInstances #-} -- Wrinkle in Note [Trees That Grow]
                                      -- in module Language.Haskell.Syntax.Extension
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE TypeApplications #-}

{-# OPTIONS_GHC -Wno-orphans #-} -- Outputable, OutputableBndrId

{-
(c) The University of Glasgow 2006
(c) The GRASP/AQUA Project, Glasgow University, 1992-1998

-}

-- | Source-language literals
module GHC.Hs.Lit
  ( module Language.Haskell.Syntax.Lit
  , module GHC.Hs.Lit
  ) where

import GHC.Prelude

import {-# SOURCE #-} GHC.Hs.Expr( pprExpr )

import GHC.Types.Basic (PprPrec(..), topPrec )
import GHC.Core.Ppr ( {- instance OutputableBndr TyVar -} )
import GHC.Types.SourceText
import GHC.Core.Type
import GHC.Utils.Outputable
import GHC.Hs.Extension
import Language.Haskell.Syntax.Expr ( HsExpr )
import Language.Haskell.Syntax.Extension
import Language.Haskell.Syntax.Lit

{-
************************************************************************
*                                                                      *
\subsection[HsLit]{Literals}
*                                                                      *
************************************************************************
-}

type instance XHsChar       (GhcPass _) = SourceText
type instance XHsCharPrim   (GhcPass _) = SourceText
type instance XHsString     (GhcPass _) = SourceText
type instance XHsStringPrim (GhcPass _) = SourceText
type instance XHsInt        (GhcPass _) = NoExtField
type instance XHsIntPrim    (GhcPass _) = SourceText
type instance XHsWordPrim   (GhcPass _) = SourceText
type instance XHsInt64Prim  (GhcPass _) = SourceText
type instance XHsWord64Prim (GhcPass _) = SourceText
type instance XHsInteger    (GhcPass _) = SourceText
type instance XHsRat        (GhcPass _) = NoExtField
type instance XHsFloatPrim  (GhcPass _) = NoExtField
type instance XHsDoublePrim (GhcPass _) = NoExtField
type instance XXLit         (GhcPass _) = DataConCantHappen

data OverLitRn
  = OverLitRn {
        OverLitRn -> Bool
ol_rebindable :: Bool,         -- Note [ol_rebindable]
        OverLitRn -> LIdP GhcRn
ol_from_fun   :: LIdP GhcRn    -- Note [Overloaded literal witnesses]
        }

data OverLitTc
  = OverLitTc {
        OverLitTc -> Bool
ol_rebindable :: Bool,         -- Note [ol_rebindable]
        OverLitTc -> HsExpr GhcTc
ol_witness    :: HsExpr GhcTc, -- Note [Overloaded literal witnesses]
        OverLitTc -> Type
ol_type :: Type }

{-
Note [Overloaded literal witnesses]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

During renaming, the coercion function needed for a given HsOverLit is
resolved according to the current scope and RebindableSyntax (see Note
[ol_rebindable]). The result of this resolution *before* type checking
is the coercion function such as 'fromInteger' or 'fromRational',
stored in the ol_from_fun field of OverLitRn.

*After* type checking, the ol_witness field of the OverLitTc contains
the witness of the literal as HsExpr, such as (fromInteger 3) or
lit_78. This witness should replace the literal. Reason: it allows
commoning up of the fromInteger calls, which wouldn't be possible if
the desugarer made the application.

The ol_type in OverLitTc records the type the overloaded literal is
found to have.
-}

type instance XOverLit GhcPs = NoExtField
type instance XOverLit GhcRn = OverLitRn
type instance XOverLit GhcTc = OverLitTc

pprXOverLit :: GhcPass p -> XOverLit (GhcPass p) -> SDoc
pprXOverLit :: forall (p :: Pass). GhcPass p -> XOverLit (GhcPass p) -> SDoc
pprXOverLit GhcPass p
GhcPs XOverLit (GhcPass p)
noExt = forall a. Outputable a => a -> SDoc
ppr XOverLit (GhcPass p)
noExt
pprXOverLit GhcPass p
GhcRn OverLitRn{ $sel:ol_from_fun:OverLitRn :: OverLitRn -> LIdP GhcRn
ol_from_fun = LIdP GhcRn
from_fun } = forall a. Outputable a => a -> SDoc
ppr LIdP GhcRn
from_fun
pprXOverLit GhcPass p
GhcTc OverLitTc{ $sel:ol_witness:OverLitTc :: OverLitTc -> HsExpr GhcTc
ol_witness = HsExpr GhcTc
witness } = forall (p :: Pass).
OutputableBndrId p =>
HsExpr (GhcPass p) -> SDoc
pprExpr HsExpr GhcTc
witness

type instance XXOverLit (GhcPass _) = DataConCantHappen

overLitType :: HsOverLit GhcTc -> Type
overLitType :: HsOverLit GhcTc -> Type
overLitType (OverLit OverLitTc{ $sel:ol_type:OverLitTc :: OverLitTc -> Type
ol_type = Type
ty } OverLitVal
_) = Type
ty

-- | @'hsOverLitNeedsParens' p ol@ returns 'True' if an overloaded literal
-- @ol@ needs to be parenthesized under precedence @p@.
hsOverLitNeedsParens :: PprPrec -> HsOverLit x -> Bool
hsOverLitNeedsParens :: forall x. PprPrec -> HsOverLit x -> Bool
hsOverLitNeedsParens PprPrec
p (OverLit { ol_val :: forall p. HsOverLit p -> OverLitVal
ol_val = OverLitVal
olv }) = OverLitVal -> Bool
go OverLitVal
olv
  where
    go :: OverLitVal -> Bool
    go :: OverLitVal -> Bool
go (HsIntegral IntegralLit
x)   = PprPrec
p forall a. Ord a => a -> a -> Bool
> PprPrec
topPrec Bool -> Bool -> Bool
&& IntegralLit -> Bool
il_neg IntegralLit
x
    go (HsFractional FractionalLit
x) = PprPrec
p forall a. Ord a => a -> a -> Bool
> PprPrec
topPrec Bool -> Bool -> Bool
&& FractionalLit -> Bool
fl_neg FractionalLit
x
    go (HsIsString {})  = Bool
False
hsOverLitNeedsParens PprPrec
_ (XOverLit { }) = Bool
False

-- | @'hsLitNeedsParens' p l@ returns 'True' if a literal @l@ needs
-- to be parenthesized under precedence @p@.
hsLitNeedsParens :: PprPrec -> HsLit x -> Bool
hsLitNeedsParens :: forall x. PprPrec -> HsLit x -> Bool
hsLitNeedsParens PprPrec
p = HsLit x -> Bool
go
  where
    go :: HsLit x -> Bool
go (HsChar {})        = Bool
False
    go (HsCharPrim {})    = Bool
False
    go (HsString {})      = Bool
False
    go (HsStringPrim {})  = Bool
False
    go (HsInt XHsInt x
_ IntegralLit
x)        = PprPrec
p forall a. Ord a => a -> a -> Bool
> PprPrec
topPrec Bool -> Bool -> Bool
&& IntegralLit -> Bool
il_neg IntegralLit
x
    go (HsIntPrim XHsIntPrim x
_ Integer
x)    = PprPrec
p forall a. Ord a => a -> a -> Bool
> PprPrec
topPrec Bool -> Bool -> Bool
&& Integer
x forall a. Ord a => a -> a -> Bool
< Integer
0
    go (HsWordPrim {})    = Bool
False
    go (HsInt64Prim XHsInt64Prim x
_ Integer
x)  = PprPrec
p forall a. Ord a => a -> a -> Bool
> PprPrec
topPrec Bool -> Bool -> Bool
&& Integer
x forall a. Ord a => a -> a -> Bool
< Integer
0
    go (HsWord64Prim {})  = Bool
False
    go (HsInteger XHsInteger x
_ Integer
x Type
_)  = PprPrec
p forall a. Ord a => a -> a -> Bool
> PprPrec
topPrec Bool -> Bool -> Bool
&& Integer
x forall a. Ord a => a -> a -> Bool
< Integer
0
    go (HsRat XHsRat x
_ FractionalLit
x Type
_)      = PprPrec
p forall a. Ord a => a -> a -> Bool
> PprPrec
topPrec Bool -> Bool -> Bool
&& FractionalLit -> Bool
fl_neg FractionalLit
x
    go (HsFloatPrim XHsFloatPrim x
_ FractionalLit
x)  = PprPrec
p forall a. Ord a => a -> a -> Bool
> PprPrec
topPrec Bool -> Bool -> Bool
&& FractionalLit -> Bool
fl_neg FractionalLit
x
    go (HsDoublePrim XHsDoublePrim x
_ FractionalLit
x) = PprPrec
p forall a. Ord a => a -> a -> Bool
> PprPrec
topPrec Bool -> Bool -> Bool
&& FractionalLit -> Bool
fl_neg FractionalLit
x
    go (XLit XXLit x
_)           = Bool
False

-- | Convert a literal from one index type to another
convertLit :: HsLit (GhcPass p1) -> HsLit (GhcPass p2)
convertLit :: forall (p1 :: Pass) (p2 :: Pass).
HsLit (GhcPass p1) -> HsLit (GhcPass p2)
convertLit (HsChar XHsChar (GhcPass p1)
a Char
x)       = forall x. XHsChar x -> Char -> HsLit x
HsChar XHsChar (GhcPass p1)
a Char
x
convertLit (HsCharPrim XHsCharPrim (GhcPass p1)
a Char
x)   = forall x. XHsCharPrim x -> Char -> HsLit x
HsCharPrim XHsCharPrim (GhcPass p1)
a Char
x
convertLit (HsString XHsString (GhcPass p1)
a FastString
x)     = forall x. XHsString x -> FastString -> HsLit x
HsString XHsString (GhcPass p1)
a FastString
x
convertLit (HsStringPrim XHsStringPrim (GhcPass p1)
a ByteString
x) = forall x. XHsStringPrim x -> ByteString -> HsLit x
HsStringPrim XHsStringPrim (GhcPass p1)
a ByteString
x
convertLit (HsInt XHsInt (GhcPass p1)
a IntegralLit
x)        = forall x. XHsInt x -> IntegralLit -> HsLit x
HsInt XHsInt (GhcPass p1)
a IntegralLit
x
convertLit (HsIntPrim XHsIntPrim (GhcPass p1)
a Integer
x)    = forall x. XHsIntPrim x -> Integer -> HsLit x
HsIntPrim XHsIntPrim (GhcPass p1)
a Integer
x
convertLit (HsWordPrim XHsWordPrim (GhcPass p1)
a Integer
x)   = forall x. XHsWordPrim x -> Integer -> HsLit x
HsWordPrim XHsWordPrim (GhcPass p1)
a Integer
x
convertLit (HsInt64Prim XHsInt64Prim (GhcPass p1)
a Integer
x)  = forall x. XHsInt64Prim x -> Integer -> HsLit x
HsInt64Prim XHsInt64Prim (GhcPass p1)
a Integer
x
convertLit (HsWord64Prim XHsWord64Prim (GhcPass p1)
a Integer
x) = forall x. XHsWord64Prim x -> Integer -> HsLit x
HsWord64Prim XHsWord64Prim (GhcPass p1)
a Integer
x
convertLit (HsInteger XHsInteger (GhcPass p1)
a Integer
x Type
b)  = forall x. XHsInteger x -> Integer -> Type -> HsLit x
HsInteger XHsInteger (GhcPass p1)
a Integer
x Type
b
convertLit (HsRat XHsRat (GhcPass p1)
a FractionalLit
x Type
b)      = forall x. XHsRat x -> FractionalLit -> Type -> HsLit x
HsRat XHsRat (GhcPass p1)
a FractionalLit
x Type
b
convertLit (HsFloatPrim XHsFloatPrim (GhcPass p1)
a FractionalLit
x)  = forall x. XHsFloatPrim x -> FractionalLit -> HsLit x
HsFloatPrim XHsFloatPrim (GhcPass p1)
a FractionalLit
x
convertLit (HsDoublePrim XHsDoublePrim (GhcPass p1)
a FractionalLit
x) = forall x. XHsDoublePrim x -> FractionalLit -> HsLit x
HsDoublePrim XHsDoublePrim (GhcPass p1)
a FractionalLit
x

{-
Note [ol_rebindable]
~~~~~~~~~~~~~~~~~~~~
The ol_rebindable field is True if this literal is actually
using rebindable syntax.  Specifically:

  False iff ol_from_fun / ol_witness is the standard one
  True  iff ol_from_fun / ol_witness is non-standard

Equivalently it's True if
  a) RebindableSyntax is on
  b) the witness for fromInteger/fromRational/fromString
     that happens to be in scope isn't the standard one
-}

-- Instance specific to GhcPs, need the SourceText
instance Outputable (HsLit (GhcPass p)) where
    ppr :: HsLit (GhcPass p) -> SDoc
ppr (HsChar XHsChar (GhcPass p)
st Char
c)       = SourceText -> SDoc -> SDoc
pprWithSourceText XHsChar (GhcPass p)
st (Char -> SDoc
pprHsChar Char
c)
    ppr (HsCharPrim XHsCharPrim (GhcPass p)
st Char
c)   = SourceText -> SDoc -> SDoc -> SDoc
pp_st_suffix XHsCharPrim (GhcPass p)
st SDoc
primCharSuffix (Char -> SDoc
pprPrimChar Char
c)
    ppr (HsString XHsString (GhcPass p)
st FastString
s)     = SourceText -> SDoc -> SDoc
pprWithSourceText XHsString (GhcPass p)
st (FastString -> SDoc
pprHsString FastString
s)
    ppr (HsStringPrim XHsStringPrim (GhcPass p)
st ByteString
s) = SourceText -> SDoc -> SDoc
pprWithSourceText XHsStringPrim (GhcPass p)
st (ByteString -> SDoc
pprHsBytes ByteString
s)
    ppr (HsInt XHsInt (GhcPass p)
_ IntegralLit
i)
      = SourceText -> SDoc -> SDoc
pprWithSourceText (IntegralLit -> SourceText
il_text IntegralLit
i) (forall doc. IsLine doc => Integer -> doc
integer (IntegralLit -> Integer
il_value IntegralLit
i))
    ppr (HsInteger XHsInteger (GhcPass p)
st Integer
i Type
_)  = SourceText -> SDoc -> SDoc
pprWithSourceText XHsInteger (GhcPass p)
st (forall doc. IsLine doc => Integer -> doc
integer Integer
i)
    ppr (HsRat XHsRat (GhcPass p)
_ FractionalLit
f Type
_)       = forall a. Outputable a => a -> SDoc
ppr FractionalLit
f
    ppr (HsFloatPrim XHsFloatPrim (GhcPass p)
_ FractionalLit
f)   = forall a. Outputable a => a -> SDoc
ppr FractionalLit
f forall doc. IsLine doc => doc -> doc -> doc
<> SDoc
primFloatSuffix
    ppr (HsDoublePrim XHsDoublePrim (GhcPass p)
_ FractionalLit
d)  = forall a. Outputable a => a -> SDoc
ppr FractionalLit
d forall doc. IsLine doc => doc -> doc -> doc
<> SDoc
primDoubleSuffix
    ppr (HsIntPrim XHsIntPrim (GhcPass p)
st Integer
i)    = SourceText -> SDoc -> SDoc
pprWithSourceText XHsIntPrim (GhcPass p)
st (Integer -> SDoc
pprPrimInt Integer
i)
    ppr (HsWordPrim XHsWordPrim (GhcPass p)
st Integer
w)   = SourceText -> SDoc -> SDoc
pprWithSourceText XHsWordPrim (GhcPass p)
st (Integer -> SDoc
pprPrimWord Integer
w)
    ppr (HsInt64Prim XHsInt64Prim (GhcPass p)
st Integer
i)  = SourceText -> SDoc -> SDoc -> SDoc
pp_st_suffix XHsInt64Prim (GhcPass p)
st SDoc
primInt64Suffix  (Integer -> SDoc
pprPrimInt64 Integer
i)
    ppr (HsWord64Prim XHsWord64Prim (GhcPass p)
st Integer
w) = SourceText -> SDoc -> SDoc -> SDoc
pp_st_suffix XHsWord64Prim (GhcPass p)
st SDoc
primWord64Suffix (Integer -> SDoc
pprPrimWord64 Integer
w)

pp_st_suffix :: SourceText -> SDoc -> SDoc -> SDoc
pp_st_suffix :: SourceText -> SDoc -> SDoc -> SDoc
pp_st_suffix SourceText
NoSourceText         SDoc
_ SDoc
doc = SDoc
doc
pp_st_suffix (SourceText String
st) SDoc
suffix SDoc
_   = forall doc. IsLine doc => String -> doc
text String
st forall doc. IsLine doc => doc -> doc -> doc
<> SDoc
suffix

-- in debug mode, print the expression that it's resolved to, too
instance OutputableBndrId p
       => Outputable (HsOverLit (GhcPass p)) where
  ppr :: HsOverLit (GhcPass p) -> SDoc
ppr (OverLit {ol_val :: forall p. HsOverLit p -> OverLitVal
ol_val=OverLitVal
val, ol_ext :: forall p. HsOverLit p -> XOverLit p
ol_ext=XOverLit (GhcPass p)
ext})
        = forall a. Outputable a => a -> SDoc
ppr OverLitVal
val forall doc. IsLine doc => doc -> doc -> doc
<+> (forall doc. IsOutput doc => doc -> doc
whenPprDebug (forall doc. IsLine doc => doc -> doc
parens (forall (p :: Pass). GhcPass p -> XOverLit (GhcPass p) -> SDoc
pprXOverLit (forall (p :: Pass). IsPass p => GhcPass p
ghcPass @p) XOverLit (GhcPass p)
ext)))

instance Outputable OverLitVal where
  ppr :: OverLitVal -> SDoc
ppr (HsIntegral IntegralLit
i)     = SourceText -> SDoc -> SDoc
pprWithSourceText (IntegralLit -> SourceText
il_text IntegralLit
i) (forall doc. IsLine doc => Integer -> doc
integer (IntegralLit -> Integer
il_value IntegralLit
i))
  ppr (HsFractional FractionalLit
f)   = forall a. Outputable a => a -> SDoc
ppr FractionalLit
f
  ppr (HsIsString SourceText
st FastString
s)  = SourceText -> SDoc -> SDoc
pprWithSourceText SourceText
st (FastString -> SDoc
pprHsString FastString
s)

-- | pmPprHsLit pretty prints literals and is used when pretty printing pattern
-- match warnings. All are printed the same (i.e., without hashes if they are
-- primitive and not wrapped in constructors if they are boxed). This happens
-- mainly for too reasons:
--  * We do not want to expose their internal representation
--  * The warnings become too messy
pmPprHsLit :: HsLit (GhcPass x) -> SDoc
pmPprHsLit :: forall (p :: Pass). HsLit (GhcPass p) -> SDoc
pmPprHsLit (HsChar XHsChar (GhcPass x)
_ Char
c)       = Char -> SDoc
pprHsChar Char
c
pmPprHsLit (HsCharPrim XHsCharPrim (GhcPass x)
_ Char
c)   = Char -> SDoc
pprHsChar Char
c
pmPprHsLit (HsString XHsString (GhcPass x)
st FastString
s)    = SourceText -> SDoc -> SDoc
pprWithSourceText XHsString (GhcPass x)
st (FastString -> SDoc
pprHsString FastString
s)
pmPprHsLit (HsStringPrim XHsStringPrim (GhcPass x)
_ ByteString
s) = ByteString -> SDoc
pprHsBytes ByteString
s
pmPprHsLit (HsInt XHsInt (GhcPass x)
_ IntegralLit
i)        = forall doc. IsLine doc => Integer -> doc
integer (IntegralLit -> Integer
il_value IntegralLit
i)
pmPprHsLit (HsIntPrim XHsIntPrim (GhcPass x)
_ Integer
i)    = forall doc. IsLine doc => Integer -> doc
integer Integer
i
pmPprHsLit (HsWordPrim XHsWordPrim (GhcPass x)
_ Integer
w)   = forall doc. IsLine doc => Integer -> doc
integer Integer
w
pmPprHsLit (HsInt64Prim XHsInt64Prim (GhcPass x)
_ Integer
i)  = forall doc. IsLine doc => Integer -> doc
integer Integer
i
pmPprHsLit (HsWord64Prim XHsWord64Prim (GhcPass x)
_ Integer
w) = forall doc. IsLine doc => Integer -> doc
integer Integer
w
pmPprHsLit (HsInteger XHsInteger (GhcPass x)
_ Integer
i Type
_)  = forall doc. IsLine doc => Integer -> doc
integer Integer
i
pmPprHsLit (HsRat XHsRat (GhcPass x)
_ FractionalLit
f Type
_)      = forall a. Outputable a => a -> SDoc
ppr FractionalLit
f
pmPprHsLit (HsFloatPrim XHsFloatPrim (GhcPass x)
_ FractionalLit
f)  = forall a. Outputable a => a -> SDoc
ppr FractionalLit
f
pmPprHsLit (HsDoublePrim XHsDoublePrim (GhcPass x)
_ FractionalLit
d) = forall a. Outputable a => a -> SDoc
ppr FractionalLit
d