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

Language.C.Analysis.TravMonad

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

Contents

AST traversal monad
Handling declarations
Symbol table scope modification
Symbol table lookup (delegate)
Symbol table modification
Additional error handling facilities
Trav - default MonadTrav implementation
Helpers

Description
Monad for Traversals of the C AST. For the traversal, we maintain a symboltable and need MonadError and unique name generation facilities. Furthermore, the user may provide callbacks to handle declarations and definitions.
Synopsis

class Monad m => MonadTrav m where

throwTravError :: Error e => e -> m a
catchTravError :: m a -> (CError -> m a) -> m a
recordError :: Error e => e -> m ()
getErrors :: m [CError]
getDefTable :: m DefTable
withDefTable :: (DefTable -> (a, DefTable)) -> m a
genName :: m Name
handleDecl :: DeclEvent -> m ()

handleTagDecl :: MonadTrav m => TagFwdDecl -> m ()
handleTagDef :: MonadTrav m => TagDef -> m ()
handleEnumeratorDef :: MonadTrav m => Enumerator -> m ()
handleTypeDef :: MonadTrav m => TypeDef -> m ()
handleObjectDef :: MonadTrav m => Ident -> ObjDef -> m ()
handleFunDef :: MonadTrav m => Ident -> FunDef -> m ()
handleVarDecl :: MonadTrav m => Decl -> m ()
handleParamDecl :: MonadTrav m => ParamDecl -> m ()
handleAsmBlock :: MonadTrav m => AsmBlock -> m ()
enterPrototypeScope :: MonadTrav m => m ()
leavePrototypeScope :: MonadTrav m => m ()
enterFunctionScope :: MonadTrav m => m ()
leaveFunctionScope :: MonadTrav m => m ()
enterBlockScope :: MonadTrav m => m ()
leaveBlockScope :: MonadTrav m => m ()
lookupTypeDef :: MonadTrav m => Ident -> m Type
lookupObject :: MonadTrav m => Ident -> m (Maybe IdentDecl)
createSUERef :: MonadTrav m => NodeInfo -> Maybe Ident -> m SUERef
hadHardErrors :: [CError] -> Bool
handleTravError :: MonadTrav m => m a -> m (Maybe a)
throwOnLeft :: (MonadTrav m, Error e) => Either e a -> m a
astError :: MonadTrav m => NodeInfo -> String -> m a
warn :: (Error e, MonadTrav m) => e -> m ()
data Trav s a
runTrav :: forall s a. s -> Trav s a -> Either [CError] (a, TravState s)
runTrav_ :: Trav () a -> Either [CError] (a, [CError])
data TravState s
initTravState :: s -> TravState s
withExtDeclHandler :: Trav s a -> (DeclEvent -> Trav s ()) -> Trav s a
modifyUserState :: (s -> s) -> Trav s ()
userState :: TravState s -> s
mapMaybeM :: Monad m => Maybe a -> (a -> m b) -> m (Maybe b)
maybeM :: Monad m => Maybe a -> (a -> m ()) -> m ()
mapSndM :: Monad m => (b -> m c) -> (a, b) -> m (a, c)
concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]

AST traversal monad

class Monad m => MonadTrav m where | Source

Traversal monad
Methods

throwTravError :: Error e => e -> m a | Source

throw an `Error`

catchTravError :: m a -> (CError -> m a) -> m a | Source

catch an `Error` (we could implement dynamically-typed catch here)

recordError :: Error e => e -> m () | Source

remember that an `Error` occured (without throwing it)

getErrors :: m [CError] | Source

return the list of recorded errors

getDefTable :: m DefTable | Source

return the definition table

withDefTable :: (DefTable -> (a, DefTable)) -> m a | Source

perform an action modifying the definition table

genName :: m Name | Source

unique name generation

handleDecl :: DeclEvent -> m () | Source

handling declarations and definitions

Instances

MonadTrav (Trav s)

Handling declarations

