package mopsa

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

Module Ast.Visitor

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 : Core.Ast.Expr.expr list;
    (*

    sub-expressions

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

    sub-statements

    *)
}

Parts of a statement/expression

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

builder function

Get the structure of an expression

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.Core.TypeExt.compare;
    (*

    Comparison function for 'a

    *)
  2. print : 'a Mopsa_utils.Core.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 : Core.Ast.Expr.expr visit_info -> unit

Register an expression with its visitor

Register a visitor of an expression

val register_stmt_with_visitor : Core.Ast.Stmt.stmt visit_info -> unit

Register a statement with its visitor

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

val fold_expr : ('a -> Core.Ast.Expr.expr -> 'a visit_action) -> ('a -> Core.Ast.Stmt.stmt -> 'a visit_action) -> 'a -> Core.Ast.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 -> Core.Ast.Expr.expr -> 'a visit_action) -> ('a -> Core.Ast.Stmt.stmt -> 'a visit_action) -> 'a -> Core.Ast.Stmt.stmt -> 'a

Similar to fold_expr but on statements

Combination of map and fold for expressions

Combination of map and fold for statements

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

Utility functions

val is_leaf_expr : Core.Ast.Expr.expr -> bool

Test whether an expression is a leaf expression

val is_atomic_expr : Core.Ast.Expr.expr -> bool

Test whether an expression has no sub-statement

val is_atomic_stmt : Core.Ast.Stmt.stmt -> bool

Test whether a statement has no sub-statement

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

Get all variables present in an expression

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

Get all variables present in a statement

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

Check whether a variable appears in an expression

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

Check whether a variable appears in a statement

Deprecated

val fold_sub_expr : ('a -> Core.Ast.Expr.expr -> 'a visit_action) -> ('a -> Core.Ast.Stmt.stmt -> 'a visit_action) -> 'a -> Core.Ast.Expr.expr -> 'a