Grasshopper project system now uses csproj extension
[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                 // If set, this was a lambda expression with an expression
22                 // argument.  And if so, we have a pointer to it, so we can
23                 // change it if needed.
24                 //
25                 Expression lambda_expr;
26                 
27                 //
28                 // The parameter list can either be:
29                 //    null: no parameters
30                 //    arraylist of Parameter (explicitly typed parameters)
31                 //    arraylist of strings (implicitly typed parameters)
32                 //
33                 public LambdaExpression (AnonymousMethodExpression parent,
34                                          GenericMethod generic, TypeContainer host,
35                                          Parameters parameters, Block container,
36                                          Location loc)
37                         : base (parent, generic, host, parameters, container, loc)
38                 {
39                         explicit_parameters = (parameters != null && parameters.Count > 0 && parameters [0].TypeName != null);
40                         if (parameters == null)
41                                 Parameters = new Parameters (new Parameter [0]);
42                 }
43
44                 public void SetExpression (Expression expr)
45                 {
46                         lambda_expr = expr;
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                 //
71                 // Returns true if this anonymous method can be implicitly
72                 // converted to the delegate type `delegate_type'
73                 //
74                 public override Expression Compatible (EmitContext ec, Type delegate_type)
75                 {
76                         if (anonymous != null)
77                                 return anonymous.AnonymousDelegate;
78
79                         if (CompatibleChecks (ec, delegate_type) == null)
80                                 return null;
81
82                         MethodGroupExpr invoke_mg = Delegate.GetInvokeMethod (
83                                 ec.ContainerType, delegate_type, loc);
84                         MethodInfo invoke_mb = (MethodInfo) invoke_mg.Methods [0];
85                         ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
86
87                         //
88                         // The lambda expression is compatible with the delegate type,
89                         // provided that:
90                         //
91
92                         //
93                         // D and L have the same number of arguments.
94                         if (Parameters.Count != invoke_pd.Count)
95                                 return null;
96
97                         Parameters parameters_copy = Parameters.Clone ();
98                         if (explicit_parameters){
99                                 //
100                                 // If L has an explicitly typed parameter list, each parameter
101                                 // in D has the same type and modifiers as the corresponding
102                                 // parameter in L.
103                                 //
104                                 if (!VerifyExplicitParameterCompatibility (delegate_type, invoke_pd))
105                                         return null;
106
107                                 parameters_copy = Parameters.Clone ();
108                         } else {
109                                 //
110                                 // If L has an implicitly typed parameter list, D has no ref or
111                                 // out parameters
112                                 //
113                                 // Note: We currently do nothing, because the preview does not
114                                 // follow the above rule.
115
116                                 //
117                                 // each parameter of L is given the type of the corresponding parameter in D
118                                 //
119
120                                 for (int i = 0; i < invoke_pd.Count; i++)
121                                         parameters_copy [i].TypeName = new TypeExpression (
122                                                 invoke_pd.ParameterType (i),
123                                                 parameters_copy [i].Location);
124                         }
125
126                         //
127                         // The return type of the delegate must be compatible with 
128                         // the anonymous type.   Instead of doing a pass to examine the block
129                         // we satisfy the rule by setting the return type on the EmitContext
130                         // to be the delegate type return type.
131                         //
132
133                         anonymous = new AnonymousMethod (
134                                 Parent != null ? Parent.AnonymousMethod : null, RootScope, Host,
135                                 GenericMethod, parameters_copy, Container, Block, invoke_mb.ReturnType,
136                                 delegate_type, loc);
137
138                         if (!anonymous.Resolve (ec))
139                                 return null;
140
141                         return anonymous.AnonymousDelegate;
142                 }
143         }
144
145         //
146         // This is a return statement that is prepended lambda expression bodies that happen
147         // to be expressions.  Depending on the return type of the delegate this will behave
148         // as either { expr (); return (); } or { return expr (); }
149         //
150         public class ContextualReturn : Statement {
151                 public Expression Expr;
152                 
153                 public ContextualReturn (Expression e)
154                 {
155                         Expr = e;
156                         loc = Expr.Location;
157                 }
158
159                 bool unwind_protect;
160
161                 public override bool Resolve (EmitContext ec)
162                 {
163                         AnonymousContainer am = ec.CurrentAnonymousMethod;
164                         if ((am != null) && am.IsIterator && ec.InIterator) {
165                                 Report.Error (1622, loc, "Cannot return a value from iterators. Use the yield return " +
166                                               "statement to return a value, or yield break to end the iteration");
167                                 return false;
168                         }
169
170                         Expr = Expr.Resolve (ec);
171                         if (Expr == null)
172                                 return false;
173
174                         if (ec.ReturnType == null){
175                                 if (!(Expr is ExpressionStatement)){
176                                         Expression.Error_InvalidExpressionStatement (Expr.Location);
177                                         return false;
178                                 }
179                         } else {
180                                 if (Expr.Type != ec.ReturnType) {
181                                         Expr = Convert.ImplicitConversionRequired (
182                                                 ec, Expr, ec.ReturnType, loc);
183                                         if (Expr == null)
184                                                 return false;
185                                 }
186                         }
187
188                         int errors = Report.Errors;
189                         unwind_protect = ec.CurrentBranching.AddReturnOrigin (ec.CurrentBranching.CurrentUsageVector, loc);
190                         if (unwind_protect)
191                                 ec.NeedReturnLabel ();
192                         ec.CurrentBranching.CurrentUsageVector.Return ();
193                         return errors == Report.Errors;
194                 }
195                 
196                 protected override void DoEmit (EmitContext ec)
197                 {
198                         Expr.Emit (ec);
199
200                         if (unwind_protect){
201                                 ec.ig.Emit (OpCodes.Stloc, ec.TemporaryReturn ());
202                                 ec.ig.Emit (OpCodes.Leave, ec.ReturnLabel);
203                         } 
204                         ec.ig.Emit (OpCodes.Ret);
205                 }
206                 
207         }
208 }