package mopsa
Install
Dune Dependency
Authors
Maintainers
Sources
md5=fdee20e988343751de440b4f6b67c0f4
sha512=f5cbf1328785d3f5ce40155dada2d95e5de5cce4f084ea30cfb04d1ab10cc9403a26cfb3fa55d0f9da72244482130fdb89c286a9aed0d640bba46b7c00e09500
doc/ast/Ast/Visitor/index.html
Module Ast.Visitor
Source
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.
x
ande
for 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
*)'a
print : '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) ->
unit
Register 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) ->
unit
Register 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.expr
map_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.stmt
Similar to map_expr
but on statements
val fold_expr :
('a -> Expr.expr -> 'a visit_action) ->
('a -> Stmt.stmt -> 'a visit_action) ->
'a ->
Expr.expr ->
'a
fold_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 ->
'a
Similar 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.expr
Combination 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.stmt
Combination 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