// lambda.cs: support for lambda expressions
//
// Authors: Miguel de Icaza (miguel@gnu.org)
+// Marek Safar (marek.safar@gmail.com)
//
// Licensed under the terms of the GNU GPL
//
using System.Reflection.Emit;
namespace Mono.CSharp {
- public class LambdaExpression : AnonymousMethodExpression {
- bool explicit_parameters;
+ public class LambdaExpression : AnonymousMethodExpression
+ {
+ readonly bool explicit_parameters;
//
// The parameters can either be:
Location loc)
: base (parent, generic, host, parameters, container, loc)
{
- explicit_parameters = parameters.FixedParameters [0].TypeName != null;
+ if (parameters.Count > 0)
+ explicit_parameters = !(parameters.FixedParameters [0] is ImplicitLambdaParameter);
}
- public bool HasExplicitParameters {
- get {
- return explicit_parameters;
- }
- }
-
- public override Expression DoResolve (EmitContext ec)
+ public static Expression System_Linq_Expressions;
+ public static Expression System_Linq_Expressions_Expression;
+
+ protected override Expression CreateExpressionTree (EmitContext ec, Type delegate_type)
{
- //
- // Only explicit parameters can be resolved at this point
- //
- if (explicit_parameters) {
- if (!Parameters.Resolve (ec))
- return null;
- }
+ System_Linq_Expressions = new MemberAccess (
+ new MemberAccess (new SimpleName ("System", loc), "Linq", loc), "Expressions", loc);
- eclass = ExprClass.Value;
- type = TypeManager.anonymous_method_type;
- return this;
- }
+ System_Linq_Expressions_Expression = new MemberAccess (
+ System_Linq_Expressions, "Expression", loc);
- //
- // Returns true if the body of lambda expression can be implicitly
- // converted to the delegate of type `delegate_type'
- //
- public override bool ImplicitStandardConversionExists (Type delegate_type)
- {
- EmitContext ec = EmitContext.TempEc;
+ MemberAccess lambda = new MemberAccess (System_Linq_Expressions_Expression, "Lambda",
+ new TypeArguments (loc, new TypeExpression (delegate_type, loc)), loc);
- using (ec.Set (EmitContext.Flags.ProbingMode)) {
- bool r = DoImplicitStandardConversion (ec, delegate_type) != null;
+ Expression args = Parameters.CreateExpressionTree (ec, loc);
+ Expression expr = Block.CreateExpressionTree (ec);
+ if (expr == null)
+ return null;
- // Ignore the result
- anonymous = null;
+ ArrayList arguments = new ArrayList (2);
+ arguments.Add (new Argument (expr));
+ arguments.Add (new Argument (args));
+ Expression invocation = new Invocation (lambda, arguments);
- return r;
- }
+ return invocation;
}
-
- //
- // Returns true if this anonymous method can be implicitly
- // converted to the delegate type `delegate_type'
- //
- public override Expression Compatible (EmitContext ec, Type delegate_type)
- {
- if (anonymous != null)
- return anonymous.AnonymousDelegate;
- return DoImplicitStandardConversion (ec, delegate_type);
+ public override bool HasExplicitParameters {
+ get {
+ return explicit_parameters;
+ }
}
- Expression DoImplicitStandardConversion (EmitContext ec, Type delegate_type)
+ protected override Parameters ResolveParameters (EmitContext ec, TypeInferenceContext tic, Type delegateType)
{
- if (CompatibleChecks (ec, delegate_type) == null)
+ if (!TypeManager.IsDelegateType (delegateType))
return null;
- MethodGroupExpr invoke_mg = Delegate.GetInvokeMethod (
- ec.ContainerType, delegate_type, loc);
- MethodInfo invoke_mb = (MethodInfo) invoke_mg.Methods [0];
- ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
+ ParameterData d_params = TypeManager.GetDelegateParameters (delegateType);
- //
- // The lambda expression is compatible with the delegate type,
- // provided that:
- //
+ if (explicit_parameters) {
+ if (!VerifyExplicitParameters (delegateType, d_params, ec.IsInProbingMode))
+ return null;
+
+ return Parameters;
+ }
//
- // D and L have the same number of arguments.
- if (Parameters.Count != invoke_pd.Count)
+ // If L has an implicitly typed parameter list we make implicit parameters explicit
+ // Set each parameter of L is given the type of the corresponding parameter in D
+ //
+ if (!VerifyParameterCompatibility (delegateType, d_params, ec.IsInProbingMode))
return null;
- if (explicit_parameters){
- //
- // If L has an explicitly typed parameter list, each parameter
- // in D has the same type and modifiers as the corresponding
- // parameter in L.
- //
- if (!VerifyExplicitParameterCompatibility (delegate_type, invoke_pd, false))
+ if (Parameters.Types == null)
+ Parameters.Types = new Type [Parameters.Count];
+
+ for (int i = 0; i < d_params.Count; i++) {
+ // D has no ref or out parameters
+ if ((d_params.ParameterModifier (i) & Parameter.Modifier.ISBYREF) != 0)
return null;
- } else {
- //
- // If L has an implicitly typed parameter list
- //
- for (int i = 0; i < invoke_pd.Count; i++) {
- // D has no ref or out parameters
- if ((invoke_pd.ParameterModifier (i) & Parameter.Modifier.ISBYREF) != 0)
- return null;
-
- //
- // Makes implicit parameters explicit
- // Set each parameter of L is given the type of the corresponding parameter in D
- //
- Parameters[i].ParameterType = invoke_pd.Types[i];
-
+
+ Type d_param = d_params.Types [i];
+
+#if MS_COMPATIBLE
+ // Blablabla, because reflection does not work with dynamic types
+ if (d_param.IsGenericParameter)
+ d_param = delegateType.GetGenericArguments () [d_param.GenericParameterPosition];
+#endif
+ // When inferred context exists all generics parameters have type replacements
+ if (tic != null) {
+ d_param = tic.InflateGenericArgument (d_param);
}
- }
- return CoreCompatibilityTest (ec, invoke_mb.ReturnType, delegate_type);
+ Parameters.Types [i] = Parameters.FixedParameters[i].ParameterType = d_param;
+ }
+ return Parameters;
}
- Expression CoreCompatibilityTest (EmitContext ec, Type return_type, Type delegate_type)
+ public override Expression DoResolve (EmitContext ec)
{
//
- // The return type of the delegate must be compatible with
- // the anonymous type. Instead of doing a pass to examine the block
- // we satisfy the rule by setting the return type on the EmitContext
- // to be the delegate type return type.
+ // Only explicit parameters can be resolved at this point
//
+ if (explicit_parameters) {
+ if (!Parameters.Resolve (ec))
+ return null;
+ }
- ToplevelBlock b = ec.IsInProbingMode ? (ToplevelBlock) Block.PerformClone () : Block;
-
- anonymous = new LambdaMethod (
- Parent != null ? Parent.AnonymousMethod : null, RootScope, Host,
- GenericMethod, Parameters, Container, b, return_type,
- delegate_type, loc);
-
- bool r;
- if (ec.IsInProbingMode)
- r = anonymous.ResolveNoDefine (ec);
- else
- r = anonymous.Resolve (ec);
-
- // Resolution failed.
- if (!r)
- return null;
+ eclass = ExprClass.Value;
+ type = TypeManager.anonymous_method_type;
+ return this;
+ }
- return anonymous.AnonymousDelegate;
+ protected override AnonymousMethod CompatibleMethodFactory (Type returnType, Type delegateType, Parameters p, ToplevelBlock b)
+ {
+ return new LambdaMethod (RootScope, Host,
+ GenericMethod, p, Container, b, returnType,
+ delegateType, loc);
}
-
+
public override string GetSignatureForError ()
{
return "lambda expression";
}
+ }
- //
- // TryBuild: tries to compile this LambdaExpression with the given
- // types as the lambda expression parameter types.
- //
- // If the lambda expression successfully builds with those types, the
- // return value will be the inferred type for the lambda expression,
- // otherwise the result will be null.
- //
- public Type TryBuild (EmitContext ec, Type [] types)
+ public class LambdaMethod : AnonymousMethod
+ {
+ public LambdaMethod (RootScopeInfo root_scope,
+ DeclSpace host, GenericMethod generic,
+ Parameters parameters, Block container,
+ ToplevelBlock block, Type return_type, Type delegate_type,
+ Location loc)
+ : base (root_scope, host, generic, parameters, container, block,
+ return_type, delegate_type, loc)
{
- for (int i = 0; i < types.Length; i++)
- Parameters [i].TypeName = new TypeExpression (types [i], Parameters [i].Location);
+ }
- // TODO: temporary hack
- ec.InferReturnType = true;
-
- Expression e;
- using (ec.Set (EmitContext.Flags.ProbingMode)) {
- e = CoreCompatibilityTest (ec, typeof (LambdaExpression), null);
+ public override string ContainerType {
+ get {
+ return "lambda expression";
}
-
- if (e == null)
- return null;
-
- return e.Type;
}
}
// This is a return statement that is prepended lambda expression bodies that happen
// to be expressions. Depending on the return type of the delegate this will behave
// as either { expr (); return (); } or { return expr (); }
-
+ //
public class ContextualReturn : Return
{
+ bool statement_return;
+
public ContextualReturn (Expression expr)
: base (expr, expr.Location)
{
}
+
+ public override Expression CreateExpressionTree (EmitContext ec)
+ {
+ return Expr.CreateExpressionTree (ec);
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ if (statement_return) {
+ ExpressionStatement es = Expr as ExpressionStatement;
+ if (es == null) {
+ Expr.Error_InvalidExpressionStatement ();
+ return;
+ }
+
+ es.EmitStatement (ec);
+ return;
+ }
+
+ base.Emit (ec);
+ }
+
+ public override bool Resolve (EmitContext ec)
+ {
+ if (ec.ReturnType == TypeManager.void_type) {
+ statement_return = true;
+ return Expr.Resolve (ec) != null;
+ }
+
+ return base.Resolve (ec);
+ }
}
}