package mopsa
Install
dune-project
Dependency
Authors
Maintainers
Sources
md5=9f673f79708b44a7effb3b6bb3618d2c
sha512=cb91cb428e43a22f1abbcb8219710d0c10a5b3756d0da392d4084b3b3a6157350776c596983e63def344f617d39964e91f244f60c07958695ee5c8c809a9f0f4
doc/ast/Ast/Visitor/index.html
Module Ast.VisitorSource
Visitors for statements and expressions
This module provides generic map, fold and fold_map functions for statements/expressions that visits their structure. To support newly added statements and expressions, visitors need to be registered with functions register_stmt_visitor and register_expr_visitor.
A visitor of a statement/expression encodes its structure, composed of two parts:
- The first part is the direct sub-node of the statement/expression, e.g.
xandefor the statementS_assign(x,e). - The second part is a builder function that reconstructs the statement given its sub-nodes.
Here is an example of registering the visitor of the assignment statement
let () =
register_stmt_visitor
(fun next s ->
match skind s with
| S_assign(x,e) ->
(* Sub-nodes *)
{ exprs = [x;e]; stmts = [] },
(* Builder *)
(function | {exprs = [x';e'-> s with skind = S_assign(x',e') | _ -> assert false) | _ -> next s)
Parts of a statement/expression
Visitor for leaf statements/expressions that have no sub-elements
Registration
type 'a visit_info = {compare : 'a Mopsa_utils.TypeExt.compare;(*Comparison function for
*)'aprint : 'a Mopsa_utils.TypeExt.print;(*Pretty-printer for
*)'a'visit : ('a -> 'a structure) -> 'a -> 'a structure;(*Visitor for
*)'a
}Registration descriptor for visitors
Register an expression with its visitor
val register_expr_visitor :
((Expr.expr -> Expr.expr structure) -> Expr.expr -> Expr.expr structure) ->
unitRegister a visitor of an expression
Register a statement with its visitor
val register_stmt_visitor :
((Stmt.stmt -> Stmt.stmt structure) -> Stmt.stmt -> Stmt.stmt structure) ->
unitRegister a visitor of a statement
Visiting iterators
Actions of a visiting iterator
val map_expr :
(Expr.expr -> Expr.expr visit_action) ->
(Stmt.stmt -> Stmt.stmt visit_action) ->
Expr.expr ->
Expr.exprmap_expr fe fs e transforms the expression e into a new one by applying visitor action fe and fs on its sub-expression and sub-statements respectively
val map_stmt :
(Expr.expr -> Expr.expr visit_action) ->
(Stmt.stmt -> Stmt.stmt visit_action) ->
Stmt.stmt ->
Stmt.stmtSimilar to map_expr but on statements
val fold_expr :
('a -> Expr.expr -> 'a visit_action) ->
('a -> Stmt.stmt -> 'a visit_action) ->
'a ->
Expr.expr ->
'afold_expr fe fs e folds the accumulated result of visitors fe and fs on the structure of expression e
val fold_stmt :
('a -> Expr.expr -> 'a visit_action) ->
('a -> Stmt.stmt -> 'a visit_action) ->
'a ->
Stmt.stmt ->
'aSimilar to fold_expr but on statements
val fold_map_expr :
('a -> Expr.expr -> ('a * Expr.expr) visit_action) ->
('a -> Stmt.stmt -> ('a * Stmt.stmt) visit_action) ->
'a ->
Expr.expr ->
'a * Expr.exprCombination of map and fold for expressions
val fold_map_stmt :
('a -> Expr.expr -> ('a * Expr.expr) visit_action) ->
('a -> Stmt.stmt -> ('a * Stmt.stmt) visit_action) ->
'a ->
Stmt.stmt ->
'a * Stmt.stmtCombination of map and fold for statements
Utility functions
Check whether a variable appears in an expression
Check whether a variable appears in a statement
Deprecated
val fold_sub_expr :
('a -> Expr.expr -> 'a visit_action) ->
('a -> Stmt.stmt -> 'a visit_action) ->
'a ->
Expr.expr ->
'a