{-|
  Copyright   :  (C) 2012-2016, University of Twente,
                     2016     , Myrtle Software Ltd,
                     2017     , Google Inc.
  License     :  BSD2 (see the file LICENSE)
  Maintainer  :  Christiaan Baaij <christiaan.baaij@gmail.com>

  Term Literal
-}

{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}

module Clash.Core.Literal
  ( Literal (..)
  , literalType
  )
where

import Control.DeepSeq                        (NFData (..))
import Data.Binary                            (Binary)
import Data.Hashable                          (Hashable)
import Data.Primitive.ByteArray               (ByteArray)
import Data.Primitive.ByteArray.Extra         ()
import GHC.Generics                           (Generic)

import {-# SOURCE #-} Clash.Core.Type         (Type)
import Clash.Core.TysPrim                     (intPrimTy, integerPrimTy,
                                               charPrimTy, stringPrimTy,
                                               wordPrimTy,
                                               int64PrimTy, word64PrimTy,
                                               floatPrimTy, doublePrimTy,
                                               naturalPrimTy, byteArrayPrimTy)

-- | Term Literal
data Literal
  = IntegerLiteral  !Integer
  | IntLiteral      !Integer
  | WordLiteral     !Integer
  | Int64Literal    !Integer
  | Word64Literal   !Integer
  | StringLiteral   !String
  | FloatLiteral    !Rational
  | DoubleLiteral   !Rational
  | CharLiteral     !Char
  | NaturalLiteral  !Integer
  | ByteArrayLiteral !ByteArray
  deriving (Literal -> Literal -> Bool
(Literal -> Literal -> Bool)
-> (Literal -> Literal -> Bool) -> Eq Literal
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Literal -> Literal -> Bool
$c/= :: Literal -> Literal -> Bool
== :: Literal -> Literal -> Bool
$c== :: Literal -> Literal -> Bool
Eq,Eq Literal
Eq Literal
-> (Literal -> Literal -> Ordering)
-> (Literal -> Literal -> Bool)
-> (Literal -> Literal -> Bool)
-> (Literal -> Literal -> Bool)
-> (Literal -> Literal -> Bool)
-> (Literal -> Literal -> Literal)
-> (Literal -> Literal -> Literal)
-> Ord Literal
Literal -> Literal -> Bool
Literal -> Literal -> Ordering
Literal -> Literal -> Literal
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Literal -> Literal -> Literal
$cmin :: Literal -> Literal -> Literal
max :: Literal -> Literal -> Literal
$cmax :: Literal -> Literal -> Literal
>= :: Literal -> Literal -> Bool
$c>= :: Literal -> Literal -> Bool
> :: Literal -> Literal -> Bool
$c> :: Literal -> Literal -> Bool
<= :: Literal -> Literal -> Bool
$c<= :: Literal -> Literal -> Bool
< :: Literal -> Literal -> Bool
$c< :: Literal -> Literal -> Bool
compare :: Literal -> Literal -> Ordering
$ccompare :: Literal -> Literal -> Ordering
$cp1Ord :: Eq Literal
Ord,Int -> Literal -> ShowS
[Literal] -> ShowS
Literal -> String
(Int -> Literal -> ShowS)
-> (Literal -> String) -> ([Literal] -> ShowS) -> Show Literal
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Literal] -> ShowS
$cshowList :: [Literal] -> ShowS
show :: Literal -> String
$cshow :: Literal -> String
showsPrec :: Int -> Literal -> ShowS
$cshowsPrec :: Int -> Literal -> ShowS
Show,(forall x. Literal -> Rep Literal x)
-> (forall x. Rep Literal x -> Literal) -> Generic Literal
forall x. Rep Literal x -> Literal
forall x. Literal -> Rep Literal x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Literal x -> Literal
$cfrom :: forall x. Literal -> Rep Literal x
Generic,Literal -> ()
(Literal -> ()) -> NFData Literal
forall a. (a -> ()) -> NFData a
rnf :: Literal -> ()
$crnf :: Literal -> ()
NFData,Eq Literal
Eq Literal
-> (Int -> Literal -> Int) -> (Literal -> Int) -> Hashable Literal
Int -> Literal -> Int
Literal -> Int
forall a. Eq a -> (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: Literal -> Int
$chash :: Literal -> Int
hashWithSalt :: Int -> Literal -> Int
$chashWithSalt :: Int -> Literal -> Int
$cp1Hashable :: Eq Literal
Hashable,Get Literal
[Literal] -> Put
Literal -> Put
(Literal -> Put)
-> Get Literal -> ([Literal] -> Put) -> Binary Literal
forall t. (t -> Put) -> Get t -> ([t] -> Put) -> Binary t
putList :: [Literal] -> Put
$cputList :: [Literal] -> Put
get :: Get Literal
$cget :: Get Literal
put :: Literal -> Put
$cput :: Literal -> Put
Binary)

-- | Determines the Type of a Literal
literalType :: Literal
            -> Type
literalType :: Literal -> Type
literalType (IntegerLiteral  Integer
_) = Type
integerPrimTy
literalType (IntLiteral      Integer
_) = Type
intPrimTy
literalType (WordLiteral     Integer
_) = Type
wordPrimTy
literalType (StringLiteral   String
_) = Type
stringPrimTy
literalType (FloatLiteral    Rational
_) = Type
floatPrimTy
literalType (DoubleLiteral   Rational
_) = Type
doublePrimTy
literalType (CharLiteral     Char
_) = Type
charPrimTy
literalType (Int64Literal    Integer
_) = Type
int64PrimTy
literalType (Word64Literal   Integer
_) = Type
word64PrimTy
literalType (NaturalLiteral  Integer
_) = Type
naturalPrimTy
literalType (ByteArrayLiteral ByteArray
_) = Type
byteArrayPrimTy