| language-c-0.3.2: Analysis and generation of C code | Source code | Contents | Index

Language.C.Analysis.SemRep

Portability | ghc
Stability | alpha
Maintainer | benedikt.huber@gmail.com

Contents

Sums of tags and identifiers
Global definitions
Events for visitors
Declarations and definitions
Declaration attributes
Types
Variable names
Attributes (STUB, not yet analyzed)
Statements and Expressions (STUB, aliases to Syntax)

Description
This module contains definitions for representing C translation units. In contrast to Language.C.Syntax.AST, the representation tries to express the semantics of of a translation unit.
Synopsis

data TagDef

= CompDef CompType
| EnumDef EnumType

typeOfTagDef :: TagDef -> TypeName
class Declaration n where

getVarDecl :: n -> VarDecl

declIdent :: Declaration n => n -> Ident
declName :: Declaration n => n -> VarName
declType :: Declaration n => n -> Type
declAttrs :: Declaration n => n -> DeclAttrs

data IdentDecl

= Declaration Decl
| ObjectDef ObjDef
| FunctionDef FunDef
| EnumeratorDef Enumerator

objKindDescr :: IdentDecl -> String
splitIdentDecls :: Bool -> Map Ident IdentDecl -> (Map Ident Decl, (Map Ident Enumerator, Map Ident ObjDef, Map Ident FunDef))

data GlobalDecls = GlobalDecls {

gObjs :: Map Ident IdentDecl
gTags :: Map SUERef TagDef
gTypeDefs :: Map Ident TypeDef

}

emptyGlobalDecls :: GlobalDecls
filterGlobalDecls :: (DeclEvent -> Bool) -> GlobalDecls -> GlobalDecls
mergeGlobalDecls :: GlobalDecls -> GlobalDecls -> GlobalDecls

data DeclEvent

= TagEvent TagDef
| DeclEvent IdentDecl
| TypeDefEvent TypeDef
| AsmEvent AsmBlock

data Decl = Decl VarDecl NodeInfo
data ObjDef = ObjDef VarDecl (Maybe Initializer) NodeInfo
isTentative :: ObjDef -> Bool
data FunDef = FunDef VarDecl Stmt NodeInfo

data ParamDecl

= ParamDecl VarDecl NodeInfo
| AbstractParamDecl VarDecl NodeInfo

data MemberDecl

= MemberDecl VarDecl (Maybe Expr) NodeInfo
| AnonBitField Type Expr NodeInfo

data TypeDef = TypeDef Ident Type Attributes NodeInfo
identOfTypeDef :: TypeDef -> Ident
data VarDecl = VarDecl VarName DeclAttrs Type
data DeclAttrs = DeclAttrs Bool Storage Attributes
isExtDecl :: Declaration n => n -> Bool

data Storage

= NoStorage
| Auto Register
| Static Linkage ThreadLocal
| FunLinkage Linkage

declStorage :: Declaration d => d -> Storage
type ThreadLocal = Bool
type Register = Bool

data Linkage

= InternalLinkage
| ExternalLinkage

data Type

= DirectType TypeName TypeQuals
| PtrType Type TypeQuals Attributes
| ArrayType Type ArraySize TypeQuals Attributes
| FunctionType FunType
| TypeDefType TypeDefRef
| TypeOfExpr Expr

data FunType

= FunType Type [ParamDecl] Bool Attributes
| FunTypeIncomplete Type Attributes

isFunctionType :: Type -> Bool
derefTypeDef :: Type -> Type
referencedType :: Type -> Maybe Type
hasTypeOfExpr :: Type -> Bool

data ArraySize

= UnknownArraySize Bool
| ArraySize Bool Expr

data TypeDefRef = TypeDefRef Ident (Maybe Type) NodeInfo

data TypeName

= TyVoid
| TyIntegral IntType
| TyFloating FloatType
| TyComplex FloatType
| TyComp CompTypeRef
| TyEnum EnumTypeRef
| TyBuiltin BuiltinType

data BuiltinType = TyVaList