handleTagDecl :: MonadTrav m => TagFwdDecl -> m () | Source

forward declaration of a tag. Only neccessary for name analysis, but otherwise no semantic consequences.

handleTagDef :: MonadTrav m => TagDef -> m () | Source

define the given composite type or enumeration If there is a declaration visible, overwrite it with the definition. Otherwise, enter a new definition in the current namespace. If there is already a definition present, yield an error (redeclaration).

handleEnumeratorDef :: MonadTrav m => Enumerator -> m () | Source

handleTypeDef :: MonadTrav m => TypeDef -> m () | Source

handleObjectDef :: MonadTrav m => Ident -> ObjDef -> m () | Source

handle object defintions (maybe tentative)

handleFunDef :: MonadTrav m => Ident -> FunDef -> m () | Source

handle function definitions

handleVarDecl :: MonadTrav m => Decl -> m () | Source

handle variable declarations (external object declarations and function prototypes) variable declarations are either function prototypes, or external declarations, and not very interesting on their own. we only put them in the symbol table and call the handle. declarations never override definitions

handleParamDecl :: MonadTrav m => ParamDecl -> m () | Source

handle parameter declaration. The interesting part is that parameters can be abstract (if they are part of a type). If they have a name, we enter the name (usually in function prototype or function scope), checking if there are duplicate definitions. FIXME: I think it would be more transparent to handle parameter declarations in a special way

handleAsmBlock :: MonadTrav m => AsmBlock -> m () | Source

Symbol table scope modification

enterPrototypeScope :: MonadTrav m => m () | Source

leavePrototypeScope :: MonadTrav m => m () | Source

enterFunctionScope :: MonadTrav m => m () | Source

leaveFunctionScope :: MonadTrav m => m () | Source

enterBlockScope :: MonadTrav m => m () | Source

leaveBlockScope :: MonadTrav m => m () | Source

Symbol table lookup (delegate)

lookupTypeDef :: MonadTrav m => Ident -> m Type | Source

lookup a type definition the 'wrong kind of object' is an internal error here, because the parser should distinguish typeDefs and other objects

lookupObject :: MonadTrav m => Ident -> m (Maybe IdentDecl) | Source

lookup an object, function or enumerator
Symbol table modification

createSUERef :: MonadTrav m => NodeInfo -> Maybe Ident -> m SUERef | Source

create a reference to a struct/union/enum This currently depends on the fact the structs are tagged with unique names. We could use the name generation of TravMonad as well, which might be the better choice when dealing with autogenerated code.
Additional error handling facilities

hadHardErrors :: [CError] -> Bool | Source

check wheter non-recoverable errors occured

handleTravError :: MonadTrav m => m a -> m (Maybe a) | Source

throwOnLeft :: (MonadTrav m, Error e) => Either e a -> m a | Source

raise an error based on an Either argument

astError :: MonadTrav m => NodeInfo -> String -> m a | Source

raise an error caused by a malformed AST

warn :: (Error e, MonadTrav m) => e -> m () | Source

Trav - default MonadTrav implementation

data Trav s a | Source

simple traversal monad, providing user state and callbacks
Instances

Monad (Trav s)
MonadTrav (Trav s)

runTrav :: forall s a. s -> Trav s a -> Either [CError] (a, TravState s) | Source

runTrav_ :: Trav () a -> Either [CError] (a, [CError]) | Source

data TravState s | Source

initTravState :: s -> TravState s | Source

withExtDeclHandler :: Trav s a -> (DeclEvent -> Trav s ()) -> Trav s a | Source

modifyUserState :: (s -> s) -> Trav s () | Source

userState :: TravState s -> s | Source

Helpers

mapMaybeM :: Monad m => Maybe a -> (a -> m b) -> m (Maybe b) | Source

maybeM :: Monad m => Maybe a -> (a -> m ()) -> m () | Source

mapSndM :: Monad m => (b -> m c) -> (a, b) -> m (a, c) | Source

concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b] | Source

Produced by Haddock version 2.4.2