merge r98600
[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.Collections.Generic;
32 using System.IO;
33 using System.Linq;
34 using System.Reflection;
35 using System.Reflection.Emit;
36
37 namespace System.Linq.Expressions {
38
39         abstract class EmitContext {
40
41                 protected LambdaExpression owner;
42                 protected Type [] param_types;
43                 protected Type return_type;
44
45                 public ILGenerator ig;
46
47                 protected EmitContext (LambdaExpression lambda)
48                 {
49                         this.owner = lambda;
50
51                         param_types = owner.Parameters.Select (p => p.Type).ToArray ();
52                         return_type = owner.GetReturnType ();
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 void Emit (Expression expression)
75                 {
76                         expression.Emit (this);
77                 }
78
79                 public LocalBuilder EmitStored (Expression expression)
80                 {
81                         var local = ig.DeclareLocal (expression.Type);
82                         expression.Emit (this);
83                         ig.Emit (OpCodes.Stloc, local);
84
85                         return local;
86                 }
87
88                 public void EmitLoad (Expression expression)
89                 {
90                         if (expression.Type.IsValueType) {
91                                 var local = EmitStored (expression);
92                                 ig.Emit (OpCodes.Ldloca, local);
93                         } else
94                                 expression.Emit (this);
95                 }
96
97                 public void EmitLoad (LocalBuilder local)
98                 {
99                         ig.Emit (OpCodes.Ldloc, local);
100                 }
101
102                 public void EmitCall (LocalBuilder local, IEnumerable<Expression> arguments, MethodInfo method)
103                 {
104                         EmitLoad (local);
105                         EmitCollection (arguments);
106                         EmitCall (method);
107                 }
108
109                 public void EmitCall (Expression expression, IEnumerable<Expression> arguments, MethodInfo method)
110                 {
111                         if (expression != null)
112                                 EmitLoad (expression);
113
114                         EmitCollection (arguments);
115                         EmitCall (method);
116                 }
117
118                 public void EmitCall (MethodInfo method)
119                 {
120                         ig.Emit (
121                                 method.IsVirtual ? OpCodes.Callvirt : OpCodes.Call,
122                                 method);
123                 }
124
125                 public void EmitCollection<T> (IEnumerable<T> collection) where T : Expression
126                 {
127                         foreach (var expression in collection)
128                                 expression.Emit (this);
129                 }
130
131                 public void EmitCollection (IEnumerable<ElementInit> initializers, LocalBuilder local)
132                 {
133                         foreach (var initializer in initializers)
134                                 initializer.Emit (this, local);
135                 }
136
137                 public void EmitCollection (IEnumerable<MemberBinding> bindings, LocalBuilder local)
138                 {
139                         foreach (var binding in bindings)
140                                 binding.Emit (this, local);
141                 }
142
143                 public void EmitIsInst (Expression expression, Type candidate)
144                 {
145                         expression.Emit (this);
146
147                         if (expression.Type.IsValueType)
148                                 ig.Emit (OpCodes.Box, expression.Type);
149
150                         ig.Emit (OpCodes.Isinst, candidate);
151                 }
152         }
153
154         class DynamicEmitContext : EmitContext {
155
156                 DynamicMethod method;
157
158                 static object mlock = new object ();
159                 static int method_count;
160
161                 public DynamicMethod Method {
162                         get { return method; }
163                 }
164
165                 public DynamicEmitContext (LambdaExpression lambda)
166                         : base (lambda)
167                 {
168                         // FIXME: Need to force this to be verifiable, see:
169                         // https://bugzilla.novell.com/show_bug.cgi?id=355005
170                         method = new DynamicMethod (GenerateName (), return_type, param_types, typeof (EmitContext), true);
171                         ig = method.GetILGenerator ();
172
173                         owner.Emit (this);
174                 }
175
176                 public override Delegate CreateDelegate ()
177                 {
178                         return method.CreateDelegate (owner.Type);
179                 }
180
181                 static string GenerateName ()
182                 {
183                         lock (mlock) {
184                                 return "lambda_method-" + (method_count++);
185                         }
186                 }
187         }
188
189         class DebugEmitContext : EmitContext {
190
191                 DynamicEmitContext dynamic_context;
192
193                 public DebugEmitContext (LambdaExpression lambda)
194                         : base (lambda)
195                 {
196                         dynamic_context = new DynamicEmitContext (lambda);
197
198                         var name = dynamic_context.Method.Name;
199                         var file_name = name + ".dll";
200
201                         var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly (
202                                 new AssemblyName (name), AssemblyBuilderAccess.RunAndSave, Path.GetTempPath ());
203
204                         var type = assembly.DefineDynamicModule (file_name, file_name).DefineType ("Linq", TypeAttributes.Public);
205
206                         var method = type.DefineMethod (name, MethodAttributes.Public | MethodAttributes.Static, return_type, param_types);
207                         ig = method.GetILGenerator ();
208
209                         owner.Emit (this);
210
211                         type.CreateType ();
212                         assembly.Save (file_name);
213                 }
214
215                 public override Delegate CreateDelegate ()
216                 {
217                         return dynamic_context.CreateDelegate ();
218                 }
219         }
220 }