2008-07-22 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / lambda.cs
index a767c3e2e1dd5fbe40dcc0bf5f26c08f943c62ad..b11dc6000e254d2c8c6beec1f9ce9445c9891e56 100644 (file)
@@ -2,10 +2,11 @@
 // 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;
@@ -14,158 +15,153 @@ using System.Reflection;
 using System.Reflection.Emit;
 
 namespace Mono.CSharp {
-       public class LambdaExpression : AnonymousMethodExpression {
-               bool explicit_parameters;
+       public class LambdaExpression : AnonymousMethodExpression
+       {
+               readonly bool explicit_parameters;
 
                //
-               // 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 (TypeContainer host, Parameters parameters, Location loc)
+                       : base (host, parameters, loc)
                {
-                       explicit_parameters = (parameters != null && parameters.Count > 0 && parameters [0].TypeName != null);
-                       if (parameters == null)
-                               Parameters = new Parameters (new Parameter [0]);
+                       if (parameters.Count > 0)
+                               explicit_parameters = !(parameters.FixedParameters [0] is ImplicitLambdaParameter);
                }
 
-               public bool HasImplicitParameters {
-                       get {
-                               return !explicit_parameters;
-                       }
+               protected override Expression CreateExpressionTree (EmitContext ec, Type delegate_type)
+               {
+                       if (ec.IsInProbingMode)
+                               return this;
+                       
+                       Expression args = Parameters.CreateExpressionTree (ec, loc);
+                       Expression expr = Block.CreateExpressionTree (ec);
+                       if (expr == null)
+                               return null;
+
+                       ArrayList arguments = new ArrayList (2);
+                       arguments.Add (new Argument (expr));
+                       arguments.Add (new Argument (args));
+                       return CreateExpressionFactoryCall ("Lambda",
+                               new TypeArguments (loc, new TypeExpression (delegate_type, loc)),
+                               arguments);
                }
 
-               public bool HasExplicitParameters {
+               public override bool HasExplicitParameters {
                        get {
                                return explicit_parameters;
                        }
                }
-               
-               public override Expression DoResolve (EmitContext ec)
+
+               protected override Parameters ResolveParameters (EmitContext ec, TypeInferenceContext tic, Type delegateType)
                {
-                       eclass = ExprClass.Value;
-                       type = TypeManager.anonymous_method_type;
+                       if (!TypeManager.IsDelegateType (delegateType))
+                               return null;
 
-                       if (explicit_parameters){
-                               if (!Parameters.Resolve (ec))
+                       ParameterData d_params = TypeManager.GetDelegateParameters (delegateType);
+
+                       if (explicit_parameters) {
+                               if (!VerifyExplicitParameters (delegateType, d_params, ec.IsInProbingMode))
                                        return null;
+
+                               return Parameters;
                        }
-                       
-                       // We will resolve parameters later, we do not
-                       // have information at this point.
-                       
-                       return this;
-               }
 
-               public override void Emit (EmitContext ec)
-               {
-                       base.Emit (ec);
+                       //
+                       // 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 (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;
+
+                               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);
+                               }
+
+                               Parameters.Types [i] = Parameters.FixedParameters[i].ParameterType = d_param;
+                       }
+                       return Parameters;
                }
 
-               public override bool ImplicitStandardConversionExists (Type delegate_type)
+               public override Expression DoResolve (EmitContext ec)
                {
-                       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 ();
+                       //
+                       // Only explicit parameters can be resolved at this point
+                       //
+                       if (explicit_parameters) {
+                               if (!Parameters.Resolve (ec))
+                                       return null;
                        }
-                       
-                       // Ignore the result
-                       anonymous = null;
 
-                       return result;
+                       eclass = ExprClass.Value;
+                       type = TypeManager.anonymous_method_type;                                               
+                       return this;
                }
-               
-               //
-               // 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)
+
+               protected override AnonymousMethodBody CompatibleMethodFactory (Type returnType, Type delegateType, Parameters p, ToplevelBlock b)
                {
-                       return DoCompatibleTest (ec, delegate_type, false);
+                       return new LambdaMethod (Host,
+                               p, b, returnType,
+                               delegateType, loc);
                }
 
-               Expression DoCompatibleTest (EmitContext ec, Type delegate_type, bool clone)
+               public override string GetSignatureForError ()
                {
-                       if (anonymous != null)
-                               return anonymous.AnonymousDelegate;
-
-                       if (CompatibleChecks (ec, delegate_type) == null)
-                               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);
-
-                       //
-                       // The lambda expression is compatible with the delegate type,
-                       // provided that:
-                       //
+                       return "lambda expression";
+               }
+       }
 
-                       //
-                       // D and L have the same number of arguments.
-                       if (Parameters.Count != invoke_pd.Count)
-                               return null;
+       public class LambdaMethod : AnonymousMethodBody
+       {
+               public LambdaMethod (TypeContainer host, Parameters parameters,
+                                       ToplevelBlock block, Type return_type, Type delegate_type,
+                                       Location loc)
+                       : base (host, parameters, block, return_type, delegate_type, loc)
+               {
+               }
 
-                       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))
-                                       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.
-
-                               //
-                               // each parameter of L is given the type of the corresponding parameter in D
-                               //
-
-                               for (int i = 0; i < invoke_pd.Count; i++)
-                                       Parameters [i].TypeName = new TypeExpression (
-                                               invoke_pd.ParameterType (i),
-                                               Parameters [i].Location);
+               public override string ContainerType {
+                       get {
+                               return "lambda expression";
                        }
+               }
 
