In corlib/System.Runtime.InteropServices:
[mono.git] / mcs / mcs / lambda.cs
1 //
2 // lambda.cs: support for lambda expressions
3 //
4 // Authors: Miguel de Icaza (miguel@gnu.org)
5 //
6 // Licensed under the terms of the GNU GPL
7 //
8 // (C) 2007 Novell, Inc
9 //
10
11 using System;
12 using System.Collections;
13 using System.Reflection;
14 using System.Reflection.Emit;
15
16 namespace Mono.CSharp {
17         public class LambdaExpression : AnonymousMethodExpression {
18                 bool explicit_parameters;
19
20                 //
21                 // The parameter list can either be:
22                 //    null: no parameters
23                 //    arraylist of Parameter (explicitly typed parameters)
24                 //    arraylist of strings (implicitly typed parameters)
25                 //
26                 public LambdaExpression (AnonymousMethodExpression parent,
27                                          GenericMethod generic, TypeContainer host,
28                                          Parameters parameters, Block container,
29                                          Location loc)
30                         : base (parent, generic, host, parameters, container, loc)
31                 {
32                         explicit_parameters = (parameters != null && parameters.Count > 0 && parameters [0].TypeName != null);
33                         if (parameters == null)
34                                 Parameters = new Parameters (new Parameter [0]);
35                 }
36
37                 public bool HasImplicitParameters {
38                         get {
39                                 return !explicit_parameters;
40                         }
41                 }
42
43                 public bool HasExplicitParameters {
44                         get {
45                                 return explicit_parameters;
46                         }
47                 }
48                 
49                 public override Expression DoResolve (EmitContext ec)
50                 {
51                         eclass = ExprClass.Value;
52                         type = TypeManager.anonymous_method_type;
53
54                         if (explicit_parameters){
55                                 if (!Parameters.Resolve (ec))
56                                         return null;
57                         }
58                         
59                         // We will resolve parameters later, we do not
60                         // have information at this point.
61                         
62                         return this;
63                 }
64
65                 public override void Emit (EmitContext ec)
66                 {
67                         base.Emit (ec);
68                 }
69
70                 public override bool ImplicitStandardConversionExists (Type delegate_type)
71                 {
72                         EmitContext ec = EmitContext.TempEc;
73
74                         bool result;
75
76                         try {
77                                 Report.DisableErrors ();
78                                 result = DoCompatibleTest (ec, delegate_type, true) != null;
79                                 if (result)
80                                         Console.WriteLine ("INFO: Lambda.Compatible Passed for {0}", delegate_type);
81                                 else
82                                         Console.WriteLine ("INFO: Lambda.Compatible Failed for {0}", delegate_type);
83                         } finally {
84                                 Report.EnableErrors ();
85                         }
86                         
87                         // Ignore the result
88                         anonymous = null;
89
90                         return result;
91                 }
92                 
93                 //
94                 // Returns true if this anonymous method can be implicitly
95                 // converted to the delegate type `delegate_type'
96                 //
97                 public override Expression Compatible (EmitContext ec, Type delegate_type)
98                 {
99                         return DoCompatibleTest (ec, delegate_type, false);
100                 }
101
102                 Expression DoCompatibleTest (EmitContext ec, Type delegate_type, bool clone)
103                 {
104                         if (anonymous != null)
105                                 return anonymous.AnonymousDelegate;
106
107                         if (CompatibleChecks (ec, delegate_type) == null)
108                                 return null;
109
110                         MethodGroupExpr invoke_mg = Delegate.GetInvokeMethod (
111                                 ec.ContainerType, delegate_type, loc);
112                         MethodInfo invoke_mb = (MethodInfo) invoke_mg.Methods [0];
113                         ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
114
115                         //
116                         // The lambda expression is compatible with the delegate type,
117                         // provided that:
118                         //
119
120                         //
121                         // D and L have the same number of arguments.
122                         if (Parameters.Count != invoke_pd.Count)
123                                 return null;
124
125                         if (explicit_parameters){
126                                 //
127                                 // If L has an explicitly typed parameter list, each parameter
128                                 // in D has the same type and modifiers as the corresponding
129                                 // parameter in L.
130                                 //
131                                 if (!VerifyExplicitParameterCompatibility (delegate_type, invoke_pd))
132                                         return null;
133                         } else {
134                                 //
135                                 // If L has an implicitly typed parameter list, D has no ref or
136                                 // out parameters
137                                 //
138                                 // Note: We currently do nothing, because the preview does not
139                                 // follow the above rule.
140
141                                 //
142                                 // each parameter of L is given the type of the corresponding parameter in D
143                                 //
144
145                                 for (int i = 0; i < invoke_pd.Count; i++)
146                                         Parameters [i].TypeName = new TypeExpression (
147                                                 invoke_pd.ParameterType (i),
148                                                 Parameters [i].Location);
149                         }
150
151                         //
152                         // The return type of the delegate must be compatible with 
153                         // the anonymous type.   Instead of doing a pass to examine the block
154                         // we satisfy the rule by setting the return type on the EmitContext
155                         // to be the delegate type return type.
156                         //
157
158                         ToplevelBlock b = clone ? (ToplevelBlock) Block.PerformClone () : Block;
159                         
160                         anonymous = new AnonymousMethod (
161                                 Parent != null ? Parent.AnonymousMethod : null, RootScope, Host,
162                                 GenericMethod, Parameters, Container, b, invoke_mb.ReturnType,
163                                 delegate_type, loc);
164
165                         if (!anonymous.Resolve (ec))
166                                 return null;
167
168                         return anonymous.AnonymousDelegate;
169                 }
170         }
171
172         //
173         // This is a return statement that is prepended lambda expression bodies that happen
174         // to be expressions.  Depending on the return type of the delegate this will behave
175         // as either { expr (); return (); } or { return expr (); }
176         //
177         public class ContextualReturn : Statement {
178                 public Expression Expr;
179                 
180                 public ContextualReturn (Expression e)
181                 {
182                         Expr = e;
183                         loc = Expr.Location;
184                 }
185
186                 bool unwind_protect;
187
188                 public override bool Resolve (EmitContext ec)
189                 {
190                         AnonymousContainer am = ec.CurrentAnonymousMethod;
191                         if ((am != null) && am.IsIterator && ec.InIterator) {
192                                 Report.Error (1622, loc, "Cannot return a value from iterators. Use the yield return " +
193                                               "statement to return a value, or yield break to end the iteration");
194                                 return false;
195                         }
196
197                         Expr = Expr.Resolve (ec);
198                         if (Expr == null)
199                                 return false;
200
201                         if (ec.ReturnType == null){
202                                 if (!(Expr is ExpressionStatement)){
203                                         Expression.Error_InvalidExpressionStatement (Expr.Location);
204                                         return false;
205                                 }
206                         } else {
207                                 if (Expr.Type != ec.ReturnType) {
208                                         Expr = Convert.ImplicitConversionRequired (
209                                                 ec, Expr, ec.ReturnType, loc);
210                                         if (Expr == null)
211                                                 return false;
212                                 }
213                         }
214
215                         int errors = Report.Errors;
216                         unwind_protect = ec.CurrentBranching.AddReturnOrigin (ec.CurrentBranching.CurrentUsageVector, loc);
217                         if (unwind_protect)
218                                 ec.NeedReturnLabel ();
219                         ec.CurrentBranching.CurrentUsageVector.Return ();
220                         return errors == Report.Errors;
221                 }
222                 
223                 protected override void DoEmit (EmitContext ec)
224                 {
225                         Expr.Emit (ec);
226
227                         if (unwind_protect){
228                                 ec.ig.Emit (OpCodes.Stloc, ec.TemporaryReturn ());
229                                 ec.ig.Emit (OpCodes.Leave, ec.ReturnLabel);
230                         } 
231                         ec.ig.Emit (OpCodes.Ret);
232                 }
233
234                 protected override void CloneTo (CloneContext clonectx, Statement t)
235                 {
236                         ContextualReturn cr = (ContextualReturn) t;
237
238                         cr.Expr = Expr.Clone (clonectx);
239                 }
240         }
241 }