123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118(* Yoann Padioleau
*
* Copyright (C) 2019 r2c
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation, with the
* special exception on linking described in file license.txt.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file
* license.txt for more details.
*)moduleAst=Ast_genericopenControlflowmoduleF=Controlflow(*****************************************************************************)(* Prelude *)(*****************************************************************************)(* A smaller wrapper around the AST generic visitor to also handles
* control flow nodes.
*
* Less useful now that we have Controlflow.exprs_of_node and
* Controlflow.fold_on_node_and_expr.
*)(*****************************************************************************)(* Types *)(*****************************************************************************)typevisitor_in=Visitor_ast.visitor_intypevisitor_out=Controlflow.node->unit(*****************************************************************************)(* Entry Points *)(*****************************************************************************)letmk_visitorvin=letvisitor=Visitor_ast.mk_visitorvininfunnode->matchnode.F.nwith(* Nothing is needed if the node has no expr information*)|F.Enter|F.Exit|F.TrueNode|F.FalseNode|F.DoHeader|F.ForHeader|F.SwitchEnd|F.Case|F.Default|F.TryHeader|F.CatchStart|F.Catch|F.TryEnd|F.Join|F.ContinueNone|F.BreakNone->()(* expr *)|F.IfHeaderexpr|F.WhileHeaderexpr|F.DoWhileTailexpr|F.SwitchHeaderexpr|F.Throwexpr|F.Return(expr)|F.Continue(Someexpr)|F.Break(Someexpr)->visitor(Ast.Eexpr)|F.ForeachHeader(pat,e)->visitor(Ast.E(Ast.LetPattern(pat,e)))|F.OtherStmtWithStmtHeader(_op,e)->visitor(Ast.Ee)|F.SimpleNodex->letany=F.any_of_simple_nodexinvisitorany(*****************************************************************************)(* Alternative visitor *)(*****************************************************************************)letexprs_of_nodenode=matchnode.nwith|Enter|Exit|TrueNode|FalseNode|DoHeader|ForHeader|SwitchEnd|Case|Default|TryHeader|CatchStart|Catch|TryEnd|Join|ContinueNone|BreakNone->[](* expr *)|IfHeaderexpr|WhileHeaderexpr|DoWhileTailexpr|SwitchHeaderexpr|Throwexpr|Return(expr)|Continue(Someexpr)|Break(Someexpr)->[expr]|ForeachHeader(pat,expr)->[Ast.LetPattern(pat,expr)]|OtherStmtWithStmtHeader(_op,e)->[e]|SimpleNodex->(matchxwith|ExprStmte->[e]|Assert(e,eopt)->e::Common.opt_to_listeopt(* TODO: should transform VarDef in it in Assign *)|DefStmt_->[]|DirectiveStmt_->[](* TODO: should use visitor! *)|OtherStmt_->[](* TODO: should transform in Assign *)|Parameter_p->[])(* this can also be used as an iter; just pass () to acc *)letfold_on_node_and_exprhook(flow:flow)acc=flow#nodes#fold(funacc(ni,node)->letxs=exprs_of_nodenodeinxs|>List.fold_left(funacce->hook(ni,node)eacc)acc)acc