data IntType

= TyBool
| TyChar
| TySChar
| TyUChar
| TyShort
| TyUShort
| TyInt
| TyUInt
| TyLong
| TyULong
| TyLLong
| TyULLong

data FloatType

= TyFloat
| TyDouble
| TyLDouble

class HasSUERef a where

sueRef :: a -> SUERef

class HasCompTyKind a where

compTag :: a -> CompTyKind

data CompTypeRef = CompTypeRef SUERef CompTyKind NodeInfo
data CompType = CompType SUERef CompTyKind [MemberDecl] Attributes NodeInfo
typeOfCompDef :: CompType -> TypeName

data CompTyKind

= StructTag
| UnionTag

data EnumTypeRef = EnumTypeRef SUERef NodeInfo
data EnumType = EnumType SUERef [Enumerator] Attributes NodeInfo
typeOfEnumDef :: EnumType -> TypeName
data Enumerator = Enumerator Ident Expr EnumType NodeInfo

data TypeQuals = TypeQuals {

constant :: Bool
volatile :: Bool
restrict :: Bool

}

noTypeQuals :: TypeQuals
mergeTypeQuals :: TypeQuals -> TypeQuals -> TypeQuals

data VarName

= VarName Ident (Maybe AsmName)
| NoName

identOfVarName :: VarName -> Ident
type AsmName = CStrLit
data Attr = Attr Ident [Expr] NodeInfo
type Attributes = [Attr]
type Stmt = CStat
type Expr = CExpr
type Initializer = CInit
type AsmBlock = CStrLit

Sums of tags and identifiers

data TagDef | Source

Composite type definitions (tags)
Constructors

CompDef CompType |
EnumDef EnumType |

Instances

Data TagDef
Typeable TagDef
Pos TagDef
CNode TagDef
Pretty TagDef
HasSUERef TagDef

typeOfTagDef :: TagDef -> TypeName | Source

return the type corresponding to a tag definition

class Declaration n where | Source

All datatypes aggregating a declaration are instances of `Declaration`
Methods

getVarDecl :: n -> VarDecl | Source

get the name, type and declaration attributes of a declaration or definition

Instances

Declaration Enumerator
Declaration VarDecl
Declaration MemberDecl
Declaration ParamDecl
Declaration FunDef
Declaration ObjDef
Declaration Decl
Declaration IdentDecl
(Declaration a, Declaration b) => Declaration (Either a b)

declIdent :: Declaration n => n -> Ident | Source

get the variable identifier of a declaration (only safe if the the declaration is known to have a name)

declName :: Declaration n => n -> VarName | Source

get the variable name of a `Declaration`

declType :: Declaration n => n -> Type | Source

get the type of a `Declaration`

declAttrs :: Declaration n => n -> DeclAttrs | Source

get the declaration attributes of a `Declaration`

data IdentDecl | Source

identifiers, typedefs and enumeration constants (namespace sum)
Constructors

Declaration Decl | object or function declaration
ObjectDef ObjDef | object definition
FunctionDef FunDef | function definition
EnumeratorDef Enumerator | definition of an enumerator

Instances

Data IdentDecl
Typeable IdentDecl
Pos IdentDecl
CNode IdentDecl
Pretty IdentDecl
Declaration IdentDecl

objKindDescr :: IdentDecl -> String | Source

textual description of the kind of an object

splitIdentDecls :: Bool -> Map Ident IdentDecl -> (Map Ident Decl, (Map Ident Enumerator, Map Ident ObjDef, Map Ident FunDef)) | Source

`splitIdentDecls includeAllDecls` splits a map of object, function and enumerator declarations and definitions into one map holding declarations, and three maps for object definitions, enumerator definitions and function definitions. If `includeAllDecls` is `True` all declarations are present in the first map, otherwise only those where no corresponding definition is available.
Global definitions

data GlobalDecls | Source

global declaration/definition table returned by the analysis
Constructors

GlobalDecls |

gObjs :: Map Ident IdentDecl |
gTags :: Map SUERef TagDef |
gTypeDefs :: Map Ident TypeDef |

