import System import Mono.Cecil import Cecil.FlowAnalysis import Cecil.FlowAnalysis.ActionFlow import Cecil.FlowAnalysis.CodeStructure class Address: [property(State)] _state as string class Customer: [property(Address)] _address as Address class Predicate: def Match(c as Customer): return c.Address.State == "SP" def GetMatchMethod(): asm = AssemblyFactory.GetAssembly(typeof(Predicate).Module.FullyQualifiedName) return asm.MainModule.Types["Predicate"].Methods.GetMethod("Match")[0] cfg = FlowGraphFactory.CreateControlFlowGraph(GetMatchMethod()) // a cfg is at the IL instruction level it is good for // low level IL optimization and simple analysis afg = FlowGraphFactory.CreateActionFlowGraph(cfg) // an afg is at the statement/expression level // good for code analysis // print the interesting blocks for block in afg.Blocks: expression as IExpression = null if block isa IReturnActionBlock: expression = (block as IReturnActionBlock).Expression Console.Write("return ") elif block isa IAssignActionBlock: expression = (block as IAssignActionBlock).AssignExpression continue if expression is null // expression is an ast like expression tree // which supports visitors // it is more like a bound ast (with all references // pointing to the right Mono.Cecil entities) // // ExpressionPrinter is a handy visitor print ExpressionPrinter.ToString(expression)
Rodrigo B. de Oliveira
|




