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





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.


AST traversal monad

class Monad m => MonadTrav m whereSource

Traversal monad


throwTravError :: Error e => e -> m aSource

throw an Error

catchTravError :: m a -> (CError -> m a) -> m aSource

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 DefTableSource

return the definition table

withDefTable :: (DefTable -> (a, DefTable)) -> m aSource

perform an action modifying the definition table

genName :: m NameSource

unique name generation

handleDecl :: DeclEvent -> m ()Source

handling declarations and definitions


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).

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

Symbol table scope modification

Symbol table lookup (delegate)

lookupTypeDef :: MonadTrav m => Ident -> m TypeSource

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 SUERefSource

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] -> BoolSource

check wheter non-recoverable errors occured

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

raise an error based on an Either argument

astError :: MonadTrav m => NodeInfo -> String -> m aSource

raise an error caused by a malformed AST

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

Trav - default MonadTrav implementation

newtype Trav s a Source

simple traversal monad, providing user state and callbacks




unTrav :: TravState s -> Either CError (a, TravState s)


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

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


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