package mopsa

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

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 and e for the statement S_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)

type parts = {
  1. exprs : Expr.expr list;
    (*

    sub-expressions

    *)
  2. stmts : Stmt.stmt list;
    (*

    sub-statements

    *)
}

Parts of a statement/expression

type 'a structure = parts * (parts -> 'a)

builder function

val structure_of_expr : Expr.expr -> Expr.expr structure

Get the structure of an expression

val structure_of_stmt : Stmt.stmt -> Stmt.stmt structure

Get the structure of a statement

val leaf : 'a -> 'a structure

Visitor for leaf statements/expressions that have no sub-elements

Registration

type 'a visit_info = {
  1. compare : 'a Mopsa_utils.TypeExt.compare;
    (*

    Comparison function for 'a

    *)
  2. print : 'a Mopsa_utils.TypeExt.print;
    (*

    Pretty-printer for 'a'

    *)
  3. visit : ('a -> 'a structure) -> 'a -> 'a structure;
    (*

    Visitor for 'a

    *)
}

Registration descriptor for visitors

val register_expr_with_visitor : Expr.expr visit_info -> unit

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

val register_stmt_with_visitor : Stmt.stmt visit_info -> unit

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

type 'a visit_action =
  1. | Keep of 'a
    (*

    Keep the given result

    *)
  2. | VisitParts of 'a
    (*

    Continue visiting the parts of the given result

    *)
  3. | Visit of 'a
    (*

    Iterate the visitor on the given result

    *)

Actions of a visiting iterator

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

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

val exists_expr : (Expr.expr -> bool) -> (Stmt.stmt -> bool) -> Expr.expr -> bool
val for_all_expr : (Expr.expr -> bool) -> (Stmt.stmt -> bool) -> Expr.expr -> bool
val exists_stmt : (Expr.expr -> bool) -> (Stmt.stmt -> bool) -> Stmt.stmt -> bool
val for_all_stmt : (Expr.expr -> bool) -> (Stmt.stmt -> bool) -> Stmt.stmt -> bool
val exists_child_expr : (Expr.expr -> bool) -> (Stmt.stmt -> bool) -> Expr.expr -> bool
val for_all_child_expr : (Expr.expr -> bool) -> (Stmt.stmt -> bool) -> Expr.expr -> bool
val exists_child_stmt : (Expr.expr -> bool) -> (Stmt.stmt -> bool) -> Stmt.stmt -> bool
val for_all_child_stmt : (Expr.expr -> bool) -> (Stmt.stmt -> bool) -> Stmt.stmt -> bool

Utility functions

val is_leaf_expr : Expr.expr -> bool

Test whether an expression is a leaf expression

val is_atomic_expr : Expr.expr -> bool

Test whether an expression has no sub-statement

val is_atomic_stmt : Stmt.stmt -> bool

Test whether a statement has no sub-statement

val expr_vars : Expr.expr -> Var.var list

Get all variables present in an expression

val stmt_vars : Stmt.stmt -> Var.var list

Get all variables present in a statement

val is_var_in_expr : Var.var -> Expr.expr -> bool

Check whether a variable appears in an expression

val is_var_in_stmt : Var.var -> Stmt.stmt -> bool

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
OCaml

Innovation. Community. Security.