language-c-0.10.1: Analysis and generation of C code

Copyright | (c) 2008 Benedikt Huber
License | BSD-style
Maintainer | benedikt.huber@gmail.com
Stability | alpha
Portability | ghc
Safe Haskell | None
Language | Haskell2010

Language.C.Analysis.DeclAnalysis

Contents

- Translating types
- Dissecting type specs
- Helpers

Description

This module performs the analysis of declarations and the translation of type specifications in the AST.

Synopsis

- analyseTypeDecl :: MonadTrav m => CDecl -> m Type
- tType :: MonadTrav m => Bool -> NodeInfo -> [CTypeQual] -> TypeSpecAnalysis -> [CDerivedDeclr] -> [CDecl] -> m Type
- tDirectType :: MonadTrav m => Bool -> NodeInfo -> [CTypeQual] -> TypeSpecAnalysis -> m Type
- tNumType :: MonadCError m => NumTypeSpec -> m (Either (FloatType, Bool) IntType)
- tArraySize :: MonadTrav m => CArrSize -> m ArraySize
- tTypeQuals :: MonadTrav m => [CTypeQual] -> m (TypeQuals, Attributes)
- mergeOldStyle :: MonadCError m => NodeInfo -> [CDecl] -> [CDerivedDeclr] -> m [CDerivedDeclr]
- canonicalTypeSpec :: MonadTrav m => [CTypeSpec] -> m TypeSpecAnalysis
- data NumBaseType
  - = NoBaseType
  - | BaseChar
  - | BaseInt
  - | BaseInt128
  - | BaseFloat
  - | BaseBFloat16
  - | BaseFloatN Int Bool
  - | BaseDouble
- data SignSpec
  - = NoSignSpec
  - | Signed
  - | Unsigned
- data SizeMod
  - = NoSizeMod
  - | ShortMod
  - | LongMod
  - | LongLongMod
- data NumTypeSpec = NumTypeSpec {
  - base :: NumBaseType
  - signSpec :: SignSpec
  - sizeMod :: SizeMod
  - isComplex :: Bool
  }
- data TypeSpecAnalysis
  - = TSNone
  - | TSVoid
  - | TSBool
  - | TSNum NumTypeSpec
  - | TSTypeDef TypeDefRef
  - | TSType Type
  - | TSNonBasic CTypeSpec
- canonicalStorageSpec :: MonadCError m => [CStorageSpec] -> m StorageSpec
- data StorageSpec
  - = NoStorageSpec
  - | AutoSpec
  - | RegSpec
  - | ThreadSpec
  - | StaticSpec Bool
  - | ExternSpec Bool
  - | ClKernelSpec
  - | ClGlobalSpec
  - | ClLocalSpec
- hasThreadLocalSpec :: StorageSpec -> Bool
- hasClKernelSpec :: StorageSpec -> Bool
- isTypeDef :: [CDeclSpec] -> Bool
- data VarDeclInfo = VarDeclInfo VarName FunctionAttrs StorageSpec Attributes Type NodeInfo
- tAttr :: (MonadCError m, MonadSymtab m) => CAttr -> m Attr
- mkVarName :: (MonadCError m, MonadSymtab m) => NodeInfo -> Maybe Ident -> Maybe AsmName -> m VarName
- getOnlyDeclr :: MonadCError m => CDecl -> m CDeclr
- nameOfDecl :: MonadCError m => CDecl -> m Ident
- analyseVarDecl :: MonadTrav m => Bool -> [CStorageSpec] -> [CAttr] -> [CTypeQual] -> TypeSpecAnalysis -> [CFunSpec] -> CDeclr -> [CDecl] -> Maybe CInit -> m VarDeclInfo
- analyseVarDecl' :: MonadTrav m => Bool -> [CDeclSpec] -> CDeclr -> [CDecl] -> Maybe CInit -> m VarDeclInfo

# Translating types

analyseTypeDecl :: MonadTrav m => CDecl -> m Type Source #

get the type of a type declaration

A type declaration `T` may appear in thre forms:

-     typeof(T)

- as abstract declarator in a function prototype, as in `f(int)`
- in a declaration without declarators, as in `struct x { int a } ;`

