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;
15 namespace Mono.CSharp {
16 public class LambdaExpression : AnonymousMethodExpression {
17 bool explicit_parameters;
20 // If set, this was a lambda expression with an expression
21 // argument. And if so, we have a pointer to it, so we can
22 // change it if needed.
24 Expression lambda_expr;
27 // The parameter list can either be:
28 // null: no parameters
29 // arraylist of Parameter (explicitly typed parameters)
30 // arraylist of strings (implicitly typed parameters)
32 public LambdaExpression (AnonymousMethodExpression parent,
33 GenericMethod generic, TypeContainer host,
34 Parameters parameters, Block container,
36 : base (parent, generic, host, parameters, container, loc)
38 explicit_parameters = (parameters != null && parameters.Count > 0 && parameters [0].TypeName != null);
39 if (parameters == null)
40 Parameters = new Parameters (new Parameter [0]);
43 public void SetExpression (Expression expr)
48 public override Expression DoResolve (EmitContext ec)
50 eclass = ExprClass.Value;
51 type = TypeManager.anonymous_method_type;
53 if (explicit_parameters){
54 if (!Parameters.Resolve (ec))
58 // We will resolve parameters later, we do not
59 // have information at this point.
64 public override void Emit (EmitContext ec)
70 // Returns true if this anonymous method can be implicitly
71 // converted to the delegate type `delegate_type'
73 public override Expression Compatible (EmitContext ec, Type delegate_type)
75 if (anonymous != null)
76 return anonymous.AnonymousDelegate;
78 if (CompatibleChecks (ec, delegate_type) == null)
81 MethodGroupExpr invoke_mg = Delegate.GetInvokeMethod (
82 ec.ContainerType, delegate_type, loc);
83 MethodInfo invoke_mb = (MethodInfo) invoke_mg.Methods [0];
84 ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
87 // The lambda expression is compatible with the delegate type,
92 // D and L have the same number of arguments.
93 if (Parameters.Count != invoke_pd.Count)
96 Parameters parameters_copy = Parameters.Clone ();
97 if (explicit_parameters){
99 // If L has an explicitly typed parameter list, each parameter
100 // in D has the same type and modifiers as the corresponding
103 if (!VerifyExplicitParameterCompatibility (delegate_type, invoke_pd))
106 parameters_copy = Parameters.Clone ();
109 // If L has an implicitly typed parameter list, D has no ref or
112 // Note: We currently do nothing, because the preview does not
113 // follow the above rule.
116 // each parameter of L is given the type of the corresponding parameter in D
119 for (int i = 0; i < invoke_pd.Count; i++)
120 parameters_copy [i].TypeName = new TypeExpression (
121 invoke_pd.ParameterType (i),
122 parameters_copy [i].Location);
126 if (invoke_mb.ReturnType == TypeManager.void_type){
130 if (lambda_expr == null){
136 // Second: the return type of the delegate must be compatible with
137 // the anonymous type. Instead of doing a pass to examine the block
138 // we satisfy the rule by setting the return type on the EmitContext
139 // to be the delegate type return type.
142 //MethodBuilder builder = method_data.MethodBuilder;
143 //ILGenerator ig = builder.GetILGenerator ();
145 Report.Debug (64, "COMPATIBLE", this, Parent, GenericMethod, Host,
146 Container, Block, invoke_mb.ReturnType, delegate_type,
147 TypeManager.IsGenericType (delegate_type), loc);
149 anonymous = new AnonymousMethod (
150 Parent != null ? Parent.AnonymousMethod : null, RootScope, Host,
151 GenericMethod, parameters, Container, Block, invoke_mb.ReturnType,
154 if (!anonymous.Resolve (ec))
157 return anonymous.AnonymousDelegate;