// 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
+// Dual licensed under the terms of the MIT X11 or GNU GPL
//
-// (C) 2007 Novell, Inc
+// Copyright 2007-2008 Novell, Inc
//
using System;
-using System.Collections;
using System.Reflection;
using System.Reflection.Emit;
namespace Mono.CSharp {
- public class LambdaExpression : AnonymousMethodExpression {
- bool explicit_parameters;
-
+ public class LambdaExpression : AnonymousMethodExpression
+ {
//
- // The parameter list can either be:
- // null: no parameters
- // arraylist of Parameter (explicitly typed parameters)
- // arraylist of strings (implicitly typed parameters)
+ // The parameters can either be:
+ // A list of Parameters (explicitly typed parameters)
+ // An ImplicitLambdaParameter
//
- public LambdaExpression (AnonymousMethodExpression parent,
- GenericMethod generic, TypeContainer host,
- Parameters parameters, Block container,
- Location loc)
- : base (parent, generic, host, parameters, container, loc)
+ public LambdaExpression (Location loc)
+ : base (loc)
{
- explicit_parameters = (parameters != null && parameters.Count > 0 && parameters [0].TypeName != null);
- if (parameters == null)
- Parameters = new Parameters (new Parameter [0]);
- }
-
- public bool HasImplicitParameters {
- get {
- return !explicit_parameters;
- }
}
- public bool HasExplicitParameters {
- get {
- return explicit_parameters;
- }
- }
-
- public override Expression DoResolve (EmitContext ec)
+ protected override Expression CreateExpressionTree (ResolveContext ec, TypeSpec delegate_type)
{
- eclass = ExprClass.Value;
- type = TypeManager.anonymous_method_type;
+ if (ec.IsInProbingMode)
+ return this;
- if (explicit_parameters){
- if (!Parameters.Resolve (ec))
- return null;
- }
-
- // We will resolve parameters later, we do not
- // have information at this point.
-
- return this;
- }
+ BlockContext bc = new BlockContext (ec.MemberContext, ec.CurrentBlock.Explicit, TypeManager.void_type) {
+ CurrentAnonymousMethod = ec.CurrentAnonymousMethod
+ };
- public override void Emit (EmitContext ec)
- {
- base.Emit (ec);
+ Expression args = Parameters.CreateExpressionTree (bc, loc);
+ Expression expr = Block.CreateExpressionTree (ec);
+ if (expr == null)
+ return null;
+
+ Arguments arguments = new Arguments (2);
+ arguments.Add (new Argument (expr));
+ arguments.Add (new Argument (args));
+ return CreateExpressionFactoryCall (ec, "Lambda",
+ new TypeArguments (new TypeExpression (delegate_type, loc)),
+ arguments);
}
- public override bool ImplicitStandardConversionExists (Type delegate_type)
- {
- EmitContext ec = EmitContext.TempEc;
-
- bool result;
-
- try {
- Report.DisableErrors ();
- result = DoCompatibleTest (ec, delegate_type, true) != null;
- if (result)
- Console.WriteLine ("INFO: Lambda.Compatible Passed for {0}", delegate_type);
- else
- Console.WriteLine ("INFO: Lambda.Compatible Failed for {0}", delegate_type);
- } finally {
- Report.EnableErrors ();
+ public override bool HasExplicitParameters {
+ get {
+ return Parameters.Count > 0 && !(Parameters.FixedParameters [0] is ImplicitLambdaParameter);
}
-
- // Ignore the result
- anonymous = null;
-
- return result;
- }
-
- //
- // 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)
- {
- return DoCompatibleTest (ec, delegate_type, false);
}
- Expression DoCompatibleTest (EmitContext ec, Type delegate_type, bool clone)
+ protected override ParametersCompiled ResolveParameters (ResolveContext ec, TypeInferenceContext tic, TypeSpec delegateType)
{
- if (anonymous != null)
- return anonymous.AnonymousDelegate;
-
- if (CompatibleChecks (ec, delegate_type) == null)
+ if (!delegateType.IsDelegate)
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);
+ AParametersCollection d_params = Delegate.GetParameters (ec.Compiler, delegateType);
- //
- // The lambda expression is compatible with the delegate type,
- // provided that:
- //
+ if (HasExplicitParameters) {
+ if (!VerifyExplicitParameters (ec, delegateType, d_params))
+ 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 (ec, 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))
+ TypeSpec [] ptypes = new TypeSpec [Parameters.Count];
+ for (int i = 0; i < d_params.Count; i++) {
+ // D has no ref or out parameters
+ if ((d_params.FixedParameters [i].ModFlags & Parameter.Modifier.ISBYREF) != 0)
return null;
- } else {
- //
- // If L has an implicitly typed parameter list, D has no ref or
- // out parameters
- //
- // Note: We currently do nothing, because the preview does not
- // follow the above rule.
+
+ TypeSpec d_param = d_params.Types [i];
//
- // each parameter of L is given the type of the corresponding parameter in D
+ // When type inference context exists try to apply inferred type arguments
//
+ if (tic != null) {
+ d_param = tic.InflateGenericArgument (d_param);
+ }
- for (int i = 0; i < invoke_pd.Count; i++)
- Parameters [i].TypeName = new TypeExpression (
- invoke_pd.ParameterType (i),
- Parameters [i].Location);
+ ptypes [i] = d_param;
+ ImplicitLambdaParameter ilp = (ImplicitLambdaParameter) Parameters.FixedParameters [i];
+ ilp.Type = d_param;
+ ilp.Resolve (null, i);
}
+ Parameters.Types = ptypes;
+ return Parameters;
+ }
+
+ protected override Expression DoResolve (ResolveContext 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 (HasExplicitParameters) {
+ if (!Parameters.Resolve (ec))
+ return null;
+ }
- ToplevelBlock b = clone ? (ToplevelBlock) Block.PerformClone () : Block;
-
- anonymous = new AnonymousMethod (
- Parent != null ? Parent.AnonymousMethod : null, RootScope, Host,
- GenericMethod, Parameters, Container, b, invoke_mb.ReturnType,
- delegate_type, loc);
+ eclass = ExprClass.Value;
+ type = InternalType.AnonymousMethod;
+ return this;
+ }
+
+ protected override AnonymousMethodBody CompatibleMethodFactory (TypeSpec returnType, TypeSpec delegateType, ParametersCompiled p, ToplevelBlock b)
+ {
+ return new LambdaMethod (p, b, returnType, delegateType, loc);
+ }
+
+ public override string GetSignatureForError ()
+ {
+ return "lambda expression";
+ }
+ }
+
+ public class LambdaMethod : AnonymousMethodBody
+ {
+ public LambdaMethod (ParametersCompiled parameters,
+ ToplevelBlock block, TypeSpec return_type, TypeSpec delegate_type,
+ Location loc)
+ : base (parameters, block, return_type, delegate_type, loc)
+ {
+ }
+
+ protected override void CloneTo (CloneContext clonectx, Expression target)
+ {
+ // TODO: nothing ??
+ }
+
+ public override string ContainerType {
+ get {
+ return "lambda expression";
+ }
+ }
- if (!anonymous.Resolve (ec))
+ public override Expression CreateExpressionTree (ResolveContext ec)
+ {
+ BlockContext bc = new BlockContext (ec.MemberContext, Block, ReturnType);
+ Expression args = parameters.CreateExpressionTree (bc, loc);
+ Expression expr = Block.CreateExpressionTree (ec);
+ if (expr == null)
return null;
- return anonymous.AnonymousDelegate;
+ Arguments arguments = new Arguments (2);
+ arguments.Add (new Argument (expr));
+ arguments.Add (new Argument (args));
+ return CreateExpressionFactoryCall (ec, "Lambda",
+ new TypeArguments (new TypeExpression (type, loc)),
+ arguments);
}
}
// to be expressions. Depending on the return type of the delegate this will behave
// as either { expr (); return (); } or { return expr (); }
//
- public class ContextualReturn : Statement {
- public Expression Expr;
-
- public ContextualReturn (Expression e)
+ public class ContextualReturn : Return
+ {
+ ExpressionStatement statement;
+
+ public ContextualReturn (Expression expr)
+ : base (expr, expr.Location)
{
- Expr = e;
- loc = Expr.Location;
}
- bool unwind_protect;
-
- public override bool Resolve (EmitContext ec)
+ public override Expression CreateExpressionTree (ResolveContext ec)
{
- AnonymousContainer am = ec.CurrentAnonymousMethod;
- if ((am != null) && am.IsIterator && ec.InIterator) {
- Report.Error (1622, loc, "Cannot return a value from iterators. Use the yield return " +
- "statement to return a value, or yield break to end the iteration");
- return false;
- }
-
- Expr = Expr.Resolve (ec);
- if (Expr == null)
- return false;
+ return Expr.CreateExpressionTree (ec);
+ }
- if (ec.ReturnType == null){
- if (!(Expr is ExpressionStatement)){
- Expression.Error_InvalidExpressionStatement (Expr.Location);
- return false;
- }
- } else {
- if (Expr.Type != ec.ReturnType) {
- Expr = Convert.ImplicitConversionRequired (
- ec, Expr, ec.ReturnType, loc);
- if (Expr == null)
- return false;
- }
+ public override void Emit (EmitContext ec)
+ {
+ if (statement != null) {
+ statement.EmitStatement (ec);
+ ec.Emit (OpCodes.Ret);
+ return;
}
- int errors = Report.Errors;
- unwind_protect = ec.CurrentBranching.AddReturnOrigin (ec.CurrentBranching.CurrentUsageVector, loc);
- if (unwind_protect)
- ec.NeedReturnLabel ();
- ec.CurrentBranching.CurrentUsageVector.Goto ();
- return errors == Report.Errors;
+ base.Emit (ec);
}
-
- protected override void DoEmit (EmitContext ec)
+
+ protected override bool DoResolve (BlockContext ec)
{
- Expr.Emit (ec);
+ //
+ // When delegate returns void, only expression statements can be used
+ //
+ if (ec.ReturnType == TypeManager.void_type) {
+ Expr = Expr.Resolve (ec);
+ if (Expr == null)
+ return false;
- if (unwind_protect){
- ec.ig.Emit (OpCodes.Stloc, ec.TemporaryReturn ());
- ec.ig.Emit (OpCodes.Leave, ec.ReturnLabel);
- }
- ec.ig.Emit (OpCodes.Ret);
- }
+ statement = Expr as ExpressionStatement;
+ if (statement == null)
+ Expr.Error_InvalidExpressionStatement (ec);
- protected override void CloneTo (CloneContext clonectx, Statement t)
- {
- ContextualReturn cr = (ContextualReturn) t;
+ return true;
+ }
- cr.Expr = Expr.Clone (clonectx);
+ return base.DoResolve (ec);
}
}
}