module Language.C.Analysis.TypeUtils (
integral,
floating,
simplePtr,
size_tType,
ptrDiffType,
boolType,
voidType,
voidPtr,
constVoidPtr,
charPtr,
constCharPtr,
stringType,
valistType,
isIntegralType,
isFloatingType,
isPointerType,
isScalarType,
isFunctionType,
typeQuals,
typeQualsUpd,
typeAttrs,
typeAttrsUpd,
baseType,
derefTypeDef,
deepDerefTypeDef,
canonicalType,
getIntType,
getFloatType
) where
import Language.C.Analysis.SemRep
import Language.C.Syntax.Constants
instance Eq TypeQuals where
(==) (TypeQuals c1 v1 r1) (TypeQuals c2 v2 r2) =
c1 == c2 && v1 == v2 && r1 == r2
instance Ord TypeQuals where
(<=) (TypeQuals c1 v1 r1) (TypeQuals c2 v2 r2) =
c1 <= c2 && v1 <= v2 && r1 <= r2
integral :: IntType -> Type
integral ty = DirectType (TyIntegral ty) noTypeQuals noAttributes
floating :: FloatType -> Type
floating ty = DirectType (TyFloating ty) noTypeQuals noAttributes
simplePtr :: Type -> Type
simplePtr t = PtrType t noTypeQuals []
constPtr :: Type -> Type
constPtr t = PtrType t (TypeQuals True False False) []
size_tType :: Type
size_tType = integral TyInt
ptrDiffType :: Type
ptrDiffType = integral TyInt
boolType :: Type
boolType = integral TyInt
voidType :: Type
voidType = DirectType TyVoid noTypeQuals noAttributes
voidPtr :: Type
voidPtr = simplePtr voidType
constVoidPtr :: Type
constVoidPtr = constPtr voidType
charPtr :: Type
charPtr = simplePtr (integral TyChar)
constCharPtr :: Type
constCharPtr = constPtr (integral TyChar)
stringType :: Type
stringType = ArrayType
(DirectType (TyIntegral TyChar) (TypeQuals True False False) noAttributes)
(UnknownArraySize False)
noTypeQuals
[]
valistType :: Type
valistType = DirectType (TyBuiltin TyVaList) noTypeQuals noAttributes
isIntegralType :: Type -> Bool
isIntegralType (DirectType (TyIntegral _) _ _) = True
isIntegralType (DirectType (TyEnum _) _ _) = True
isIntegralType _ = False
isFloatingType :: Type -> Bool
isFloatingType (DirectType (TyFloating _) _ _) = True
isFloatingType _ = False
isPointerType :: Type -> Bool
isPointerType (PtrType _ _ _) = True
isPointerType (ArrayType _ _ _ _) = True
isPointerType _ = False
isScalarType :: Type -> Bool
isScalarType t = isIntegralType t || isPointerType t || isFloatingType t
isFunctionType :: Type -> Bool
isFunctionType ty =
case ty of TypeDefType (TypeDefRef _ (Just actual_ty) _) _ _ -> isFunctionType actual_ty
TypeDefType _ _ _ -> error "isFunctionType: unresolved typeDef"
FunctionType _ _ -> True
_ -> False
typeQuals :: Type -> TypeQuals
typeQuals (DirectType _ q _) = q
typeQuals (PtrType _ q _) = q
typeQuals (ArrayType _ _ q _) = q
typeQuals (FunctionType _ _) = noTypeQuals
typeQuals (TypeDefType (TypeDefRef _ Nothing _) q _) = q
typeQuals (TypeDefType (TypeDefRef _ (Just t) _) q _) = mergeTypeQuals q (typeQuals t)
typeQualsUpd :: (TypeQuals -> TypeQuals) -> Type -> Type
typeQualsUpd f ty =
case ty of DirectType ty_name ty_quals ty_attrs -> DirectType ty_name (f ty_quals) ty_attrs
PtrType ty_inner ty_quals ty_attrs -> PtrType ty_inner (f ty_quals) ty_attrs
ArrayType ty_inner sz ty_quals ty_attrs -> ArrayType ty_inner sz (f ty_quals) ty_attrs
FunctionType ty_inner ty_attrs -> FunctionType ty_inner ty_attrs
TypeDefType ty_ref ty_quals ty_attrs -> TypeDefType ty_ref (f ty_quals) ty_attrs
typeAttrs :: Type -> Attributes
typeAttrs (DirectType _ _ a) = a
typeAttrs (PtrType _ _ a) = a
typeAttrs (ArrayType _ _ _ a) = a
typeAttrs (FunctionType _ a) = a
typeAttrs (TypeDefType (TypeDefRef _ Nothing _) _ a) = a
typeAttrs (TypeDefType (TypeDefRef _ (Just t) _) _ a) = mergeAttributes a (typeAttrs t)
typeAttrsUpd :: (Attributes -> Attributes) -> Type -> Type
typeAttrsUpd f ty =
case ty of DirectType ty_name ty_quals ty_attrs -> DirectType ty_name ty_quals (f ty_attrs)
PtrType ty_inner ty_quals ty_attrs -> PtrType ty_inner ty_quals (f ty_attrs)
ArrayType ty_inner sz ty_quals ty_attrs -> ArrayType ty_inner sz ty_quals (f ty_attrs)
FunctionType ty_inner ty_attrs -> FunctionType ty_inner (f ty_attrs)
TypeDefType ty_ref ty_quals ty_attrs -> TypeDefType ty_ref ty_quals (f ty_attrs)
baseType :: Type -> Type
baseType (PtrType t _ _) = t
baseType (ArrayType t _ _ _) = t
baseType _ = error "base of non-pointer type"
derefTypeDef :: Type -> Type
derefTypeDef (TypeDefType (TypeDefRef _ (Just t) _) q a) =
(typeAttrsUpd (mergeAttributes a) . typeQualsUpd (mergeTypeQuals q))
(derefTypeDef t)
derefTypeDef ty = ty
deepDerefTypeDef :: Type -> Type
deepDerefTypeDef (PtrType t quals attrs) =
PtrType (deepDerefTypeDef t) quals attrs
deepDerefTypeDef (ArrayType t size quals attrs) =
ArrayType (deepDerefTypeDef t) size quals attrs
deepDerefTypeDef (FunctionType (FunType rt params varargs) attrs) =
FunctionType (FunType (deepDerefTypeDef rt) params varargs) attrs
deepDerefTypeDef (FunctionType (FunTypeIncomplete rt) attrs) =
FunctionType (FunTypeIncomplete (deepDerefTypeDef rt)) attrs
deepDerefTypeDef (TypeDefType (TypeDefRef _ (Just t) _) q a) =
(typeAttrsUpd (mergeAttributes a) . typeQualsUpd (mergeTypeQuals q))
(deepDerefTypeDef t)
deepDerefTypeDef t = t
canonicalType :: Type -> Type
canonicalType t =
case deepDerefTypeDef t of
FunctionType ft attrs -> simplePtr (FunctionType ft attrs)
t' -> t'
testFlags :: Enum f => [f] -> Flags f -> Bool
testFlags flags fi = and $ map ((flip testFlag) fi) flags
getIntType :: Flags CIntFlag -> IntType
getIntType flags | testFlags [FlagLongLong, FlagUnsigned] flags = TyULLong
| testFlag FlagLongLong flags = TyLLong
| testFlags [FlagLong, FlagUnsigned] flags = TyULong
| testFlag FlagLong flags = TyLong
| testFlag FlagUnsigned flags = TyUInt
| otherwise = TyInt
getFloatType :: String -> FloatType
getFloatType fs | last fs `elem` ['f', 'F'] = TyFloat
| last fs `elem` ['l', 'L'] = TyLDouble
| otherwise = TyDouble