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 parameters can either be:
22 // A list of Parameters (explicitly typed parameters)
23 // An ImplicitLambdaParameter
25 public LambdaExpression (AnonymousMethodExpression parent,
26 GenericMethod generic, TypeContainer host,
27 Parameters parameters, Block container,
29 : base (parent, generic, host, parameters, container, loc)
31 explicit_parameters = parameters.FixedParameters [0].TypeName != null;
34 public bool HasExplicitParameters {
36 return explicit_parameters;
40 public override Expression DoResolve (EmitContext ec)
43 // Only explicit parameters can be resolved at this point
45 if (explicit_parameters) {
46 if (!Parameters.Resolve (ec))
50 eclass = ExprClass.Value;
51 type = TypeManager.anonymous_method_type;
55 public override bool ImplicitStandardConversionExists (Type delegate_type)
57 EmitContext ec = EmitContext.TempEc;
62 Report.DisableErrors ();
63 result = DoCompatibleTest (ec, delegate_type, true) != null;
65 Report.EnableErrors ();
75 // Returns true if this anonymous method can be implicitly
76 // converted to the delegate type `delegate_type'
78 public override Expression Compatible (EmitContext ec, Type delegate_type)
80 return DoCompatibleTest (ec, delegate_type, false);
83 Expression DoCompatibleTest (EmitContext ec, Type delegate_type, bool clone)
85 if (anonymous != null)
86 return anonymous.AnonymousDelegate;
88 if (CompatibleChecks (ec, delegate_type) == null)
91 MethodGroupExpr invoke_mg = Delegate.GetInvokeMethod (
92 ec.ContainerType, delegate_type, loc);
93 MethodInfo invoke_mb = (MethodInfo) invoke_mg.Methods [0];
94 ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
97 // The lambda expression is compatible with the delegate type,
102 // D and L have the same number of arguments.
103 if (Parameters.Count != invoke_pd.Count)
106 if (explicit_parameters){
108 // If L has an explicitly typed parameter list, each parameter
109 // in D has the same type and modifiers as the corresponding
112 if (!VerifyExplicitParameterCompatibility (delegate_type, invoke_pd, false))
116 // If L has an implicitly typed parameter list, D has no ref or
119 // Note: We currently do nothing, because the preview does not
120 // follow the above rule.
123 // each parameter of L is given the type of the corresponding parameter in D
126 for (int i = 0; i < invoke_pd.Count; i++)
127 Parameters [i].TypeName = new TypeExpression (
128 invoke_pd.ParameterType (i),
129 Parameters [i].Location);
132 return CoreCompatibilityTest (ec, clone, invoke_mb.ReturnType, delegate_type);
135 Expression CoreCompatibilityTest (EmitContext ec, bool clone, Type return_type, Type delegate_type)
138 // The return type of the delegate must be compatible with
139 // the anonymous type. Instead of doing a pass to examine the block
140 // we satisfy the rule by setting the return type on the EmitContext
141 // to be the delegate type return type.
144 ToplevelBlock b = clone ? (ToplevelBlock) Block.PerformClone () : Block;
146 anonymous = new AnonymousMethod (
147 Parent != null ? Parent.AnonymousMethod : null, RootScope, Host,
148 GenericMethod, Parameters, Container, b, return_type,
153 r = anonymous.ResolveNoDefine (ec);
155 r = anonymous.Resolve (ec);
157 // Resolution failed.
161 return anonymous.AnonymousDelegate;
164 public override string GetSignatureForError ()
166 return "lambda expression";
170 // TryBuild: tries to compile this LambdaExpression with the given
171 // types as the lambda expression parameter types.
173 // If the lambda expression successfully builds with those types, the
174 // return value will be the inferred type for the lambda expression,
175 // otherwise the result will be null.
177 public Type TryBuild (EmitContext ec, Type [] types)
179 for (int i = 0; i < types.Length; i++)
180 Parameters [i].TypeName = new TypeExpression (types [i], Parameters [i].Location);
184 Report.DisableErrors ();
185 e = CoreCompatibilityTest (ec, true, null, null);
187 Report.EnableErrors ();
198 // This is a return statement that is prepended lambda expression bodies that happen
199 // to be expressions. Depending on the return type of the delegate this will behave
200 // as either { expr (); return (); } or { return expr (); }
202 public class ContextualReturn : Statement {
203 public Expression Expr;
205 public ContextualReturn (Expression e)
213 public override bool Resolve (EmitContext ec)
215 AnonymousContainer am = ec.CurrentAnonymousMethod;
216 if ((am != null) && am.IsIterator && ec.InIterator) {
217 Report.Error (1622, loc, "Cannot return a value from iterators. Use the yield return " +
218 "statement to return a value, or yield break to end the iteration");
222 Expr = Expr.Resolve (ec);
226 if (ec.ReturnType == null){
227 ec.ReturnType = Expr.Type;
229 if (Expr.Type != ec.ReturnType) {
230 Expression nExpr = Convert.ImplicitConversionRequired (
231 ec, Expr, ec.ReturnType, loc);
233 Report.Error (1662, loc, "Could not implicitly convert from {0} to {1}",
234 TypeManager.CSharpName (Expr.Type),
235 TypeManager.CSharpName (ec.ReturnType));
242 int errors = Report.Errors;
243 unwind_protect = ec.CurrentBranching.AddReturnOrigin (ec.CurrentBranching.CurrentUsageVector, loc);
245 ec.NeedReturnLabel ();
246 ec.CurrentBranching.CurrentUsageVector.Goto ();
247 return errors == Report.Errors;
250 protected override void DoEmit (EmitContext ec)
255 ec.ig.Emit (OpCodes.Stloc, ec.TemporaryReturn ());
256 ec.ig.Emit (OpCodes.Leave, ec.ReturnLabel);
258 ec.ig.Emit (OpCodes.Ret);
261 protected override void CloneTo (CloneContext clonectx, Statement t)
263 ContextualReturn cr = (ContextualReturn) t;
265 cr.Expr = Expr.Clone (clonectx);