Fixing a bug with the new FixTryGetParameterConditionalChecks - which would always...
[castle.git] / MonoRail / Castle.MonoRail.Views.Brail / FixTryGetParameterConditionalChecks.cs
bloba0097aebe2311c8bf0e7db2d520765c8109dc6b1
1 namespace Castle.MonoRail.Views.Brail
3 using Boo.Lang.Compiler.Ast;
4 using Boo.Lang.Compiler.Steps;
6 /// <summary>
7 /// We need to handle the following code:
8 /// if ?Error:
9 /// end
10 ///
11 /// However, because ?Op will return an IgnoreNull object, we can't just
12 /// use the default boo behavior, but need to fix it up
13 /// </summary>
14 public class FixTryGetParameterConditionalChecks : AbstractNamespaceSensitiveTransformerCompilerStep
16 public override void Run()
18 Visit(CompileUnit);
21 public override void OnBinaryExpression(BinaryExpression node)
23 if (node.Operator != BinaryOperatorType.Equality)
24 return;
26 if (IsTryGetParameterInvocation(node.Left) == false &&
27 IsTryGetParameterInvocation(node.Right) == false)
28 return;
30 MethodInvocationExpression mie = new MethodInvocationExpression();
31 ReferenceExpression expression = AstUtil.CreateReferenceExpression("Castle.MonoRail.Views.Brail.IgnoreNull.AreEqual");
32 mie.Target = expression;
33 mie.Arguments.Add(node.Left);
34 mie.Arguments.Add(node.Right);
36 ReplaceCurrentNode(mie);
40 public override void OnIfStatement(IfStatement node)
42 base.OnIfStatement(node);
43 node.Condition = FixCondition(node.Condition);
46 public override void OnUnlessStatement(UnlessStatement node)
48 base.OnUnlessStatement(node);
49 node.Condition = FixCondition(node.Condition);
52 private Expression FixCondition(Expression condition)
54 if (IsTryGetParameterInvocation(condition) == false)
55 return condition;
57 string name = ((ReferenceExpression) condition).Name.Substring(1);
58 condition = new MethodInvocationExpression(
59 new MemberReferenceExpression(new SuperLiteralExpression(), "TryGetParameter"),
60 new StringLiteralExpression(name)
63 MemberReferenceExpression isNull =
64 new MemberReferenceExpression(condition, "_IsIgnoreNullReferencingNotNullObject_");
66 return isNull;
69 private static bool IsTryGetParameterInvocation(Expression condition)
71 ReferenceExpression expression = condition as ReferenceExpression;
72 if (expression == null)
73 return false;
75 return expression.Name.StartsWith("?");