Instances

Pretty GlobalDecls

emptyGlobalDecls :: GlobalDecls | Source

empty global declaration table

filterGlobalDecls :: (DeclEvent -> Bool) -> GlobalDecls -> GlobalDecls | Source

filter global declarations

mergeGlobalDecls :: GlobalDecls -> GlobalDecls -> GlobalDecls | Source

merge global declarations
Events for visitors

data DeclEvent | Source

Declaration events Those events are reported to callbacks, which are executed during the traversal.
Constructors

TagEvent TagDef | file-scope struct/union/enum event
DeclEvent IdentDecl | file-scope declaration or definition
TypeDefEvent TypeDef | a type definition
AsmEvent AsmBlock | assembler block

Instances

Pos DeclEvent
CNode DeclEvent

Declarations and definitions

data Decl | Source

Declarations, which aren't definitions
Constructors

Decl VarDecl NodeInfo |

Instances

Data Decl
Typeable Decl
Pos Decl
CNode Decl
Pretty Decl
Declaration Decl

data ObjDef | Source

Object Definitions An object definition is a declaration together with an initializer. If the initializer is missing, it is a tentative definition, i.e. a definition which might be overriden later on.
Constructors

ObjDef VarDecl (Maybe Initializer) NodeInfo |

Instances

Data ObjDef
Typeable ObjDef
Pos ObjDef
CNode ObjDef
Pretty ObjDef
Declaration ObjDef

isTentative :: ObjDef -> Bool | Source

Returns `True` if the given object definition is tentative.

data FunDef | Source

Function definitions A function definition is a declaration together with a statement (the function body).
Constructors

FunDef VarDecl Stmt NodeInfo |

Instances

Data FunDef
Typeable FunDef
Pos FunDef
CNode FunDef
Pretty FunDef
Declaration FunDef

data ParamDecl | Source

Parameter declaration
Constructors

ParamDecl VarDecl NodeInfo |
AbstractParamDecl VarDecl NodeInfo |

Instances

Data ParamDecl
Typeable ParamDecl
Pos ParamDecl
CNode ParamDecl
Pretty ParamDecl
Declaration ParamDecl

data MemberDecl | Source

Struct/Union member declaration
Constructors

MemberDecl VarDecl (Maybe Expr) NodeInfo

    MemberDecl vardecl bitfieldsize node

AnonBitField Type Expr NodeInfo

    AnonBitField typ size

Instances

Data MemberDecl
Typeable MemberDecl
Pos MemberDecl
CNode MemberDecl
Pretty MemberDecl
Declaration MemberDecl

data TypeDef | Source

`typedef` definitions. The identifier is a new name for the given type.
Constructors

TypeDef Ident Type Attributes NodeInfo |

Instances

Data TypeDef
Typeable TypeDef
Pos TypeDef
CNode TypeDef
Pretty TypeDef

identOfTypeDef :: TypeDef -> Ident | Source

return the idenitifier of a `typedef`

data VarDecl | Source

Generic variable declarations
Constructors

VarDecl VarName DeclAttrs Type |

Instances

Data VarDecl
Typeable VarDecl
Pretty VarDecl
Declaration VarDecl

Declaration attributes

data DeclAttrs | Source

Declaration attributes of the form `DeclAttrs isInlineFunction storage linkage attrs` They specify the storage and linkage of a declared object.
Constructors

DeclAttrs Bool Storage Attributes

    DeclAttrs inline storage attrs

Instances

Data DeclAttrs
Typeable DeclAttrs
Pretty DeclAttrs

isExtDecl :: Declaration n => n -> Bool | Source

data Storage | Source

Storage duration and linkage of a variable
Constructors

NoStorage | no storage
Auto Register | automatic storage (optional: register)
Static Linkage ThreadLocal | static storage, with linkage and thread local specifier (gnu c)
FunLinkage Linkage | function, either internal or external linkage

Instances

Eq Storage
Data Storage
Ord Storage
Show Storage
Typeable Storage
Pretty Storage

