2 // lambda.cs: support for lambda expressions
4 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Licensed under the terms of the GNU GPL
8 // (C) 2007 Novell, Inc
12 using System.Collections;
13 using System.Reflection;
14 using System.Reflection.Emit;
16 namespace Mono.CSharp {
17 public class LambdaExpression : AnonymousMethodExpression {
18 bool explicit_parameters;
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.
25 Expression lambda_expr;
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)
33 public LambdaExpression (AnonymousMethodExpression parent,
34 GenericMethod generic, TypeContainer host,
35 Parameters parameters, Block container,
37 : base (parent, generic, host, parameters, container, loc)
39 explicit_parameters = (parameters != null && parameters.Count > 0 && parameters [0].TypeName != null);
40 if (parameters == null)
41 Parameters = new Parameters (new Parameter [0]);
44 public void SetExpression (Expression expr)
49 public override Expression DoResolve (EmitContext ec)
51 eclass = ExprClass.Value;
52 type = TypeManager.anonymous_method_type;
54 if (explicit_parameters){
55 if (!Parameters.Resolve (ec))
59 // We will resolve parameters later, we do not
60 // have information at this point.
65 public override void Emit (EmitContext ec)
71 // Returns true if this anonymous method can be implicitly
72 // converted to the delegate type `delegate_type'
74 public override Expression Compatible (EmitContext ec, Type delegate_type)
76 if (anonymous != null)
77 return anonymous.AnonymousDelegate;
79 if (CompatibleChecks (ec, delegate_type) == null)
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);
88 // The lambda expression is compatible with the delegate type,
93 // D and L have the same number of arguments.
94 if (Parameters.Count != invoke_pd.Count)
97 Parameters parameters_copy = Parameters.Clone ();
98 if (explicit_parameters){
100 // If L has an explicitly typed parameter list, each parameter
101 // in D has the same type and modifiers as the corresponding
104 if (!VerifyExplicitParameterCompatibility (delegate_type, invoke_pd))
107 parameters_copy = Parameters.Clone ();
110 // If L has an implicitly typed parameter list, D has no ref or
113 // Note: We currently do nothing, because the preview does not
114 // follow the above rule.
117 // each parameter of L is given the type of the corresponding parameter in D
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);
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.
133 anonymous = new AnonymousMethod (
134 Parent != null ? Parent.AnonymousMethod : null, RootScope, Host,
135 GenericMethod, parameters_copy, Container, Block, invoke_mb.ReturnType,
138 if (!anonymous.Resolve (ec))
141 return anonymous.AnonymousDelegate;
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 (); }
150 public class ContextualReturn : Statement {
151 public Expression Expr;
153 public ContextualReturn (Expression e)
161 public override bool Resolve (EmitContext ec)
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");
170 Expr = Expr.Resolve (ec);
174 if (ec.ReturnType == null){
175 if (!(Expr is ExpressionStatement)){
176 Expression.Error_InvalidExpressionStatement (Expr.Location);
180 if (Expr.Type != ec.ReturnType) {
181 Expr = Convert.ImplicitConversionRequired (
182 ec, Expr, ec.ReturnType, loc);
188 int errors = Report.Errors;
189 unwind_protect = ec.CurrentBranching.AddReturnOrigin (ec.CurrentBranching.CurrentUsageVector, loc);
191 ec.NeedReturnLabel ();
192 ec.CurrentBranching.CurrentUsageVector.Return ();
193 return errors == Report.Errors;
196 protected override void DoEmit (EmitContext ec)
201 ec.ig.Emit (OpCodes.Stloc, ec.TemporaryReturn ());
202 ec.ig.Emit (OpCodes.Leave, ec.ReturnLabel);
204 ec.ig.Emit (OpCodes.Ret);