Currently, `analyseTypeDecl` is exlusively used for analysing types for GNU's `typeof(T)`.

We move attributes to the type, as they have no meaning for the abstract declarator

tType :: MonadTrav m => Bool -> NodeInfo -> [CTypeQual] -> TypeSpecAnalysis -> [CDerivedDeclr] -> [CDecl] -> m Type Source #

translate a type

tDirectType :: MonadTrav m => Bool -> NodeInfo -> [CTypeQual] -> TypeSpecAnalysis -> m Type Source #

translate a type without (syntactic) indirections Due to the GNU `typeof` extension and typeDefs, this can be an arbitrary type

tNumType :: MonadCError m => NumTypeSpec -> m (Either (FloatType, Bool) IntType) Source #

Mapping from num type specs to C types (C99 6.7.2-2), ignoring the complex qualifier.

tArraySize :: MonadTrav m => CArrSize -> m ArraySize Source #

tTypeQuals :: MonadTrav m => [CTypeQual] -> m (TypeQuals, Attributes) Source #

mergeOldStyle :: MonadCError m => NodeInfo -> [CDecl] -> [CDerivedDeclr] -> m [CDerivedDeclr] Source #

convert old style parameters

This requires matching parameter names and declarations, as in the following example:

    int f(d,c,a,b)
    char a,*b;
    int c;
    { }

is converted to

    int f(int d, int c, char a, char* b)

TODO: This could be moved to syntax, as it operates on the AST only

# Dissecting type specs

canonicalTypeSpec :: MonadTrav m => [CTypeSpec] -> m TypeSpecAnalysis Source #

data NumBaseType Source #

Constructors

NoBaseType |
BaseChar |
BaseInt |
BaseInt128 |
BaseFloat |
BaseBFloat16 |
BaseFloatN Int Bool |
BaseDouble |

#### Instances

Instances details

Eq NumBaseType Source # |
Instance details Defined in Language.C.Analysis.DeclAnalysis Methods (==) :: NumBaseType -> NumBaseType -> Bool Source # (/=) :: NumBaseType -> NumBaseType -> Bool Source #
Ord NumBaseType Source # |

Instance details

Defined in Language.C.Analysis.DeclAnalysis

Methods

compare :: NumBaseType -> NumBaseType -> Ordering Source #

(<) :: NumBaseType -> NumBaseType -> Bool Source #

(<=) :: NumBaseType -> NumBaseType -> Bool Source #

(>) :: NumBaseType -> NumBaseType -> Bool Source #

(>=) :: NumBaseType -> NumBaseType -> Bool Source #

max :: NumBaseType -> NumBaseType -> NumBaseType Source #

min :: NumBaseType -> NumBaseType -> NumBaseType Source #

data SignSpec Source #

Constructors

NoSignSpec |
Signed |
Unsigned |

#### Instances

Instances details

Eq SignSpec Source # |
Instance details Defined in Language.C.Analysis.DeclAnalysis Methods (==) :: SignSpec -> SignSpec -> Bool Source # (/=) :: SignSpec -> SignSpec -> Bool Source #
Ord SignSpec Source # |

Instance details

Defined in Language.C.Analysis.DeclAnalysis

Methods

compare :: SignSpec -> SignSpec -> Ordering Source #

(<) :: SignSpec -> SignSpec -> Bool Source #

(<=) :: SignSpec -> SignSpec -> Bool Source #

(>) :: SignSpec -> SignSpec -> Bool Source #

(>=) :: SignSpec -> SignSpec -> Bool Source #

max :: SignSpec -> SignSpec -> SignSpec Source #

min :: SignSpec -> SignSpec -> SignSpec Source #

data SizeMod Source #

Constructors

NoSizeMod |
ShortMod |
LongMod |
LongLongMod |

#### Instances

Instances details

