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 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
);
33 base.OnReferenceExpression(node
);
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
)
60 public StringLiteralExpression
GetNameLiteral(string name
)
63 return CodeBuilder
.CreateStringLiteral(name
.Substring(1));
65 return CodeBuilder
.CreateStringLiteral(name
);