Minor style changes
[castle.git] / MonoRail / Castle.MonoRail.Views.Brail / ReturnValueVisitor.cs
blobf5e43ac0b4debb360b97e376319eb6c3b6511d61
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Views.Brail
17 using Boo.Lang.Compiler.Ast;
18 using Boo.Lang.Compiler.Steps;
19 using Castle.MonoRail.Framework;
21 //This will search for all return statements in a method
22 //and will replace them with a method call (only a marker)
23 //called transform, later, the output attribute will replace it
24 //with the correct method call.
25 public class ReturnValueVisitor : DepthFirstVisitor
27 private NormalizeStatementModifiers normalizer;
28 private MethodInvocationExpression mie;
29 private bool found = false;
31 public bool Found
33 get { return found; }
36 public ReturnValueVisitor()
38 normalizer = new NormalizeStatementModifiers();
39 mie = new MethodInvocationExpression();
40 mie.Target = AstUtil.CreateReferenceExpression("transform");
43 public override void OnReturnStatement(ReturnStatement stmt)
45 // First normalize the statement
46 normalizer.Visit(stmt);
47 //empty return, so error
48 if (stmt.Expression == null)
49 throw new RailsException("An empty return statement on a method with output attribute");
50 found = true;
51 Block block = (Block) stmt.ParentNode;
52 int index = 0;
53 while(block.Statements[index] != stmt)
55 index ++;
58 MethodInvocationExpression invocation = mie.CloneNode();
59 invocation.Arguments.Add(stmt.Expression);
61 stmt.Expression = invocation;