/src/Castle.Core/DynamicProxy/Generators/GeneratorUtil.cs

http://github.com/castleproject/Castle.Core-READONLY · C# · 70 lines · 47 code · 10 blank · 13 comment · 3 complexity · c5d2a81acb63056d04f576d1cbd1e296 MD5 · raw file

  1. // Copyright 2004-2011 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.
  14. namespace Castle.DynamicProxy.Generators
  15. {
  16. using System.Reflection;
  17. using Castle.DynamicProxy.Generators.Emitters;
  18. using Castle.DynamicProxy.Generators.Emitters.SimpleAST;
  19. using Castle.DynamicProxy.Tokens;
  20. public static class GeneratorUtil
  21. {
  22. public static void CopyOutAndRefParameters(TypeReference[] dereferencedArguments, LocalReference invocation,
  23. MethodInfo method, MethodEmitter emitter)
  24. {
  25. var parameters = method.GetParameters();
  26. if (!ArgumentsUtil.IsAnyByRef(parameters))
  27. {
  28. return; //saving the need to create locals if there is no need
  29. }
  30. var arguments = StoreInvocationArgumentsInLocal(emitter, invocation);
  31. for (var i = 0; i < parameters.Length; i++)
  32. {
  33. if (!parameters[i].ParameterType.IsByRef)
  34. {
  35. continue;
  36. }
  37. emitter.CodeBuilder.AddStatement(AssignArgument(dereferencedArguments, i, arguments));
  38. }
  39. }
  40. private static ConvertExpression Argument(int i, LocalReference invocationArgs, TypeReference[] arguments)
  41. {
  42. return new ConvertExpression(arguments[i].Type, new LoadRefArrayElementExpression(i, invocationArgs));
  43. }
  44. private static AssignStatement AssignArgument(TypeReference[] dereferencedArguments, int i,
  45. LocalReference invocationArgs)
  46. {
  47. return new AssignStatement(dereferencedArguments[i], Argument(i, invocationArgs, dereferencedArguments));
  48. }
  49. private static AssignStatement GetArguments(LocalReference invocationArgs, LocalReference invocation)
  50. {
  51. return new AssignStatement(invocationArgs, new MethodInvocationExpression(invocation, InvocationMethods.GetArguments));
  52. }
  53. private static LocalReference StoreInvocationArgumentsInLocal(MethodEmitter emitter, LocalReference invocation)
  54. {
  55. var invocationArgs = emitter.CodeBuilder.DeclareLocal(typeof(object[]));
  56. emitter.CodeBuilder.AddStatement(GetArguments(invocationArgs, invocation));
  57. return invocationArgs;
  58. }
  59. }
  60. }