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 (TypeContainer host, Parameters parameters, Location loc)
28 : base (host, 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 ParameterData 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 if (Parameters.Types == null)
80 Parameters.Types = new Type [Parameters.Count];
82 for (int i = 0; i < d_params.Count; i++) {
83 // D has no ref or out parameters
84 if ((d_params.ParameterModifier (i) & Parameter.Modifier.ISBYREF) != 0)
87 Type d_param = d_params.Types [i];
90 // Blablabla, because reflection does not work with dynamic types
91 if (d_param.IsGenericParameter)
92 d_param = delegateType.GetGenericArguments () [d_param.GenericParameterPosition];
94 // When inferred context exists all generics parameters have type replacements
96 d_param = tic.InflateGenericArgument (d_param);
99 Parameters.Types [i] = Parameters.FixedParameters[i].ParameterType = d_param;
104 public override Expression DoResolve (EmitContext ec)
107 // Only explicit parameters can be resolved at this point
109 if (explicit_parameters) {
110 if (!Parameters.Resolve (ec))
114 eclass = ExprClass.Value;
115 type = TypeManager.anonymous_method_type;
119 protected override AnonymousMethodBody CompatibleMethodFactory (Type returnType, Type delegateType, Parameters p, ToplevelBlock b)
121 return new LambdaMethod (Host,
126 public override string GetSignatureForError ()
128 return "lambda expression";
132 public class LambdaMethod : AnonymousMethodBody
134 public LambdaMethod (TypeContainer host, Parameters parameters,
135 ToplevelBlock block, Type return_type, Type delegate_type,
137 : base (host, parameters, block,
138 return_type, delegate_type, loc)
142 public override string ContainerType {
144 return "lambda expression";
148 public override Expression CreateExpressionTree (EmitContext ec)
150 return Block.CreateExpressionTree (ec);
155 // This is a return statement that is prepended lambda expression bodies that happen
156 // to be expressions. Depending on the return type of the delegate this will behave
157 // as either { expr (); return (); } or { return expr (); }
159 public class ContextualReturn : Return
161 public ContextualReturn (Expression expr)
162 : base (expr, expr.Location)
166 public override Expression CreateExpressionTree (EmitContext ec)
168 return Expr.CreateExpressionTree (ec);
171 public override void Emit (EmitContext ec)
173 if (ec.ReturnType == TypeManager.void_type)
174 ((ExpressionStatement) Expr).EmitStatement (ec);
179 protected override bool DoResolve (EmitContext ec)
182 // When delegate returns void, only expression statements can be used
184 if (ec.ReturnType == TypeManager.void_type) {
185 Expr = Expr.Resolve (ec);
189 if (Expr is ExpressionStatement) {
190 if (Expr is Invocation)
191 return TypeManager.IsEqual (Expr.Type, TypeManager.void_type);
195 Expr.Error_InvalidExpressionStatement ();
199 return base.DoResolve (ec);