5 // Miguel de Icaza (miguel@novell.com)
6 // Jb Evain (jbevain@novell.com)
8 // (C) 2008 Novell, Inc. (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
33 using System.Reflection;
34 using System.Reflection.Emit;
36 namespace System.Linq.Expressions {
38 abstract class EmitContext {
40 protected LambdaExpression owner;
41 protected Type [] param_types;
43 public ILGenerator ig;
45 static object mlock = new object ();
46 static int method_count;
48 protected EmitContext (LambdaExpression lambda)
52 param_types = owner.Parameters.Select (p => p.Type).ToArray ();
55 public static EmitContext Create (LambdaExpression lambda)
57 if (Environment.GetEnvironmentVariable ("LINQ_DBG") != null)
58 return new DebugEmitContext (lambda);
60 return new DynamicEmitContext (lambda);
63 public int GetParameterPosition (ParameterExpression p)
65 int position = owner.Parameters.IndexOf (p);
67 throw new InvalidOperationException ("Parameter not in scope");
72 public abstract Delegate CreateDelegate ();
74 public abstract void Emit ();
76 protected static string GenerateName ()
79 return "lambda_method-" + (method_count++);
84 class DynamicEmitContext : EmitContext {
88 public DynamicEmitContext (LambdaExpression lambda)
92 // We probably want to use the 3.5 new API calls to associate
93 // the method with the "sandboxed" Assembly, instead am currently
94 // dumping these types in this class
96 Type owner_of_code = typeof (EmitContext);
99 // FIXME: Need to force this to be verifiable, see:
100 // https://bugzilla.novell.com/show_bug.cgi?id=355005
102 method = new DynamicMethod (GenerateName (), lambda.Body.Type, param_types, owner_of_code);
103 ig = method.GetILGenerator ();
106 public override Delegate CreateDelegate ()
108 return method.CreateDelegate (owner.Type);
111 public override void Emit ()
117 class DebugEmitContext : EmitContext {
119 AssemblyBuilder assembly;
121 MethodBuilder method;
123 public DebugEmitContext (LambdaExpression lambda)
126 var name = GenerateName ();
127 var file_name = name + ".dll";
129 assembly = AppDomain.CurrentDomain.DefineDynamicAssembly (
130 new AssemblyName (file_name), AssemblyBuilderAccess.RunAndSave, Path.GetTempPath ());
132 type = assembly.DefineDynamicModule (file_name, file_name).DefineType ("Linq", TypeAttributes.Public);
134 method = type.DefineMethod (name, MethodAttributes.Public | MethodAttributes.Static, owner.Body.Type, param_types);
135 ig = method.GetILGenerator ();
138 public override Delegate CreateDelegate ()
140 return Delegate.CreateDelegate (owner.Type, GetMethod ());
143 MethodInfo GetMethod ()
145 return type.GetMethod (method.Name, BindingFlags.Static | BindingFlags.Public);
148 public override void Emit ()
153 assembly.Save (assembly.GetName ().FullName);