Eq SizeMod Source # |
Instance details Defined in Language.C.Analysis.DeclAnalysis Methods (==) :: SizeMod -> SizeMod -> Bool Source # (/=) :: SizeMod -> SizeMod -> Bool Source #
Ord SizeMod Source # |
Instance details Defined in Language.C.Analysis.DeclAnalysis Methods compare :: SizeMod -> SizeMod -> Ordering Source # (<) :: SizeMod -> SizeMod -> Bool Source # (<=) :: SizeMod -> SizeMod -> Bool Source # (>) :: SizeMod -> SizeMod -> Bool Source # (>=) :: SizeMod -> SizeMod -> Bool Source # max :: SizeMod -> SizeMod -> SizeMod Source # min :: SizeMod -> SizeMod -> SizeMod Source #

data NumTypeSpec Source #

Constructors

NumTypeSpec |

Fields

- base :: NumBaseType
- signSpec :: SignSpec
- sizeMod :: SizeMod
- isComplex :: Bool

data TypeSpecAnalysis Source #

Constructors

TSNone |
TSVoid |
TSBool |
TSNum NumTypeSpec |
TSTypeDef TypeDefRef |
TSType Type |
TSNonBasic CTypeSpec |

canonicalStorageSpec :: MonadCError m => [CStorageSpec] -> m StorageSpec Source #

data StorageSpec Source #

Constructors

NoStorageSpec |
AutoSpec |
RegSpec |
ThreadSpec |
StaticSpec Bool |
ExternSpec Bool |
ClKernelSpec |
ClGlobalSpec |
ClLocalSpec |

#### Instances

Instances details

Read StorageSpec Source # |
Instance details Defined in Language.C.Analysis.DeclAnalysis Methods readsPrec :: Int -> ReadS StorageSpec Source # readList :: ReadS [StorageSpec] Source # readPrec :: ReadPrec StorageSpec Source # readListPrec :: ReadPrec [StorageSpec] Source #
Show StorageSpec Source # |
Instance details Defined in Language.C.Analysis.DeclAnalysis Methods showsPrec :: Int -> StorageSpec -> ShowS Source # show :: StorageSpec -> String Source # showList :: [StorageSpec] -> ShowS Source #
Eq StorageSpec Source # |
Instance details Defined in Language.C.Analysis.DeclAnalysis Methods (==) :: StorageSpec -> StorageSpec -> Bool Source # (/=) :: StorageSpec -> StorageSpec -> Bool Source #
Ord StorageSpec Source # |

Instance details

Defined in Language.C.Analysis.DeclAnalysis

Methods

compare :: StorageSpec -> StorageSpec -> Ordering Source #

(<) :: StorageSpec -> StorageSpec -> Bool Source #

(<=) :: StorageSpec -> StorageSpec -> Bool Source #

(>) :: StorageSpec -> StorageSpec -> Bool Source #

(>=) :: StorageSpec -> StorageSpec -> Bool Source #

max :: StorageSpec -> StorageSpec -> StorageSpec Source #

min :: StorageSpec -> StorageSpec -> StorageSpec Source #

hasThreadLocalSpec :: StorageSpec -> Bool Source #

hasClKernelSpec :: StorageSpec -> Bool Source #

isTypeDef :: [CDeclSpec] -> Bool Source #

# Helpers

data VarDeclInfo Source #

Constructors

VarDeclInfo VarName FunctionAttrs StorageSpec Attributes Type NodeInfo |

tAttr :: (MonadCError m, MonadSymtab m) => CAttr -> m Attr Source #

translate `attribute` annotations TODO: This is a unwrap and wrap stub

mkVarName :: (MonadCError m, MonadSymtab m) => NodeInfo -> Maybe Ident -> Maybe AsmName -> m VarName Source #

construct a name for a variable TODO: more or less bogus

getOnlyDeclr :: MonadCError m => CDecl -> m CDeclr Source #

nameOfDecl :: MonadCError m => CDecl -> m Ident Source #

analyseVarDecl :: MonadTrav m => Bool -> [CStorageSpec] -> [CAttr] -> [CTypeQual] -> TypeSpecAnalysis -> [CFunSpec] -> CDeclr -> [CDecl] -> Maybe CInit -> m VarDeclInfo Source #

analyse declarators

analyseVarDecl' :: MonadTrav m => Bool -> [CDeclSpec] -> CDeclr -> [CDecl] -> Maybe CInit -> m VarDeclInfo Source #