public static BoundProgram Perform(BoundProgram program, IList<BoundExpression> resultExpressions) { // If the last statement of the program is a return, we don't // have to rewrite the program. var body = program.Body; if ( body.Body.Nodes.Count > 0 && body.Body.Nodes[body.Body.Nodes.Count - 1] is BoundReturn ) return program; // If we don't have any result expressions, we only have to // insert a return statement. if (resultExpressions == null || resultExpressions.Count == 0) { // Create a new nodes list with the added return statement. var nodes = new ReadOnlyArray<BoundStatement>.Builder(); nodes.AddRange(body.Body.Nodes); nodes.Add(new BoundReturn( null, SourceLocation.Missing )); // Return the updated program. return program.Update( body.Update( body.Body.Update( body.Body.Temporaries, nodes.ToReadOnly(), body.Body.Location ), body.Closure, body.ScopedClosure, body.Arguments, body.Locals, body.MappedArguments, body.Flags, body.TypeManager ) ); } // Otherwise, we need to do a full rewrite. return program.Update( (BoundBody)new Rewriter(resultExpressions, body.TypeManager).Visit(program.Body) ); }
public static BoundProgram Perform(BoundProgram node) { return node.Update(Perform(node.Body)); }