{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE InstanceSigs #-}

module Codegen.ExprGen.Types where

import qualified Ast.Types as AT
import qualified LLVM.AST.Type as T

-- | Type conversion to LLVM IR.
class ToLLVM a where
  toLLVM :: a -> T.Type

instance ToLLVM AT.Type where
  toLLVM :: AT.Type -> T.Type
  toLLVM :: Type -> Type
toLLVM Type
expr = case Type
expr of
    AT.TInt Int
width -> Word32 -> Type
T.IntegerType (Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
width)
    Type
AT.TFloat -> FloatingPointType -> Type
T.FloatingPointType FloatingPointType
T.FloatFP
    Type
AT.TDouble -> FloatingPointType -> Type
T.FloatingPointType FloatingPointType
T.DoubleFP
    Type
AT.TChar -> Word32 -> Type
T.IntegerType Word32
8
    Type
AT.TBoolean -> Word32 -> Type
T.IntegerType Word32
1
    Type
AT.TVoid -> Type
T.void
    AT.TPointer Type
t -> Type -> Type
T.ptr (Type -> Type
forall a. ToLLVM a => a -> Type
toLLVM Type
t)
    AT.TArray Type
t (Just Int
n) -> Word64 -> Type -> Type
T.ArrayType (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n) (Type -> Type
forall a. ToLLVM a => a -> Type
toLLVM Type
t)
    AT.TArray Type
t Maybe Int
Nothing -> Type -> Type
T.ptr (Type -> Type
forall a. ToLLVM a => a -> Type
toLLVM Type
t)
    AT.TFunction Type
ret [Type]
params Bool
var -> Type -> [Type] -> Bool -> Type
T.FunctionType (Type -> Type
forall a. ToLLVM a => a -> Type
toLLVM Type
ret) ((Type -> Type) -> [Type] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map Type -> Type
forall a. ToLLVM a => a -> Type
toLLVM [Type]
params) Bool
var
    AT.TStruct String
_ [(String, Type)]
fields -> Bool -> [Type] -> Type
T.StructureType Bool
False (((String, Type) -> Type) -> [(String, Type)] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map (Type -> Type
forall a. ToLLVM a => a -> Type
toLLVM (Type -> Type)
-> ((String, Type) -> Type) -> (String, Type) -> Type
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String, Type) -> Type
forall a b. (a, b) -> b
snd) [(String, Type)]
fields)
    AT.TUnion String
_ [(String, Type)]
variants -> Bool -> [Type] -> Type
T.StructureType Bool
False (((String, Type) -> Type) -> [(String, Type)] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map (Type -> Type
forall a. ToLLVM a => a -> Type
toLLVM (Type -> Type)
-> ((String, Type) -> Type) -> (String, Type) -> Type
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String, Type) -> Type
forall a b. (a, b) -> b
snd) [(String, Type)]
variants)
    AT.TTypedef String
_ Type
t -> Type -> Type
forall a. ToLLVM a => a -> Type
toLLVM Type
t
    AT.TMutable Type
t -> Type -> Type
forall a. ToLLVM a => a -> Type
toLLVM Type
t
    Type
AT.TUnknown -> Type
T.void