| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 | module Gidl.Types  ( module Gidl.Types.AST  , module Gidl.Types.Base  , lookupTypeName  , insertType  , typeLeaves  , childTypes  , sizeOf  , basePrimType  , typeName  ) whereimport Data.Tuple (swap)import Data.List (nub)import Gidl.Types.ASTimport Gidl.Types.BaselookupTypeName :: TypeName -> TypeEnv -> Maybe TypelookupTypeName tn te =  case aux te of    Just a -> Just a    Nothing -> case aux baseTypeEnv of      Just a -> Just a      Nothing -> Nothing  where  aux (TypeEnv e) = lookup tn etypeName :: Type -> TypeNametypeName (StructType n _) = ntypeName (PrimType (EnumType n _ _)) = ntypeName (PrimType (Newtype n _)) = ntypeName (PrimType VoidType) = error "XXX"typeName t@(PrimType (AtomType _)) =  let TypeEnv bte = baseTypeEnv in  case lookup t (map swap bte) of    Just n -> n    Nothing -> error "impossible: cannot find name for AtomType in baseTypeEnv"insertType :: TypeName -> Type -> TypeEnv -> TypeEnvinsertType tn t e@(TypeEnv te) = case lookupTypeName tn e of  Nothing -> TypeEnv ((tn,t):te)  Just _ -> error ("insertType invariant broken: type " ++ tn ++ " already exists")typeLeaves :: Type -> [Type]typeLeaves (StructType _ s) = nub (map snd s)typeLeaves (PrimType (Newtype _ tn)) = [PrimType tn]typeLeaves _ = []childTypes :: Type -> [Type]childTypes t = [t] ++ concat (map childTypes (typeLeaves t))sizeOf :: Type -> IntegersizeOf (StructType _ s) = sum [ sizeOf tr | (_, tr) <- s ]sizeOf (PrimType (Newtype _ tr)) = sizeOf (PrimType tr)sizeOf (PrimType (EnumType _ bs _)) = bitsSize bssizeOf (PrimType (AtomType (AtomInt bs))) = bitsSize bssizeOf (PrimType (AtomType (AtomWord bs))) = bitsSize bssizeOf (PrimType (AtomType AtomFloat)) = 4sizeOf (PrimType (AtomType AtomDouble)) = 8sizeOf (PrimType VoidType) = 0bitsSize :: Bits -> IntegerbitsSize Bits8  = 1bitsSize Bits16 = 2bitsSize Bits32 = 4bitsSize Bits64 = 8-- Reduce a newtype to the innermost concrete typebasePrimType :: PrimType -> PrimTypebasePrimType (Newtype _ t) = basePrimType tbasePrimType a = a
 |