generate dynamic method's names
[mono.git] / mcs / class / System.Core / System.Linq.Expressions / EmitContext.cs
1 //
2 // EmitContext.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@novell.com)
6 //   Jb Evain (jbevain@novell.com)
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
30 using System;
31 using System.IO;
32 using System.Linq;
33 using System.Reflection;
34 using System.Reflection.Emit;
35
36 namespace System.Linq.Expressions {
37
38         abstract class EmitContext {
39
40                 protected LambdaExpression owner;
41                 protected Type [] param_types;
42
43                 public ILGenerator ig;
44
45                 static object mlock = new object ();
46                 static int method_count;
47
48                 protected EmitContext (LambdaExpression lambda)
49                 {
50                         this.owner = lambda;
51
52                         param_types = owner.Parameters.Select (p => p.Type).ToArray ();
53                 }
54
55                 public static EmitContext Create (LambdaExpression lambda)
56                 {
57                         if (Environment.GetEnvironmentVariable ("LINQ_DBG") != null)
58                                 return new DebugEmitContext (lambda);
59
60                         return new DynamicEmitContext (lambda);
61                 }
62
63                 public int GetParameterPosition (ParameterExpression p)
64                 {
65                         int position = owner.Parameters.IndexOf (p);
66                         if (position == -1)
67                                 throw new InvalidOperationException ("Parameter not in scope");
68
69                         return position;
70                 }
71
72                 public abstract Delegate CreateDelegate ();
73
74                 public abstract void Emit ();
75
76                 protected static string GenerateName ()
77                 {
78                         lock (mlock) {
79                                 return "lambda_method-" + (method_count++);
80                         }
81                 }
82         }
83
84         class DynamicEmitContext : EmitContext {
85
86                 DynamicMethod method;
87
88                 public DynamicEmitContext (LambdaExpression lambda)
89                         : base (lambda)
90                 {
91                         //
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
95                         //
96                         Type owner_of_code = typeof (EmitContext);
97
98                         //
99                         // FIXME: Need to force this to be verifiable, see:
100                         // https://bugzilla.novell.com/show_bug.cgi?id=355005
101                         //
102                         method = new DynamicMethod (GenerateName (), lambda.Body.Type, param_types, owner_of_code);
103                         ig = method.GetILGenerator ();
104                 }
105
106                 public override Delegate CreateDelegate ()
107                 {
108                         return method.CreateDelegate (owner.Type);
109                 }
110
111                 public override void Emit ()
112                 {
113                         owner.Emit (this);
114                 }
115         }
116
117         class DebugEmitContext : EmitContext {
118
119                 AssemblyBuilder assembly;
120                 TypeBuilder type;
121                 MethodBuilder method;
122
123                 public DebugEmitContext (LambdaExpression lambda)
124                         : base (lambda)
125                 {
126                         var name = GenerateName ();
127                         var file_name = name + ".dll";
128
129                         assembly = AppDomain.CurrentDomain.DefineDynamicAssembly (
130                                 new AssemblyName (file_name), AssemblyBuilderAccess.RunAndSave, Path.GetTempPath ());
131
132                         type = assembly.DefineDynamicModule (file_name, file_name).DefineType ("Linq", TypeAttributes.Public);
133
134                         method = type.DefineMethod (name, MethodAttributes.Public | MethodAttributes.Static, owner.Body.Type, param_types);
135                         ig = method.GetILGenerator ();
136                 }
137
138                 public override Delegate CreateDelegate ()
139                 {
140                         return Delegate.CreateDelegate (owner.Type, GetMethod ());
141                 }
142
143                 MethodInfo GetMethod ()
144                 {
145                         return type.GetMethod (method.Name, BindingFlags.Static | BindingFlags.Public);
146                 }
147
148                 public override void Emit ()
149                 {
150                         owner.Emit (this);
151
152                         type.CreateType ();
153                         assembly.Save (assembly.GetName ().FullName);
154                 }
155         }
156 }