1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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;
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");
51 Block block
= (Block
) stmt
.ParentNode
;
53 while(block
.Statements
[index
] != stmt
)
58 MethodInvocationExpression invocation
= mie
.CloneNode();
59 invocation
.Arguments
.Add(stmt
.Expression
);
61 stmt
.Expression
= invocation
;