More working tests.
[castle.git] / MonoRail / Castle.MonoRail.Views.Brail / FixTryGetParameterConditionalChecks.cs
blob3f15bbecf2f872169cff7c000db3c33150d0c96a
1 // Copyright 2004-2008 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;
20 /// <summary>
21 /// We need to handle the following code:
22 /// if ?Error:
23 /// end
24 ///
25 /// However, because ?Op will return an IgnoreNull object, we can't just
26 /// use the default boo behavior, but need to fix it up
27 /// </summary>
28 public class FixTryGetParameterConditionalChecks : AbstractNamespaceSensitiveTransformerCompilerStep
30 public override void Run()
32 Visit(CompileUnit);
35 public override void OnBinaryExpression(BinaryExpression node)
37 if (node.Operator != BinaryOperatorType.Equality)
38 return;
40 if (IsTryGetParameterInvocation(node.Left) == false &&
41 IsTryGetParameterInvocation(node.Right) == false)
42 return;
44 MethodInvocationExpression mie = new MethodInvocationExpression();
45 ReferenceExpression expression = AstUtil.CreateReferenceExpression("Castle.MonoRail.Views.Brail.IgnoreNull.AreEqual");
46 mie.Target = expression;
47 mie.Arguments.Add(node.Left);
48 mie.Arguments.Add(node.Right);
50 ReplaceCurrentNode(mie);
54 public override void OnIfStatement(IfStatement node)
56 base.OnIfStatement(node);
57 node.Condition = FixCondition(node.Condition);
60 public override void OnUnlessStatement(UnlessStatement node)
62 base.OnUnlessStatement(node);
63 node.Condition = FixCondition(node.Condition);
66 private Expression FixCondition(Expression condition)
68 if (IsTryGetParameterInvocation(condition) == false)
69 return condition;
71 string name = ((ReferenceExpression) condition).Name.Substring(1);
72 condition = new MethodInvocationExpression(
73 new MemberReferenceExpression(new SuperLiteralExpression(), "TryGetParameter"),
74 new StringLiteralExpression(name)
77 MemberReferenceExpression isNull =
78 new MemberReferenceExpression(condition, "_IsIgnoreNullReferencingNotNullObject_");
80 return isNull;
83 private static bool IsTryGetParameterInvocation(Expression condition)
85 ReferenceExpression expression = condition as ReferenceExpression;
86 if (expression == null)
87 return false;
89 return expression.Name.StartsWith("?");