123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176(* Iain Proctor, Yoann Padioleau, Jiao Li
*
* Copyright (C) 2009-2010 Facebook
* 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.
*)openCommonopenAst_genericmoduleF=Controlflow(*****************************************************************************)(* Prelude *)(*****************************************************************************)(* Visitor of nodes and their expressions for dataflow purposes.
*
* This mostly factorize code having to deal with differences
* between lvalue and rvalue.
*
* alt:
* - alternate expression type where lvalue/rvalue are clearly separated?
*)(*****************************************************************************)(* Types *)(*****************************************************************************)(* left or right handside of assignment (lvalue or rvalue) *)typelhs_or_rhs=|Lhs|Rhs(* function to apply when folding over nodes and expressions when
* we encounter a variable. The 'a is usually a set, for example
* for reaching definitions a NodeiSet.t, in which the function
* will add or remove the nodei passed as a first parameter, depending
* whether we are in an lvalue or rvalue context.
*)type'afold_fn=F.nodei->Dataflow.var->lhs_or_rhs->'a->'a(*****************************************************************************)(* Helpers *)(*****************************************************************************)leterror_todoany=letv=Meta_ast.vof_anyanyinlets=Ocaml.string_of_vvinpr2s;failwith("Dataflow_visitor:error_todo ")(*****************************************************************************)(* Expression Visitor *)(*****************************************************************************)letrecexpr_foldnifnlhsexpracc=(* Used for known left hand value, e.g. reference *)letreclvl=expr_foldnifnLhsinletrecl=expr_foldnifnlhsinletrecr=expr_foldnifnRhsinmatchexprwith|Name((name,_tok),_idinfo)->(* calling the hook! *)fnninamelhsacc|Assign(e,e1)->reclvle(recre1acc)|AssignOp(e,_op,e1)->(* x += b <=> x = x + b hence the call also to 'recr e' *)reclvle(recre(recre1acc))(* otherwise regular recurse (could use a visitor) *)|L_|Nop->acc|IdSpecial_->acc(* todo: Special cases for function that are known to take implicit
* lvalue, e.g., sscanf?
*)(* Todo: false positive because passsing by reference *)|Call(e,args)->recre(List.fold_left(funacc'->function|Arge1->recre1acc'|ArgKwd_|ArgType_|ArgOther_->error_todo(Eexpr))accargs)|ObjAccess(e,_id)->recleacc|ArrayAccess(e,e1)->recle(recre1acc)|Conditional(e,e1,e2)->recle(recle2(recle1acc))|Cast(_,e)->recreacc|Lambda_|AnonClass_->acc|Yielde|Awaite->recreacc|(Container(_,_)|Tuple_|Record_|Constructor(_,_)|Xml_|LetPattern(_,_)|MatchPattern(_,_)|Seq_|Ref_|DeRef_|Ellipses_|OtherExpr(_,_))->error_todo(Eexpr)(*****************************************************************************)(* Node Visitor *)(*****************************************************************************)let(node_fold:F.nodei->'afold_fn->Controlflow.node_kind->'a->'a)=funnifnnodeacc->matchnodewith(* 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.SimpleStmt(F.TodoSimpleStmt)|F.ContinueNone|F.BreakNone->acc(* expr *)|F.IfHeaderexpr|F.WhileHeaderexpr|F.DoWhileTailexpr|F.SwitchHeaderexpr|F.Throwexpr|F.SimpleStmt(F.ExprStmt(expr))|F.Return(expr)|F.Continue(Someexpr)|F.Break(Someexpr)->expr_foldnifnRhsexpracc|F.Parameter(_)->(* env.fold_fn name true acc *)raiseTodo|F.ForeachHeader(*var_list *)->raiseTodo(*
List.fold_left (fun acc' var ->
match var with
| (Some _, _) -> raise Todo
| (None, IdVar(DName(s, _), _)) ->
env.fold_fn s true acc'
| _ -> acc') acc var_list
*)(*****************************************************************************)(* Main entry points *)(*****************************************************************************)let(flow_fold:'afold_fn->'a->F.flow->'a)=funfold_fnaccflow->flow#nodes#fold(funacc'(ni,nd)->node_foldnifold_fnnd.F.nacc')accletflow_fold_lvfold_fnaccflow=flow_fold(funndivarlhsacc'->iflhs=Lhsthenfold_fnndivaracc'elseacc')accflowletflow_fold_rvfold_fnaccflow=flow_fold(funndivarlhsacc'->iflhs=Rhsthenfold_fnndivaracc'elseacc')accflow