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