Fixing IOC-60, Custom Parameters do not play a role in resolving chains
[castle.git] / MonoRail / Castle.MonoRail.Views.Brail / ReplaceUknownWithParameters.cs
blob6a6aa1016c83029e53810d7302cf8b8495a8f537
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 Boo.Lang.Compiler.TypeSystem;
20 // Replace any uknown identifier with a call to GetParameter('unknown')
21 // this mean that unknonw identifier in scripts will only fail in run time if they
22 // were not defined by the controller.
23 public class ReplaceUknownWithParameters : ProcessMethodBodiesWithDuckTyping
25 private IMethod _getParam;
26 private IMethod _tryGetParam;
28 public override void OnReferenceExpression(ReferenceExpression node)
30 IEntity entity = NameResolutionService.Resolve(node.Name);
31 if (entity != null)
33 base.OnReferenceExpression(node);
35 else
37 MethodInvocationExpression mie = CodeBuilder.CreateMethodInvocation(
38 CodeBuilder.CreateSelfReference(_currentMethod.DeclaringType),
39 GetMethod(node.Name));
40 mie.Arguments.Add(GetNameLiteral(node.Name));
41 node.ParentNode.Replace(node, mie);
45 protected override void InitializeMemberCache()
47 base.InitializeMemberCache();
48 _getParam = TypeSystemServices.Map(typeof(BrailBase).GetMethod("GetParameter"));
49 _tryGetParam = TypeSystemServices.Map(typeof(BrailBase).GetMethod("TryGetParameter"));
52 public IMethod GetMethod(string name)
54 if (name[0] == '?')
55 return _tryGetParam;
56 else
57 return _getParam;
60 public StringLiteralExpression GetNameLiteral(string name)
62 if (name[0] == '?')
63 return CodeBuilder.CreateStringLiteral(name.Substring(1));
64 else
65 return CodeBuilder.CreateStringLiteral(name);