{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RankNTypes #-}
module Clash.Core.Var
( Attr' (..)
, Var (..)
, IdScope (..)
, Id
, TyVar
, mkId
, mkLocalId
, mkGlobalId
, mkTyVar
, setVarUnique
, setVarType
, setIdScope
, modifyVarName
, isGlobalId
, isLocalId
, attrName
)
where
import Control.DeepSeq (NFData (..))
import Data.Binary (Binary)
import Data.Function (on)
import Data.Hashable (Hashable)
import GHC.Generics (Generic)
import Clash.Core.Name (Name (..))
import {-# SOURCE #-} Clash.Core.Term (Term, TmName)
import {-# SOURCE #-} Clash.Core.Type (Kind, Type, TyName)
import Clash.Unique
data Attr'
= BoolAttr' String Bool
| IntegerAttr' String Integer
| StringAttr' String String
| Attr' String
deriving (Attr' -> Attr' -> Bool
(Attr' -> Attr' -> Bool) -> (Attr' -> Attr' -> Bool) -> Eq Attr'
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Attr' -> Attr' -> Bool
$c/= :: Attr' -> Attr' -> Bool
== :: Attr' -> Attr' -> Bool
$c== :: Attr' -> Attr' -> Bool
Eq, Int -> Attr' -> ShowS
[Attr'] -> ShowS
Attr' -> String
(Int -> Attr' -> ShowS)
-> (Attr' -> String) -> ([Attr'] -> ShowS) -> Show Attr'
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Attr'] -> ShowS
$cshowList :: [Attr'] -> ShowS
show :: Attr' -> String
$cshow :: Attr' -> String
showsPrec :: Int -> Attr' -> ShowS
$cshowsPrec :: Int -> Attr' -> ShowS
Show, Attr' -> ()
(Attr' -> ()) -> NFData Attr'
forall a. (a -> ()) -> NFData a
rnf :: Attr' -> ()
$crnf :: Attr' -> ()
NFData, (forall x. Attr' -> Rep Attr' x)
-> (forall x. Rep Attr' x -> Attr') -> Generic Attr'
forall x. Rep Attr' x -> Attr'
forall x. Attr' -> Rep Attr' x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Attr' x -> Attr'
$cfrom :: forall x. Attr' -> Rep Attr' x
Generic, Int -> Attr' -> Int
Attr' -> Int
(Int -> Attr' -> Int) -> (Attr' -> Int) -> Hashable Attr'
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: Attr' -> Int
$chash :: Attr' -> Int
hashWithSalt :: Int -> Attr' -> Int
$chashWithSalt :: Int -> Attr' -> Int
Hashable, Eq Attr'
Eq Attr' =>
(Attr' -> Attr' -> Ordering)
-> (Attr' -> Attr' -> Bool)
-> (Attr' -> Attr' -> Bool)
-> (Attr' -> Attr' -> Bool)
-> (Attr' -> Attr' -> Bool)
-> (Attr' -> Attr' -> Attr')
-> (Attr' -> Attr' -> Attr')
-> Ord Attr'
Attr' -> Attr' -> Bool
Attr' -> Attr' -> Ordering
Attr' -> Attr' -> Attr'
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 :: Attr' -> Attr' -> Attr'
$cmin :: Attr' -> Attr' -> Attr'
max :: Attr' -> Attr' -> Attr'
$cmax :: Attr' -> Attr' -> Attr'
>= :: Attr' -> Attr' -> Bool
$c>= :: Attr' -> Attr' -> Bool
> :: Attr' -> Attr' -> Bool
$c> :: Attr' -> Attr' -> Bool
<= :: Attr' -> Attr' -> Bool
$c<= :: Attr' -> Attr' -> Bool
< :: Attr' -> Attr' -> Bool
$c< :: Attr' -> Attr' -> Bool
compare :: Attr' -> Attr' -> Ordering
$ccompare :: Attr' -> Attr' -> Ordering
$cp1Ord :: Eq Attr'
Ord, Get Attr'
[Attr'] -> Put
Attr' -> Put
(Attr' -> Put) -> Get Attr' -> ([Attr'] -> Put) -> Binary Attr'
forall t. (t -> Put) -> Get t -> ([t] -> Put) -> Binary t
putList :: [Attr'] -> Put
$cputList :: [Attr'] -> Put
get :: Get Attr'
$cget :: Get Attr'
put :: Attr' -> Put
$cput :: Attr' -> Put
Binary)
attrName :: Attr' -> String
attrName :: Attr' -> String
attrName (BoolAttr' n :: String
n _) = String
n
attrName (IntegerAttr' n :: String
n _) = String
n
attrName (StringAttr' n :: String
n _) = String
n
attrName (Attr' n :: String
n) = String
n
data Var a
= TyVar
{ Var a -> Name a
varName :: !(Name a)
, Var a -> Int
varUniq :: {-# UNPACK #-} !Unique
, Var a -> Kind
varType :: Kind
}
| Id
{ varName :: !(Name a)
, varUniq :: {-# UNPACK #-} !Unique
, varType :: Type
, Var a -> IdScope
idScope :: IdScope
}
deriving (Int -> Var a -> ShowS
[Var a] -> ShowS
Var a -> String
(Int -> Var a -> ShowS)
-> (Var a -> String) -> ([Var a] -> ShowS) -> Show (Var a)
forall a. Int -> Var a -> ShowS
forall a. [Var a] -> ShowS
forall a. Var a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Var a] -> ShowS
$cshowList :: forall a. [Var a] -> ShowS
show :: Var a -> String
$cshow :: forall a. Var a -> String
showsPrec :: Int -> Var a -> ShowS
$cshowsPrec :: forall a. Int -> Var a -> ShowS
Show,(forall x. Var a -> Rep (Var a) x)
-> (forall x. Rep (Var a) x -> Var a) -> Generic (Var a)
forall x. Rep (Var a) x -> Var a
forall x. Var a -> Rep (Var a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (Var a) x -> Var a
forall a x. Var a -> Rep (Var a) x
$cto :: forall a x. Rep (Var a) x -> Var a
$cfrom :: forall a x. Var a -> Rep (Var a) x
Generic,Var a -> ()
(Var a -> ()) -> NFData (Var a)
forall a. Var a -> ()
forall a. (a -> ()) -> NFData a
rnf :: Var a -> ()
$crnf :: forall a. Var a -> ()
NFData,Int -> Var a -> Int
Var a -> Int
(Int -> Var a -> Int) -> (Var a -> Int) -> Hashable (Var a)
forall a. Int -> Var a -> Int
forall a. Var a -> Int
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: Var a -> Int
$chash :: forall a. Var a -> Int
hashWithSalt :: Int -> Var a -> Int
$chashWithSalt :: forall a. Int -> Var a -> Int
Hashable,Get (Var a)
[Var a] -> Put
Var a -> Put
(Var a -> Put) -> Get (Var a) -> ([Var a] -> Put) -> Binary (Var a)
forall a. Get (Var a)
forall a. [Var a] -> Put
forall a. Var a -> Put
forall t. (t -> Put) -> Get t -> ([t] -> Put) -> Binary t
putList :: [Var a] -> Put
$cputList :: forall a. [Var a] -> Put
get :: Get (Var a)
$cget :: forall a. Get (Var a)
put :: Var a -> Put
$cput :: forall a. Var a -> Put
Binary)
varKey :: Var a -> (Unique, Maybe IdScope)
varKey :: Var a -> (Int, Maybe IdScope)
varKey TyVar{Int
varUniq :: Int
varUniq :: forall a. Var a -> Int
varUniq} = (Int
varUniq, Maybe IdScope
forall a. Maybe a
Nothing)
varKey Id{Int
varUniq :: Int
varUniq :: forall a. Var a -> Int
varUniq,IdScope
idScope :: IdScope
idScope :: forall a. Var a -> IdScope
idScope} = (Int
varUniq, IdScope -> Maybe IdScope
forall a. a -> Maybe a
Just IdScope
idScope)
instance Eq (Var a) where
== :: Var a -> Var a -> Bool
(==) = (Int, Maybe IdScope) -> (Int, Maybe IdScope) -> Bool
forall a. Eq a => a -> a -> Bool
(==) ((Int, Maybe IdScope) -> (Int, Maybe IdScope) -> Bool)
-> (Var a -> (Int, Maybe IdScope)) -> Var a -> Var a -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` Var a -> (Int, Maybe IdScope)
forall a. Var a -> (Int, Maybe IdScope)
varKey
/= :: Var a -> Var a -> Bool
(/=) = (Int, Maybe IdScope) -> (Int, Maybe IdScope) -> Bool
forall a. Eq a => a -> a -> Bool
(/=) ((Int, Maybe IdScope) -> (Int, Maybe IdScope) -> Bool)
-> (Var a -> (Int, Maybe IdScope)) -> Var a -> Var a -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` Var a -> (Int, Maybe IdScope)
forall a. Var a -> (Int, Maybe IdScope)
varKey
instance Ord (Var a) where
compare :: Var a -> Var a -> Ordering
compare = (Int, Maybe IdScope) -> (Int, Maybe IdScope) -> Ordering
forall a. Ord a => a -> a -> Ordering
compare ((Int, Maybe IdScope) -> (Int, Maybe IdScope) -> Ordering)
-> (Var a -> (Int, Maybe IdScope)) -> Var a -> Var a -> Ordering
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` Var a -> (Int, Maybe IdScope)
forall a. Var a -> (Int, Maybe IdScope)
varKey
instance Uniquable (Var a) where
getUnique :: Var a -> Int
getUnique = Var a -> Int
forall a. Var a -> Int
varUniq
setUnique :: Var a -> Int -> Var a
setUnique var :: Var a
var u :: Int
u = Var a
var {varUniq :: Int
varUniq=Int
u, varName :: Name a
varName=(Var a -> Name a
forall a. Var a -> Name a
varName Var a
var){nameUniq :: Int
nameUniq=Int
u}}
data IdScope = GlobalId | LocalId
deriving (Int -> IdScope -> ShowS
[IdScope] -> ShowS
IdScope -> String
(Int -> IdScope -> ShowS)
-> (IdScope -> String) -> ([IdScope] -> ShowS) -> Show IdScope
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [IdScope] -> ShowS
$cshowList :: [IdScope] -> ShowS
show :: IdScope -> String
$cshow :: IdScope -> String
showsPrec :: Int -> IdScope -> ShowS
$cshowsPrec :: Int -> IdScope -> ShowS
Show,(forall x. IdScope -> Rep IdScope x)
-> (forall x. Rep IdScope x -> IdScope) -> Generic IdScope
forall x. Rep IdScope x -> IdScope
forall x. IdScope -> Rep IdScope x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep IdScope x -> IdScope
$cfrom :: forall x. IdScope -> Rep IdScope x
Generic,IdScope -> ()
(IdScope -> ()) -> NFData IdScope
forall a. (a -> ()) -> NFData a
rnf :: IdScope -> ()
$crnf :: IdScope -> ()
NFData,Int -> IdScope -> Int
IdScope -> Int
(Int -> IdScope -> Int) -> (IdScope -> Int) -> Hashable IdScope
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: IdScope -> Int
$chash :: IdScope -> Int
hashWithSalt :: Int -> IdScope -> Int
$chashWithSalt :: Int -> IdScope -> Int
Hashable,Get IdScope
[IdScope] -> Put
IdScope -> Put
(IdScope -> Put)
-> Get IdScope -> ([IdScope] -> Put) -> Binary IdScope
forall t. (t -> Put) -> Get t -> ([t] -> Put) -> Binary t
putList :: [IdScope] -> Put
$cputList :: [IdScope] -> Put
get :: Get IdScope
$cget :: Get IdScope
put :: IdScope -> Put
$cput :: IdScope -> Put
Binary,IdScope -> IdScope -> Bool
(IdScope -> IdScope -> Bool)
-> (IdScope -> IdScope -> Bool) -> Eq IdScope
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: IdScope -> IdScope -> Bool
$c/= :: IdScope -> IdScope -> Bool
== :: IdScope -> IdScope -> Bool
$c== :: IdScope -> IdScope -> Bool
Eq,Eq IdScope
Eq IdScope =>
(IdScope -> IdScope -> Ordering)
-> (IdScope -> IdScope -> Bool)
-> (IdScope -> IdScope -> Bool)
-> (IdScope -> IdScope -> Bool)
-> (IdScope -> IdScope -> Bool)
-> (IdScope -> IdScope -> IdScope)
-> (IdScope -> IdScope -> IdScope)
-> Ord IdScope
IdScope -> IdScope -> Bool
IdScope -> IdScope -> Ordering
IdScope -> IdScope -> IdScope
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 :: IdScope -> IdScope -> IdScope
$cmin :: IdScope -> IdScope -> IdScope
max :: IdScope -> IdScope -> IdScope
$cmax :: IdScope -> IdScope -> IdScope
>= :: IdScope -> IdScope -> Bool
$c>= :: IdScope -> IdScope -> Bool
> :: IdScope -> IdScope -> Bool
$c> :: IdScope -> IdScope -> Bool
<= :: IdScope -> IdScope -> Bool
$c<= :: IdScope -> IdScope -> Bool
< :: IdScope -> IdScope -> Bool
$c< :: IdScope -> IdScope -> Bool
compare :: IdScope -> IdScope -> Ordering
$ccompare :: IdScope -> IdScope -> Ordering
$cp1Ord :: Eq IdScope
Ord)
type Id = Var Term
type TyVar = Var Type
modifyVarName ::
(Name a -> Name a)
-> Var a
-> Var a
modifyVarName :: (Name a -> Name a) -> Var a -> Var a
modifyVarName f :: Name a -> Name a
f (TyVar n :: Name a
n _ k :: Kind
k) =
let n' :: Name a
n' = Name a -> Name a
f Name a
n
in Name a -> Int -> Kind -> Var a
forall a. Name a -> Int -> Kind -> Var a
TyVar Name a
n' (Name a -> Int
forall a. Name a -> Int
nameUniq Name a
n') Kind
k
modifyVarName f :: Name a -> Name a
f (Id n :: Name a
n _ t :: Kind
t s :: IdScope
s) =
let n' :: Name a
n' = Name a -> Name a
f Name a
n
in Name a -> Int -> Kind -> IdScope -> Var a
forall a. Name a -> Int -> Kind -> IdScope -> Var a
Id Name a
n' (Name a -> Int
forall a. Name a -> Int
nameUniq Name a
n') Kind
t IdScope
s
mkTyVar
:: Kind
-> TyName
-> TyVar
mkTyVar :: Kind -> TyName -> TyVar
mkTyVar tyKind :: Kind
tyKind tyName :: TyName
tyName = TyName -> Int -> Kind -> TyVar
forall a. Name a -> Int -> Kind -> Var a
TyVar TyName
tyName (TyName -> Int
forall a. Name a -> Int
nameUniq TyName
tyName) Kind
tyKind
mkId
:: Type
-> IdScope
-> TmName
-> Id
mkId :: Kind -> IdScope -> TmName -> Id
mkId tmType :: Kind
tmType scope :: IdScope
scope tmName :: TmName
tmName = TmName -> Int -> Kind -> IdScope -> Id
forall a. Name a -> Int -> Kind -> IdScope -> Var a
Id TmName
tmName (TmName -> Int
forall a. Name a -> Int
nameUniq TmName
tmName) Kind
tmType IdScope
scope
mkLocalId
:: Type
-> TmName
-> Id
mkLocalId :: Kind -> TmName -> Id
mkLocalId tmType :: Kind
tmType tmName :: TmName
tmName = TmName -> Int -> Kind -> IdScope -> Id
forall a. Name a -> Int -> Kind -> IdScope -> Var a
Id TmName
tmName (TmName -> Int
forall a. Name a -> Int
nameUniq TmName
tmName) Kind
tmType IdScope
LocalId
mkGlobalId
:: Type
-> TmName
-> Id
mkGlobalId :: Kind -> TmName -> Id
mkGlobalId tmType :: Kind
tmType tmName :: TmName
tmName = TmName -> Int -> Kind -> IdScope -> Id
forall a. Name a -> Int -> Kind -> IdScope -> Var a
Id TmName
tmName (TmName -> Int
forall a. Name a -> Int
nameUniq TmName
tmName) Kind
tmType IdScope
GlobalId
setVarUnique
:: Var a
-> Unique
-> Var a
setVarUnique :: Var a -> Int -> Var a
setVarUnique v :: Var a
v u :: Int
u = Var a
v { varUniq :: Int
varUniq = Int
u, varName :: Name a
varName = (Var a -> Name a
forall a. Var a -> Name a
varName Var a
v) {nameUniq :: Int
nameUniq = Int
u} }
setVarType
:: Var a
-> Type
-> Var a
setVarType :: Var a -> Kind -> Var a
setVarType v :: Var a
v t :: Kind
t = Var a
v { varType :: Kind
varType = Kind
t }
isGlobalId
:: Var a
-> Bool
isGlobalId :: Var a -> Bool
isGlobalId (Id {idScope :: forall a. Var a -> IdScope
idScope = IdScope
GlobalId}) = Bool
True
isGlobalId _ = Bool
False
isLocalId
:: Var a
-> Bool
isLocalId :: Var a -> Bool
isLocalId (Id {idScope :: forall a. Var a -> IdScope
idScope = IdScope
LocalId}) = Bool
True
isLocalId _ = Bool
False
setIdScope
:: IdScope
-> Var a
-> Var a
setIdScope :: IdScope -> Var a -> Var a
setIdScope s :: IdScope
s (Id nm :: Name a
nm u :: Int
u t :: Kind
t _) = Name a -> Int -> Kind -> IdScope -> Var a
forall a. Name a -> Int -> Kind -> IdScope -> Var a
Id Name a
nm Int
u Kind
t IdScope
s
setIdScope _ v :: Var a
v = Var a
v