EmitContext.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // EmitContext.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. // Jb Evain ([email protected])
  7. //
  8. // (C) 2008 Novell, Inc. (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.IO;
  31. using System.Linq;
  32. using System.Reflection;
  33. using System.Reflection.Emit;
  34. namespace System.Linq.Expressions {
  35. abstract class EmitContext {
  36. protected LambdaExpression owner;
  37. protected Type [] param_types;
  38. protected Type return_type;
  39. public ILGenerator ig;
  40. static object mlock = new object ();
  41. static int method_count;
  42. protected EmitContext (LambdaExpression lambda)
  43. {
  44. this.owner = lambda;
  45. param_types = owner.Parameters.Select (p => p.Type).ToArray ();
  46. return_type = owner.GetReturnType ();
  47. }
  48. public static EmitContext Create (LambdaExpression lambda)
  49. {
  50. if (Environment.GetEnvironmentVariable ("LINQ_DBG") != null)
  51. return new DebugEmitContext (lambda);
  52. return new DynamicEmitContext (lambda);
  53. }
  54. public int GetParameterPosition (ParameterExpression p)
  55. {
  56. int position = owner.Parameters.IndexOf (p);
  57. if (position == -1)
  58. throw new InvalidOperationException ("Parameter not in scope");
  59. return position;
  60. }
  61. public abstract Delegate CreateDelegate ();
  62. public abstract void Emit ();
  63. protected static string GenerateName ()
  64. {
  65. lock (mlock) {
  66. return "lambda_method-" + (method_count++);
  67. }
  68. }
  69. }
  70. class DynamicEmitContext : EmitContext {
  71. DynamicMethod method;
  72. public DynamicMethod Method {
  73. get { return method; }
  74. }
  75. public DynamicEmitContext (LambdaExpression lambda)
  76. : base (lambda)
  77. {
  78. // FIXME: Need to force this to be verifiable, see:
  79. // https://bugzilla.novell.com/show_bug.cgi?id=355005
  80. method = new DynamicMethod (GenerateName (), return_type, param_types, typeof (EmitContext), true);
  81. ig = method.GetILGenerator ();
  82. }
  83. public override Delegate CreateDelegate ()
  84. {
  85. return method.CreateDelegate (owner.Type);
  86. }
  87. public override void Emit ()
  88. {
  89. owner.Emit (this);
  90. }
  91. }
  92. class DebugEmitContext : EmitContext {
  93. AssemblyBuilder assembly;
  94. string file_name;
  95. TypeBuilder type;
  96. MethodBuilder method;
  97. DynamicEmitContext dynamic_context;
  98. public DebugEmitContext (LambdaExpression lambda)
  99. : base (lambda)
  100. {
  101. dynamic_context = new DynamicEmitContext (lambda);
  102. var name = dynamic_context.Method.Name;
  103. file_name = name + ".dll";
  104. assembly = AppDomain.CurrentDomain.DefineDynamicAssembly (
  105. new AssemblyName (name), AssemblyBuilderAccess.RunAndSave, Path.GetTempPath ());
  106. type = assembly.DefineDynamicModule (file_name, file_name).DefineType ("Linq", TypeAttributes.Public);
  107. method = type.DefineMethod (name, MethodAttributes.Public | MethodAttributes.Static, return_type, param_types);
  108. ig = method.GetILGenerator ();
  109. }
  110. public override Delegate CreateDelegate ()
  111. {
  112. return dynamic_context.CreateDelegate ();
  113. }
  114. public override void Emit ()
  115. {
  116. dynamic_context.Emit ();
  117. owner.Emit (this);
  118. type.CreateType ();
  119. assembly.Save (file_name);
  120. }
  121. }
  122. }