Copyright | (c) 2008 Benedikt Huber |
---|---|
License | BSD-style |
Maintainer | benedikt.huber@gmail.com |
Stability | alpha |
Portability | ghc |
Safe Haskell | None |
Language | Haskell98 |
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.
- class Monad m => MonadName m where
- class Monad m => MonadSymtab m where
- class Monad m => MonadCError m where
- class (MonadName m, MonadSymtab m, MonadCError m) => MonadTrav m where
- handleTagDecl :: (MonadCError m, MonadSymtab m) => TagFwdDecl -> m ()
- handleTagDef :: MonadTrav m => TagDef -> m ()
- handleEnumeratorDef :: (MonadCError m, MonadSymtab m) => Enumerator -> m ()
- handleTypeDef :: MonadTrav m => TypeDef -> m ()
- handleObjectDef :: MonadTrav m => Bool -> Ident -> ObjDef -> m ()
- handleFunDef :: MonadTrav m => Ident -> FunDef -> m ()
- handleVarDecl :: MonadTrav m => Bool -> Decl -> m ()
- handleParamDecl :: MonadTrav m => ParamDecl -> m ()
- handleAsmBlock :: MonadTrav m => AsmBlock -> m ()
- enterPrototypeScope :: MonadSymtab m => m ()
- leavePrototypeScope :: MonadSymtab m => m ()
- enterFunctionScope :: MonadSymtab m => m ()
- leaveFunctionScope :: MonadSymtab m => m ()
- enterBlockScope :: MonadSymtab m => m ()
- leaveBlockScope :: MonadSymtab m => m ()
- lookupTypeDef :: (MonadCError m, MonadSymtab m) => Ident -> m Type
- lookupObject :: (MonadCError m, MonadSymtab m) => Ident -> m (Maybe IdentDecl)
- createSUERef :: (MonadCError m, MonadSymtab m) => NodeInfo -> Maybe Ident -> m SUERef
- hadHardErrors :: [CError] -> Bool
- handleTravError :: MonadCError m => m a -> m (Maybe a)
- throwOnLeft :: (MonadCError m, Error e) => Either e a -> m a
- astError :: MonadCError m => NodeInfo -> String -> m a
- warn :: (Error e, MonadCError 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
- getUserState :: Trav s s
- data TravOptions = TravOptions {}
- modifyOptions :: (TravOptions -> TravOptions) -> Trav s ()
- travErrors :: TravState s -> [CError]
- data CLanguage
- 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]
Name generation monad
Symbol table monad
class Monad m => MonadSymtab m where #
getDefTable :: m DefTable #
return the definition table
withDefTable :: (DefTable -> (a, DefTable)) -> m a #
perform an action modifying the definition table
MonadSymtab (Trav s) # | |
Specialized C error-handling monad
class Monad m => MonadCError m where #
throwTravError :: Error e => e -> m a #
throw an Error
catchTravError :: m a -> (CError -> m a) -> m a #
catch an Error
(we could implement dynamically-typed catch here)
recordError :: Error e => e -> m () #
remember that an Error
occurred (without throwing it)
return the list of recorded errors
MonadCError (Trav s) # | |
AST traversal monad
class (MonadName m, MonadSymtab m, MonadCError m) => MonadTrav m where #
Traversal monad
handleDecl :: DeclEvent -> m () #
handling declarations and definitions
Handling declarations
handleTagDecl :: (MonadCError m, MonadSymtab m) => TagFwdDecl -> m () #
forward declaration of a tag. Only necessary for name analysis, but otherwise no semantic consequences.
handleTagDef :: MonadTrav m => TagDef -> m () #
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 :: (MonadCError m, MonadSymtab m) => Enumerator -> m () #
handleTypeDef :: MonadTrav m => TypeDef -> m () #
handleObjectDef :: MonadTrav m => Bool -> Ident -> ObjDef -> m () #
handle object defintions (maybe tentative)
handleFunDef :: MonadTrav m => Ident -> FunDef -> m () #
handle function definitions
handleVarDecl :: MonadTrav m => Bool -> Decl -> m () #
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 () #
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 () #
Symbol table scope modification
enterPrototypeScope :: MonadSymtab m => m () #
leavePrototypeScope :: MonadSymtab m => m () #
enterFunctionScope :: MonadSymtab m => m () #
leaveFunctionScope :: MonadSymtab m => m () #
enterBlockScope :: MonadSymtab m => m () #
leaveBlockScope :: MonadSymtab m => m () #
Symbol table lookup (delegate)
lookupTypeDef :: (MonadCError m, MonadSymtab m) => Ident -> m Type #
lookup a type definition the 'wrong kind of object' is an internal error here, because the parser should distinguish typeDefs and other objects
lookupObject :: (MonadCError m, MonadSymtab m) => Ident -> m (Maybe IdentDecl) #
lookup an object, function or enumerator
Symbol table modification
createSUERef :: (MonadCError m, MonadSymtab m) => NodeInfo -> Maybe Ident -> m SUERef #
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 #
check wheter non-recoverable errors occurred
handleTravError :: MonadCError m => m a -> m (Maybe a) #
throwOnLeft :: (MonadCError m, Error e) => Either e a -> m a #
raise an error based on an Either argument
astError :: MonadCError m => NodeInfo -> String -> m a #
raise an error caused by a malformed AST
warn :: (Error e, MonadCError m) => e -> m () #
Trav - default MonadTrav implementation
simple traversal monad, providing user state and callbacks
initTravState :: s -> TravState s #
modifyUserState :: (s -> s) -> Trav s () #
getUserState :: Trav s s #
data TravOptions #
modifyOptions :: (TravOptions -> TravOptions) -> Trav s () #
travErrors :: TravState s -> [CError] #
Language options
The variety of the C language to accept. Note: this is not yet enforced.
Helpers
concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b] #