declStorage :: Declaration d => d -> Storage | Source

get the `Storage` of a declaration

type ThreadLocal = Bool | Source

type Register = Bool | Source

data Linkage | Source

Linkage: Either internal to the translation unit or external
Constructors

InternalLinkage |
ExternalLinkage |

Instances

Eq Linkage
Data Linkage
Ord Linkage
Show Linkage
Typeable Linkage
Pretty Linkage

Types

data Type | Source

types of C objects
Constructors

DirectType TypeName TypeQuals | a non-derived type
PtrType Type TypeQuals Attributes | pointer type
ArrayType Type ArraySize TypeQuals Attributes | array type
FunctionType FunType | function type
TypeDefType TypeDefRef | a defined type
TypeOfExpr Expr | (GNU) typeof (broken and should be removed, but we do not yet have expression type analysis)

Instances

Data Type
Typeable Type
Pretty Type

data FunType | Source

Function types are of the form `FunType return-type params isVariadic attrs`. If the parameter types aren't yet known, the function has type `FunTypeIncomplete type attrs`.
Constructors

FunType Type [ParamDecl] Bool Attributes |
FunTypeIncomplete Type Attributes |

Instances

Data FunType
Typeable FunType

isFunctionType :: Type -> Bool | Source

return `True` if the given type is a function type Result is undefined in the presence of TypeOfExpr or undefined typeDefs

derefTypeDef :: Type -> Type | Source

resolve typedefs, if possible

referencedType :: Type -> Maybe Type | Source

hasTypeOfExpr :: Type -> Bool | Source

data ArraySize | Source

