Copyright | (c) 2008 Benedikt Huber |
---|---|
License | BSD-style |
Maintainer | benedikt.huber@gmail.com |
Stability | alpha |
Portability | ghc |
Safe Haskell | None |
Language | Haskell98 |
Language.C.Analysis.SemRep
Contents
Description
This module contains definitions for representing C translation units.
In contrast to AST
, the representation tries to express the semantics of
of a translation unit.
- data TagDef
- typeOfTagDef :: TagDef -> TypeName
- class Declaration n where
- declIdent :: Declaration n => n -> Ident
- declName :: Declaration n => n -> VarName
- declType :: Declaration n => n -> Type
- declAttrs :: Declaration n => n -> DeclAttrs
- data IdentDecl
- objKindDescr :: IdentDecl -> String
- splitIdentDecls :: Bool -> Map Ident IdentDecl -> (Map Ident Decl, (Map Ident Enumerator, Map Ident ObjDef, Map Ident FunDef))
- data GlobalDecls = GlobalDecls {}
- emptyGlobalDecls :: GlobalDecls
- filterGlobalDecls :: (DeclEvent -> Bool) -> GlobalDecls -> GlobalDecls
- mergeGlobalDecls :: GlobalDecls -> GlobalDecls -> GlobalDecls
- data DeclEvent
- data Decl = Decl VarDecl NodeInfo
- data ObjDef = ObjDef VarDecl (Maybe Initializer) NodeInfo
- isTentative :: ObjDef -> Bool
- data FunDef = FunDef VarDecl Stmt NodeInfo
- data ParamDecl
- data MemberDecl
- data TypeDef = TypeDef Ident Type Attributes NodeInfo
- identOfTypeDef :: TypeDef -> Ident
- data VarDecl = VarDecl VarName DeclAttrs Type
- data DeclAttrs = DeclAttrs FunctionAttrs Storage Attributes
- isExtDecl :: Declaration n => n -> Bool
- data FunctionAttrs = FunctionAttrs {
- isInline :: Bool
- isNoreturn :: Bool
- functionAttrs :: Declaration d => d -> FunctionAttrs
- noFunctionAttrs :: FunctionAttrs
- data Storage
- declStorage :: Declaration d => d -> Storage
- type ThreadLocal = Bool
- type Register = Bool
- data Linkage
- hasLinkage :: Storage -> Bool
- declLinkage :: Declaration d => d -> Linkage
- data Type
- data FunType
- data ArraySize
- data TypeDefRef = TypeDefRef Ident Type NodeInfo
- data TypeName
- data BuiltinType
- data IntType
- data FloatType
- class HasSUERef a where
- class HasCompTyKind a where
- data CompTypeRef = CompTypeRef SUERef CompTyKind NodeInfo
- data CompType = CompType SUERef CompTyKind [MemberDecl] Attributes NodeInfo
- typeOfCompDef :: CompType -> TypeName
- data CompTyKind
- 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 {}
- noTypeQuals :: TypeQuals
- mergeTypeQuals :: TypeQuals -> TypeQuals -> TypeQuals
- data VarName
- identOfVarName :: VarName -> Ident
- isNoName :: VarName -> Bool
- type AsmName = CStrLit
- data Attr = Attr Ident [Expr] NodeInfo
- type Attributes = [Attr]
- noAttributes :: Attributes
- mergeAttributes :: Attributes -> Attributes -> Attributes
- type Stmt = CStat
- type Expr = CExpr
- type Initializer = CInit
- type AsmBlock = CStrLit
Sums of tags and identifiers
Composite type definitions (tags)
typeOfTagDef :: TagDef -> TypeName #
return the type corresponding to a tag definition
class Declaration n where #
All datatypes aggregating a declaration are instances of Declaration
Minimal complete definition
Methods
getVarDecl :: n -> VarDecl #
get the name, type and declaration attributes of a declaration or definition
Instances
declIdent :: Declaration n => n -> Ident #
get the variable identifier of a declaration (only safe if the the declaration is known to have a name)
declName :: Declaration n => n -> VarName #
get the variable name of a Declaration
declType :: Declaration n => n -> Type #
get the type of a Declaration
declAttrs :: Declaration n => n -> DeclAttrs #
get the declaration attributes of a Declaration
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 |
objKindDescr :: IdentDecl -> String #
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)) #
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 #
global declaration/definition table returned by the analysis
emptyGlobalDecls :: GlobalDecls #
empty global declaration table
filterGlobalDecls :: (DeclEvent -> Bool) -> GlobalDecls -> GlobalDecls #
filter global declarations
mergeGlobalDecls :: GlobalDecls -> GlobalDecls -> GlobalDecls #
merge global declarations
Events for visitors
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 |
ParamEvent ParamDecl | parameter declaration |
LocalEvent IdentDecl | local variable declaration or definition |
TypeDefEvent TypeDef | a type definition |
AsmEvent AsmBlock | assembler block |
Declarations and definitions
Declarations, which aren't definitions
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 |
isTentative :: ObjDef -> Bool #
Returns True
if the given object definition is tentative.
Function definitions
A function definition is a declaration together with a statement (the function body).
Parameter declaration
data MemberDecl #
Struct/Union member declaration
Constructors
MemberDecl VarDecl (Maybe Expr) NodeInfo | MemberDecl vardecl bitfieldsize node |
AnonBitField Type Expr NodeInfo | AnonBitField typ size |
Instances
typedef
definitions.
The identifier is a new name for the given type.
Constructors
TypeDef Ident Type Attributes NodeInfo |
identOfTypeDef :: TypeDef -> Ident #
return the idenitifier of a typedef
Generic variable declarations
Declaration attributes
Declaration attributes of the form DeclAttrs isInlineFunction storage linkage attrs
They specify the storage and linkage of a declared object.
Constructors
DeclAttrs FunctionAttrs Storage Attributes | DeclAttrs fspecs storage attrs |
isExtDecl :: Declaration n => n -> Bool #
functionAttrs :: Declaration d => d -> FunctionAttrs #
get the `function attributes' of a declaration
Storage duration and linkage of a variable
Constructors
NoStorage | no storage |
Auto Register | automatic storage (optional: register) |
Static Linkage ThreadLocal | static storage, linkage spec and thread local specifier (gnu c) |
FunLinkage Linkage | function, either internal or external linkage |
declStorage :: Declaration d => d -> Storage #
get the Storage
of a declaration
type ThreadLocal = Bool #
Linkage: Either no linkage, internal to the translation unit or external
Constructors
NoLinkage | |
InternalLinkage | |
ExternalLinkage |
hasLinkage :: Storage -> Bool #
return True
if the object has linkage
declLinkage :: Declaration d => d -> Linkage #
Get the linkage of a definition
Types
types of C objects
Constructors
DirectType TypeName TypeQuals Attributes | a non-derived type |
PtrType Type TypeQuals Attributes | pointer type |
ArrayType Type ArraySize TypeQuals Attributes | array type |
FunctionType FunType Attributes | function type |
TypeDefType TypeDefRef TypeQuals Attributes | a defined type |
Function types are of the form FunType return-type params isVariadic
.
If the parameter types aren't yet known, the function has type FunTypeIncomplete type attrs
.
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 |
data TypeDefRef #
typdef references If the actual type is known, it is attached for convenience
Constructors
TypeDefRef Ident Type NodeInfo |
Instances
normalized type representation
integral types (C99 6.7.2.2)
floating point type (C99 6.7.2.2)
accessor class : struct/union/enum names
Minimal complete definition
class HasCompTyKind a where #
accessor class : composite type tags (struct or union)
Minimal complete definition
Methods
compTag :: a -> CompTyKind #
Instances
data CompTypeRef #
composite type declarations
Constructors
CompTypeRef SUERef CompTyKind NodeInfo |
Instances
Composite type (struct or union).
Constructors
CompType SUERef CompTyKind [MemberDecl] Attributes NodeInfo |
typeOfCompDef :: CompType -> TypeName #
return the type of a composite type definition
Representation of C enumeration types
Constructors
EnumType SUERef [Enumerator] Attributes NodeInfo | EnumType name enumeration-constants attrs node |
typeOfEnumDef :: EnumType -> TypeName #
return the type of an enum definition
data Enumerator #
An Enumerator consists of an identifier, a constant expressions and the link to its type
Constructors
Enumerator Ident Expr EnumType NodeInfo |
Instances
Type qualifiers: constant, volatile and restrict
Constructors
TypeQuals | |
no type qualifiers
mergeTypeQuals :: TypeQuals -> TypeQuals -> TypeQuals #
merge (&&) two type qualifier sets
Variable names
VarName name assembler-name
is a name of an declared object
identOfVarName :: VarName -> Ident #
Attributes (STUB, not yet analyzed)
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
type Attributes = [Attr] #
Empty attribute list
mergeAttributes :: Attributes -> Attributes -> Attributes #
Merge attribute lists TODO: currently does not remove duplicates
Statements and Expressions (STUB, aliases to Syntax)
type Initializer = CInit #
Initializer
is currently an alias for CInit
.
We're planning a normalized representation, but this depends on the implementation of constant expression evaluation