{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE GADTs                 #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ParallelListComp      #-}
{-# LANGUAGE TypeSynonymInstances  #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# OPTIONS_HADDOCK hide #-}
-- |
-- Module      : LLVM.AST.Type.Global
-- 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.Global
  where

import LLVM.AST.Type.Downcast
import LLVM.AST.Type.Function
import LLVM.AST.Type.Name

import qualified LLVM.AST.Global                                    as LLVM
import qualified LLVM.AST.Name                                      as LLVM
import qualified LLVM.AST.Type                                      as LLVM


-- | A global function definition.
--
type GlobalFunction args t = Function Label args t

instance Downcast (GlobalFunction args t) LLVM.Global where
  downcast :: GlobalFunction args t -> Global
downcast GlobalFunction args t
f = Global
LLVM.functionDefaults { name :: Name
LLVM.name       = Name
nm
                                     , returnType :: Type
LLVM.returnType = Type
res
                                     , parameters :: ([Parameter], Bool)
LLVM.parameters = ([Parameter]
params, Bool
False)
                                     }
    where
      trav :: GlobalFunction args t -> ([LLVM.Type], LLVM.Type, LLVM.Name)
      trav :: GlobalFunction args t -> ([Type], Type, Name)
trav (Body Type t
t Maybe TailCall
_ Label
n) = ([], Type t -> Type
forall typed untyped.
(Downcast typed untyped, HasCallStack) =>
typed -> untyped
downcast Type t
t, Label -> Name
forall typed untyped.
(Downcast typed untyped, HasCallStack) =>
typed -> untyped
downcast Label
n)
      trav (Lam PrimType a
a Operand a
_ Function Label args t
l)  = let ([Type]
as, Type
r, Name
n) = Function Label args t -> ([Type], Type, Name)
forall (args :: [*]) t.
GlobalFunction args t -> ([Type], Type, Name)
trav Function Label args t
l
                          in  (PrimType a -> Type
forall typed untyped.
(Downcast typed untyped, HasCallStack) =>
typed -> untyped
downcast PrimType a
a Type -> [Type] -> [Type]
forall a. a -> [a] -> [a]
: [Type]
as, Type
r, Name
n)
      --
      ([Type]
args, Type
res, Name
nm)  = GlobalFunction args t -> ([Type], Type, Name)
forall (args :: [*]) t.
GlobalFunction args t -> ([Type], Type, Name)
trav GlobalFunction args t
f
      params :: [Parameter]
params           = [ Type -> Name -> [ParameterAttribute] -> Parameter
LLVM.Parameter Type
t (Word -> Name
LLVM.UnName Word
i) [] | Type
t <- [Type]
args | Word
i <- [Word
0..] ]