EmitContext.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. public ILGenerator ig;
  39. static object mlock = new object ();
  40. static int method_count;
  41. protected EmitContext (LambdaExpression lambda)
  42. {
  43. this.owner = lambda;
  44. param_types = owner.Parameters.Select (p => p.Type).ToArray ();
  45. }
  46. public static EmitContext Create (LambdaExpression lambda)
  47. {
  48. if (Environment.GetEnvironmentVariable ("LINQ_DBG") != null)
  49. return new DebugEmitContext (lambda);
  50. return new DynamicEmitContext (lambda);
  51. }
  52. public int GetParameterPosition (ParameterExpression p)
  53. {
  54. int position = owner.Parameters.IndexOf (p);
  55. if (position == -1)
  56. throw new InvalidOperationException ("Parameter not in scope");
  57. return position;
  58. }
  59. public abstract Delegate CreateDelegate ();
  60. public abstract void Emit ();
  61. protected static string GenerateName ()
  62. {
  63. lock (mlock) {
  64. return "lambda_method-" + (method_count++);
  65. }
  66. }
  67. }
  68. class DynamicEmitContext : EmitContext {
  69. DynamicMethod method;
  70. public DynamicEmitContext (LambdaExpression lambda)
  71. : base (lambda)
  72. {
  73. //
  74. // We probably want to use the 3.5 new API calls to associate
  75. // the method with the "sandboxed" Assembly, instead am currently
  76. // dumping these types in this class
  77. //
  78. Type owner_of_code = typeof (EmitContext);
  79. //
  80. // FIXME: Need to force this to be verifiable, see:
  81. // https://bugzilla.novell.com/show_bug.cgi?id=355005
  82. //
  83. method = new DynamicMethod (GenerateName (), lambda.Body.Type, param_types, owner_of_code);
  84. ig = method.GetILGenerator ();
  85. }
  86. public override Delegate CreateDelegate ()
  87. {
  88. return method.CreateDelegate (owner.Type);
  89. }
  90. public override void Emit ()
  91. {
  92. owner.Emit (this);
  93. }
  94. }
  95. class DebugEmitContext : EmitContext {
  96. AssemblyBuilder assembly;
  97. TypeBuilder type;
  98. MethodBuilder method;
  99. public DebugEmitContext (LambdaExpression lambda)
  100. : base (lambda)
  101. {
  102. var name = GenerateName ();
  103. var file_name = name + ".dll";
  104. assembly = AppDomain.CurrentDomain.DefineDynamicAssembly (
  105. new AssemblyName (file_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, owner.Body.Type, param_types);
  108. ig = method.GetILGenerator ();
  109. }
  110. public override Delegate CreateDelegate ()
  111. {
  112. return Delegate.CreateDelegate (owner.Type, GetMethod ());
  113. }
  114. MethodInfo GetMethod ()
  115. {
  116. return type.GetMethod (method.Name, BindingFlags.Static | BindingFlags.Public);
  117. }
  118. public override void Emit ()
  119. {
  120. owner.Emit (this);
  121. type.CreateType ();
  122. assembly.Save (assembly.GetName ().FullName);
  123. }
  124. }
  125. }