2 // lambda.cs: support for lambda expressions
4 // Authors: Miguel de Icaza (miguel@gnu.org)
5 // Marek Safar (marek.safar@gmail.com)
7 // Dual licensed under the terms of the MIT X11 or GNU GPL
9 // Copyright 2007-2008 Novell, Inc
13 using System.Collections;
14 using System.Reflection;
15 using System.Reflection.Emit;
17 namespace Mono.CSharp {
18 public class LambdaExpression : AnonymousMethodExpression
20 readonly bool explicit_parameters;
23 // The parameters can either be:
24 // A list of Parameters (explicitly typed parameters)
25 // An ImplicitLambdaParameter
27 public LambdaExpression (Parameters parameters, Location loc)
28 : base (parameters, loc)
30 if (parameters.Count > 0)
31 explicit_parameters = !(parameters.FixedParameters [0] is ImplicitLambdaParameter);
34 protected override Expression CreateExpressionTree (EmitContext ec, Type delegate_type)
36 if (ec.IsInProbingMode)
39 Expression args = Parameters.CreateExpressionTree (ec, loc);
40 Expression expr = Block.CreateExpressionTree (ec);
44 ArrayList arguments = new ArrayList (2);
45 arguments.Add (new Argument (expr));
46 arguments.Add (new Argument (args));
47 return CreateExpressionFactoryCall ("Lambda",
48 new TypeArguments (loc, new TypeExpression (delegate_type, loc)),
52 public override bool HasExplicitParameters {
54 return explicit_parameters;
58 protected override Parameters ResolveParameters (EmitContext ec, TypeInferenceContext tic, Type delegateType)
60 if (!TypeManager.IsDelegateType (delegateType))
63 AParametersCollection d_params = TypeManager.GetDelegateParameters (delegateType);
65 if (explicit_parameters) {
66 if (!VerifyExplicitParameters (delegateType, d_params, ec.IsInProbingMode))
73 // If L has an implicitly typed parameter list we make implicit parameters explicit
74 // Set each parameter of L is given the type of the corresponding parameter in D
76 if (!VerifyParameterCompatibility (delegateType, d_params, ec.IsInProbingMode))
79 Type [] ptypes = new Type [Parameters.Count];
80 for (int i = 0; i < d_params.Count; i++) {
81 // D has no ref or out parameters
82 if ((d_params.FixedParameters [i].ModFlags & Parameter.Modifier.ISBYREF) != 0)
85 Type d_param = d_params.Types [i];
88 // Blablabla, because reflection does not work with dynamic types
89 if (d_param.IsGenericParameter)
90 d_param = delegateType.GetGenericArguments () [d_param.GenericParameterPosition];
93 // When type inference context exists try to apply inferred type arguments
96 d_param = tic.InflateGenericArgument (d_param);
100 ((ImplicitLambdaParameter) Parameters.FixedParameters [i]).Type = d_param;
103 Parameters.Types = ptypes;
107 public override Expression DoResolve (EmitContext ec)
110 // Only explicit parameters can be resolved at this point
112 if (explicit_parameters) {
113 if (!Parameters.Resolve (ec))
117 eclass = ExprClass.Value;
118 type = TypeManager.anonymous_method_type;
122 protected override AnonymousMethodBody CompatibleMethodFactory (Type returnType, Type delegateType, Parameters p, ToplevelBlock b)
124 return new LambdaMethod (p, b, returnType, delegateType, loc);
127 public override string GetSignatureForError ()
129 return "lambda expression";
133 public class LambdaMethod : AnonymousMethodBody
135 public LambdaMethod (Parameters parameters,
136 ToplevelBlock block, Type return_type, Type delegate_type,
138 : base (parameters, block, return_type, delegate_type, loc)
142 public override string ContainerType {
144 return "lambda expression";
148 public override Expression CreateExpressionTree (EmitContext ec)
150 Expression args = parameters.CreateExpressionTree (ec, loc);
151 Expression expr = Block.CreateExpressionTree (ec);
155 ArrayList arguments = new ArrayList (2);
156 arguments.Add (new Argument (expr));
157 arguments.Add (new Argument (args));
158 return CreateExpressionFactoryCall ("Lambda",
159 new TypeArguments (loc, new TypeExpression (type, loc)),
165 // This is a return statement that is prepended lambda expression bodies that happen
166 // to be expressions. Depending on the return type of the delegate this will behave
167 // as either { expr (); return (); } or { return expr (); }
169 public class ContextualReturn : Return
171 public ContextualReturn (Expression expr)
172 : base (expr, expr.Location)
176 public override Expression CreateExpressionTree (EmitContext ec)
178 return Expr.CreateExpressionTree (ec);
181 public override void Emit (EmitContext ec)
183 if (ec.ReturnType == TypeManager.void_type) {
184 ((ExpressionStatement) Expr).EmitStatement (ec);
185 ec.ig.Emit (OpCodes.Ret);
192 protected override bool DoResolve (EmitContext ec)
195 // When delegate returns void, only expression statements can be used
197 if (ec.ReturnType == TypeManager.void_type) {
198 Expr = Expr.Resolve (ec);
202 if (Expr is ExpressionStatement)
205 Expr.Error_InvalidExpressionStatement ();
209 return base.DoResolve (ec);