EmitContext.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 DynamicEmitContext (LambdaExpression lambda)
  73. : base (lambda)
  74. {
  75. //
  76. // We probably want to use the 3.5 new API calls to associate
  77. // the method with the "sandboxed" Assembly, instead am currently
  78. // dumping these types in this class
  79. //
  80. Type owner_of_code = typeof (EmitContext);
  81. //
  82. // FIXME: Need to force this to be verifiable, see:
  83. // https://bugzilla.novell.com/show_bug.cgi?id=355005
  84. //
  85. method = new DynamicMethod (GenerateName (), return_type, param_types, owner_of_code);
  86. ig = method.GetILGenerator ();
  87. }
  88. public override Delegate CreateDelegate ()
  89. {
  90. return method.CreateDelegate (owner.Type);
  91. }
  92. public override void Emit ()
  93. {
  94. owner.Emit (this);
  95. }
  96. }
  97. class DebugEmitContext : EmitContext {
  98. AssemblyBuilder assembly;
  99. string file_name;
  100. TypeBuilder type;
  101. MethodBuilder method;
  102. public DebugEmitContext (LambdaExpression lambda)
  103. : base (lambda)
  104. {
  105. var name = GenerateName ();
  106. file_name = name + ".dll";
  107. assembly = AppDomain.CurrentDomain.DefineDynamicAssembly (
  108. new AssemblyName (name), AssemblyBuilderAccess.RunAndSave, Path.GetTempPath ());
  109. type = assembly.DefineDynamicModule (file_name, file_name).DefineType ("Linq", TypeAttributes.Public);
  110. method = type.DefineMethod (name, MethodAttributes.Public | MethodAttributes.Static, return_type, param_types);
  111. ig = method.GetILGenerator ();
  112. }
  113. public override Delegate CreateDelegate ()
  114. {
  115. return Delegate.CreateDelegate (owner.Type, GetMethod ());
  116. }
  117. MethodInfo GetMethod ()
  118. {
  119. return type.GetMethod (method.Name, BindingFlags.Static | BindingFlags.Public);
  120. }
  121. public override void Emit ()
  122. {
  123. owner.Emit (this);
  124. type.CreateType ();
  125. assembly.Save (file_name);
  126. }
  127. }
  128. }