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 // The parameter list can either be:
22 // null: no parameters
23 // arraylist of Parameter (explicitly typed parameters)
24 // arraylist of strings (implicitly typed parameters)
26 public LambdaExpression (AnonymousMethodExpression parent,
27 GenericMethod generic, TypeContainer host,
28 Parameters parameters, Block container,
30 : base (parent, generic, host, parameters, container, loc)
32 explicit_parameters = (parameters != null && parameters.Count > 0 && parameters [0].TypeName != null);
33 if (parameters == null)
34 Parameters = new Parameters (new Parameter [0]);
37 public bool HasImplicitParameters {
39 return !explicit_parameters;
43 public bool HasExplicitParameters {
45 return explicit_parameters;
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)
70 public override bool ImplicitStandardConversionExists (Type delegate_type)
72 EmitContext ec = EmitContext.TempEc;
77 Report.DisableErrors ();
78 result = DoCompatibleTest (ec, delegate_type, true) != null;
80 Report.EnableErrors ();
90 // Returns true if this anonymous method can be implicitly
91 // converted to the delegate type `delegate_type'
93 public override Expression Compatible (EmitContext ec, Type delegate_type)
95 return DoCompatibleTest (ec, delegate_type, false);
98 Expression DoCompatibleTest (EmitContext ec, Type delegate_type, bool clone)
100 if (anonymous != null)
101 return anonymous.AnonymousDelegate;
103 if (CompatibleChecks (ec, delegate_type) == null)
106 MethodGroupExpr invoke_mg = Delegate.GetInvokeMethod (
107 ec.ContainerType, delegate_type, loc);
108 MethodInfo invoke_mb = (MethodInfo) invoke_mg.Methods [0];
109 ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
112 // The lambda expression is compatible with the delegate type,
117 // D and L have the same number of arguments.
118 if (Parameters.Count != invoke_pd.Count)
121 if (explicit_parameters){
123 // If L has an explicitly typed parameter list, each parameter
124 // in D has the same type and modifiers as the corresponding
127 if (!VerifyExplicitParameterCompatibility (delegate_type, invoke_pd))
131 // If L has an implicitly typed parameter list, D has no ref or
134 // Note: We currently do nothing, because the preview does not
135 // follow the above rule.
138 // each parameter of L is given the type of the corresponding parameter in D
141 for (int i = 0; i < invoke_pd.Count; i++)
142 Parameters [i].TypeName = new TypeExpression (
143 invoke_pd.ParameterType (i),
144 Parameters [i].Location);
148 // The return type of the delegate must be compatible with
149 // the anonymous type. Instead of doing a pass to examine the block
150 // we satisfy the rule by setting the return type on the EmitContext
151 // to be the delegate type return type.
154 ToplevelBlock b = clone ? (ToplevelBlock) Block.PerformClone () : Block;
156 anonymous = new AnonymousMethod (
157 Parent != null ? Parent.AnonymousMethod : null, RootScope, Host,
158 GenericMethod, Parameters, Container, b, invoke_mb.ReturnType,
161 if (!anonymous.Resolve (ec))
164 return anonymous.AnonymousDelegate;
169 // This is a return statement that is prepended lambda expression bodies that happen
170 // to be expressions. Depending on the return type of the delegate this will behave
171 // as either { expr (); return (); } or { return expr (); }
173 public class ContextualReturn : Statement {
174 public Expression Expr;
176 public ContextualReturn (Expression e)
184 public override bool Resolve (EmitContext ec)
186 AnonymousContainer am = ec.CurrentAnonymousMethod;
187 if ((am != null) && am.IsIterator && ec.InIterator) {
188 Report.Error (1622, loc, "Cannot return a value from iterators. Use the yield return " +
189 "statement to return a value, or yield break to end the iteration");
193 Expr = Expr.Resolve (ec);
197 if (ec.ReturnType == null){
198 if (!(Expr is ExpressionStatement)){
199 Expression.Error_InvalidExpressionStatement (Expr.Location);
203 if (Expr.Type != ec.ReturnType) {
204 Expr = Convert.ImplicitConversionRequired (
205 ec, Expr, ec.ReturnType, loc);
211 int errors = Report.Errors;
212 unwind_protect = ec.CurrentBranching.AddReturnOrigin (ec.CurrentBranching.CurrentUsageVector, loc);
214 ec.NeedReturnLabel ();
215 ec.CurrentBranching.CurrentUsageVector.Goto ();
216 return errors == Report.Errors;
219 protected override void DoEmit (EmitContext ec)
224 ec.ig.Emit (OpCodes.Stloc, ec.TemporaryReturn ());
225 ec.ig.Emit (OpCodes.Leave, ec.ReturnLabel);
227 ec.ig.Emit (OpCodes.Ret);
230 protected override void CloneTo (CloneContext clonectx, Statement t)
232 ContextualReturn cr = (ContextualReturn) t;
234 cr.Expr = Expr.Clone (clonectx);