{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE GADTs                 #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# OPTIONS_HADDOCK hide #-}
-- |
-- Module      : LLVM.AST.Type.Operand
-- Copyright   : [2015..2020] The Accelerate Team
-- License     : BSD3
--
-- Maintainer  : Trevor L. McDonell <trevor.mcdonell@gmail.com>
-- Stability   : experimental
-- Portability : non-portable (GHC extensions)
--

module LLVM.AST.Type.Operand (

  Operand(..),

) where

import LLVM.AST.Type.Constant
import LLVM.AST.Type.Downcast
import LLVM.AST.Type.Name
import LLVM.AST.Type.Representation

import qualified LLVM.AST.Operand                                   as LLVM


-- | An 'Operand' is roughly anything that is an argument to an 'Instruction'
--
data Operand a where
  LocalReference        :: Type a -> Name a -> Operand a
  ConstantOperand       :: Constant a -> Operand a


-- | Convert to llvm-hs
--
instance Downcast (Operand a) LLVM.Operand where
  downcast :: Operand a -> Operand
downcast (LocalReference Type a
t Name a
n) = Type -> Name -> Operand
LLVM.LocalReference (Type a -> Type
forall typed untyped.
(Downcast typed untyped, HasCallStack) =>
typed -> untyped
downcast Type a
t) (Name a -> Name
forall typed untyped.
(Downcast typed untyped, HasCallStack) =>
typed -> untyped
downcast Name a
n)
  downcast (ConstantOperand Constant a
c)  = Constant -> Operand
LLVM.ConstantOperand (Constant a -> Constant
forall typed untyped.
(Downcast typed untyped, HasCallStack) =>
typed -> untyped
downcast Constant a
c)

instance TypeOf Operand where
  typeOf :: Operand a -> Type a
typeOf (LocalReference Type a
t Name a
_) = Type a
t
  typeOf (ConstantOperand Constant a
c)  = Constant a -> Type a
forall (f :: * -> *) a. TypeOf f => f a -> Type a
typeOf Constant a
c