An array type may either have unknown size or a specified array size, the latter either variable or constant. Furthermore, when used as a function parameters, the size may be qualified as static. In a function prototype, the size may be `Unspecified variable size' (`[*]`).
Constructors

UnknownArraySize Bool

    UnknownArraySize is-starred

ArraySize Bool Expr

    FixedSizeArray is-static size-expr

Instances

Data ArraySize
Typeable ArraySize

data TypeDefRef | Source

typdef references If the actual type is known, it is attached for convenience
Constructors

TypeDefRef Ident (Maybe Type) NodeInfo |

Instances

Data TypeDefRef
Typeable TypeDefRef
Pos TypeDefRef
CNode TypeDefRef

data TypeName | Source

normalized type representation
Constructors

TyVoid |
TyIntegral IntType |
TyFloating FloatType |
TyComplex FloatType |
TyComp CompTypeRef |
TyEnum EnumTypeRef |
TyBuiltin BuiltinType |

Instances

Data TypeName
Typeable TypeName

data BuiltinType | Source

Builtin type (va_list)
Constructors

TyVaList |

Instances

Data BuiltinType
Typeable BuiltinType

data IntType | Source

integral types (C99 6.7.2.2)
Constructors

TyBool |
TyChar |
TySChar |
TyUChar |
TyShort |
TyUShort |
TyInt |
TyUInt |
TyLong |
TyULong |
TyLLong |
TyULLong |

Instances

Eq IntType
Data IntType
Ord IntType
Show IntType
Typeable IntType

data FloatType | Source

floating point type (C99 6.7.2.2)
Constructors

TyFloat |
TyDouble |
TyLDouble |

Instances

Eq FloatType
Data FloatType
Ord FloatType
Show FloatType
Typeable FloatType

class HasSUERef a where | Source

accessor class : struct/union/enum names
Methods

sueRef :: a -> SUERef | Source

Instances

HasSUERef EnumType
HasSUERef CompType
HasSUERef EnumTypeRef
HasSUERef CompTypeRef
HasSUERef TagDef
HasSUERef TagFwdDecl

class HasCompTyKind a where | Source

accessor class : composite type tags (struct or union)
Methods

compTag :: a -> CompTyKind | Source

Instances

HasCompTyKind CompType
HasCompTyKind CompTypeRef

data CompTypeRef | Source

composite type declarations
Constructors

CompTypeRef SUERef CompTyKind NodeInfo |

Instances

Data CompTypeRef
Typeable CompTypeRef
Pos CompTypeRef
CNode CompTypeRef
HasCompTyKind CompTypeRef
HasSUERef CompTypeRef

data CompType | Source

Composite type (struct or union).
Constructors

CompType SUERef CompTyKind [MemberDecl] Attributes NodeInfo |

Instances

Data CompType
Typeable CompType
Pos CompType
CNode CompType
Pretty CompType
HasCompTyKind CompType
HasSUERef CompType

typeOfCompDef :: CompType -> TypeName | Source

return the type of a composite type definition

data CompTyKind | Source

a tag to determine wheter we refer to a `struct` or `union`, see `CompType`.
Constructors

StructTag |
UnionTag |

Instances

Eq CompTyKind
Data CompTyKind
Ord CompTyKind
Show CompTyKind
Typeable CompTyKind

data EnumTypeRef | Source

Constructors

EnumTypeRef SUERef NodeInfo |

Instances

Data EnumTypeRef
Typeable EnumTypeRef
Pos EnumTypeRef
CNode EnumTypeRef
HasSUERef EnumTypeRef

data EnumType | Source

Representation of C enumeration types
Constructors

EnumType SUERef [Enumerator] Attributes NodeInfo

    EnumType name enumeration-constants attrs node

Instances

Data EnumType
Typeable EnumType
Pos EnumType
CNode EnumType
Pretty EnumType
HasSUERef EnumType

typeOfEnumDef :: EnumType -> TypeName | Source

return the type of an enum definition

data Enumerator | Source

An Enumerator consists of an identifier, a constant expressions and the link to its type
Constructors

Enumerator Ident Expr EnumType NodeInfo |

Instances

Data Enumerator
Typeable Enumerator
Pos Enumerator
CNode Enumerator
Pretty Enumerator
Declaration Enumerator

data TypeQuals | Source

Type qualifiers: constant, volatile and restrict
Constructors

TypeQuals |

constant :: Bool |
volatile :: Bool |
restrict :: Bool |

Instances

Data TypeQuals
Typeable TypeQuals
Pretty TypeQuals

noTypeQuals :: TypeQuals | Source

no type qualifiers

mergeTypeQuals :: TypeQuals -> TypeQuals -> TypeQuals | Source

merge (&&) two type qualifier sets
Variable names

data VarName | Source

`VarName name assembler-name` is a name of an declared object
Constructors

VarName Ident (Maybe AsmName) |
NoName |

Instances

Data VarName
Typeable VarName
Pretty VarName

identOfVarName :: VarName -> Ident | Source

type AsmName = CStrLit | Source

Assembler name (alias for `CStrLit`)
Attributes (STUB, not yet analyzed)

data Attr | Source

`__attribute__` annotations

Those are of the form `Attr attribute-name attribute-parameters`, and serve as generic properties of some syntax tree elements.

Some examples:

- labels can be attributed with unused to indicate that their not used
- struct definitions can be attributed with packed to tell the compiler to use the most compact representation
- declarations can be attributed with deprecated
- function declarations can be attributes with noreturn to tell the compiler that the function will never return,
- or with const to indicate that it is a pure function

TODO: ultimatively, we want to parse attributes and represent them in a typed way

Constructors

Attr Ident [Expr] NodeInfo |

Instances

Data Attr
Typeable Attr
Pos Attr
CNode Attr
Pretty Attr

type Attributes = [Attr] | Source

Statements and Expressions (STUB, aliases to Syntax)

type Stmt = CStat | Source

`Stmt` is an alias for `CStat` (Syntax)

type Expr = CExpr | Source

`Expr` is currently an alias for `CExpr` (Syntax)

type Initializer = CInit | Source

`Initializer` is currently an alias for `CInit`. We're planning a normalized representation, but this depends on the implementation of constant expression evaluation

type AsmBlock = CStrLit | Source

Top level assembler block (alias for `CStrLit`)
Produced by Haddock version 2.4.2