1 // Copyright 2004-2008 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 Boo
.Lang
.Compiler
.TypeSystem
;
22 /// Replace any uknown identifier with a call to GetParameter('unknown')
23 /// this mean that unknonw identifier in scripts will only fail in run time if they
24 /// were not defined by the controller.
26 public class ReplaceUknownWithParameters
: ProcessMethodBodiesWithDuckTyping
28 private IMethod getParam
;
29 private IMethod tryGetParam
;
31 public override void OnReferenceExpression(ReferenceExpression node
)
33 IEntity entity
= NameResolutionService
.Resolve(node
.Name
);
36 base.OnReferenceExpression(node
);
40 MethodInvocationExpression mie
= CodeBuilder
.CreateMethodInvocation(
41 CodeBuilder
.CreateSelfReference(_currentMethod
.DeclaringType
),
42 GetMethod(node
.Name
));
43 mie
.Arguments
.Add(GetNameLiteral(node
.Name
));
44 node
.ParentNode
.Replace(node
, mie
);
48 protected override void InitializeMemberCache()
50 base.InitializeMemberCache();
51 getParam
= TypeSystemServices
.Map(typeof(BrailBase
).GetMethod("GetParameter"));
52 tryGetParam
= TypeSystemServices
.Map(typeof(BrailBase
).GetMethod("TryGetParameter"));
55 public IMethod
GetMethod(string name
)
63 public StringLiteralExpression
GetNameLiteral(string name
)
66 return CodeBuilder
.CreateStringLiteral(name
.Substring(1));
68 return CodeBuilder
.CreateStringLiteral(name
);