+               public override Expression CreateExpressionTree (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.
+                       // Remove IL method implementation when expression tree is requested
                        //
+                       method.Parent.PartialContainer.RemoveMethod (method);
 
-                       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);
-
-                       if (!anonymous.Resolve (ec))
+                       Expression args = parameters.CreateExpressionTree (ec, loc);
+                       Expression expr = Block.CreateExpressionTree (ec);
+                       if (expr == null)
                                return null;
 
-                       return anonymous.AnonymousDelegate;
+                       ArrayList arguments = new ArrayList (2);
+                       arguments.Add (new Argument (expr));
+                       arguments.Add (new Argument (args));
+                       return CreateExpressionFactoryCall ("Lambda",
+                               new TypeArguments (loc, new TypeExpression (type, loc)),
+                               arguments);
                }
        }
 
@@ -174,68 +170,47 @@ namespace Mono.CSharp {
        // 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
+       {
+               public ContextualReturn (Expression expr)
+                       : base (expr, expr.Location)
                {
-                       Expr = e;
-                       loc = Expr.Location;
                }
 
-               bool unwind_protect;
+               public override Expression CreateExpressionTree (EmitContext ec)
+               {
+                       return Expr.CreateExpressionTree (ec);
+               }
 
-               public override bool Resolve (EmitContext ec)
+               public override void Emit (EmitContext 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;
+                       if (ec.ReturnType == TypeManager.void_type) {
+                               ((ExpressionStatement) Expr).EmitStatement (ec);
+                               ec.ig.Emit (OpCodes.Ret);
+                               return;
                        }
 
-                       Expr = Expr.Resolve (ec);
-                       if (Expr == null)
-                               return false;
+                       base.Emit (ec);
+               }
 
-                       if (ec.ReturnType == null){
-                               if (!(Expr is ExpressionStatement)){
-                                       Expression.Error_InvalidExpressionStatement (Expr.Location);
+               protected override bool DoResolve (EmitContext 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;
-                               }
-                       } else {
-                               if (Expr.Type != ec.ReturnType) {
-                                       Expr = Convert.ImplicitConversionRequired (
-                                               ec, Expr, ec.ReturnType, loc);
-                                       if (Expr == null)
-                                               return false;
-                               }
+                               
+                               if (Expr is ExpressionStatement)
+                                       return true;
+                               
+                               Expr.Error_InvalidExpressionStatement ();
+                               return false;
                        }
 
-                       int errors = Report.Errors;
-                       unwind_protect = ec.CurrentBranching.AddReturnOrigin (ec.CurrentBranching.CurrentUsageVector, loc);
-                       if (unwind_protect)
-                               ec.NeedReturnLabel ();
-                       ec.CurrentBranching.CurrentUsageVector.Return ();
-                       return errors == Report.Errors;
-               }
-               
-               protected override void DoEmit (EmitContext ec)
-               {
-                       Expr.Emit (ec);
-
-                       if (unwind_protect){
-                               ec.ig.Emit (OpCodes.Stloc, ec.TemporaryReturn ());
-                               ec.ig.Emit (OpCodes.Leave, ec.ReturnLabel);
-                       } 
-                       ec.ig.Emit (OpCodes.Ret);
-               }
-
-               protected override void CloneTo (CloneContext clonectx, Statement t)
-               {
-                       ContextualReturn cr = (ContextualReturn) t;
-
-                       cr.Expr = Expr.Clone (clonectx);
+                       return base.DoResolve (ec);
                }
        }
 }