Author: miguel
Date: 2007-02-11 15:30:59 -0500 (Sun, 11 Feb 2007)
New Revision: 72630

Added:
   trunk/mcs/tests/ltest-1.cs
Modified:
   trunk/mcs/gmcs/ChangeLog
   trunk/mcs/gmcs/Makefile
   trunk/mcs/gmcs/cs-parser.jay
   trunk/mcs/mcs/ChangeLog
   trunk/mcs/mcs/cs-parser.jay
   trunk/mcs/mcs/ecore.cs
   trunk/mcs/mcs/expression.cs
   trunk/mcs/mcs/lambda.cs
Log:
Lambda expression support work:

        * Explicit typed parameters,
        * Block bodies
        * Expression bodies

All rules of compatibility have been implemented, this only works currently
for *one* probe of lambda expressions (no support for overload resolution yet).


2007-02-11  Miguel de Icaza  <[EMAIL PROTECTED]>

        * lambda.cs (LambdaExpression.Compatible): Remove some early code,
        now all the heavy lifting to check that embedded statements or
        expressions have the right form is done in the ContextualReturn.

        (ContextualReturn): New class.  

        * ecore.cs (Error_InvalidExpressionStatement): Make a helper
        method that can be invoked to report 201, so we do not replicate
        this everywhere.

        * cs-parser.jay: Reuse Error_InvalidExpressionStatement.
        
        * cs-tokenizer.cs (xtoken): Correctly compute the column, it was
        treating tabs as spaces. 

2007-02-11  Miguel de Icaza  <[EMAIL PROTECTED]>

        * cs-parser.jay (lambda_expression_body): when the body is an
        expression add a statement of the form:

                contextual-return expression.

        Where `contextual-return' is similar to `return', the difference
        being that if the delegate that the lambda will be converted to
        has a void return type, it will check that the result is a
        ExpressionStatement and the result is a plain ret (no return
        values on the stack).  If the return type of the delegate is of a
        given type, this turns into a return with a value and does the
        regular checking to check that the computed value can be
        implicitly converted to the delegate return.




Modified: trunk/mcs/gmcs/ChangeLog
===================================================================
--- trunk/mcs/gmcs/ChangeLog    2007-02-11 17:24:37 UTC (rev 72629)
+++ trunk/mcs/gmcs/ChangeLog    2007-02-11 20:30:59 UTC (rev 72630)
@@ -1,3 +1,19 @@
+2007-02-11  Miguel de Icaza  <[EMAIL PROTECTED]>
+
+       * cs-parser.jay (lambda_expression_body): when the body is an
+       expression add a statement of the form:
+
+               contextual-return expression.
+
+       Where `contextual-return' is similar to `return', the difference
+       being that if the delegate that the lambda will be converted to
+       has a void return type, it will check that the result is a
+       ExpressionStatement and the result is a plain ret (no return
+       values on the stack).  If the return type of the delegate is of a
+       given type, this turns into a return with a value and does the
+       regular checking to check that the computed value can be
+       implicitly converted to the delegate return.
+
 2007-01-30  Miguel de Icaza  <[EMAIL PROTECTED]>
 
        * cs-parser.jay (anonymous_method_expression): move the

Modified: trunk/mcs/gmcs/Makefile
===================================================================
--- trunk/mcs/gmcs/Makefile     2007-02-11 17:24:37 UTC (rev 72629)
+++ trunk/mcs/gmcs/Makefile     2007-02-11 20:30:59 UTC (rev 72630)
@@ -100,4 +100,8 @@
        mono gmcs.exe --typetest ../mcs/lambda.test
 
 l:
-       mono --debug gmcs.exe -langversion:linq l1.cs
\ No newline at end of file
+       mono --debug gmcs.exe -langversion:linq l1.cs
+
+qh:
+       make cs-parser.cs
+       MONO_PATH="../class/lib/net_2_0_bootstrap:$MONO_PATH" 
/cvs/mono/runtime/mono-wrapper  ../class/lib/net_2_0_bootstrap/mcs.exe  
/codepage:65001 -d:GMCS_SOURCE  -d:NET_1_1 -d:NET_2_0 -debug -target:exe 
-out:gmcs.exe cs-parser.cs  @gmcs.exe.sources

Modified: trunk/mcs/gmcs/cs-parser.jay
===================================================================
--- trunk/mcs/gmcs/cs-parser.jay        2007-02-11 17:24:37 UTC (rev 72629)
+++ trunk/mcs/gmcs/cs-parser.jay        2007-02-11 20:30:59 UTC (rev 72630)
@@ -3908,12 +3908,14 @@
          }
          expression 
          {
+               Block b = end_block (lexer.Location);
                oob_stack.Push ($2);
-               $$ = end_block (lexer.Location);
+               b.AddStatement (new ContextualReturn ((Expression) $2));
+               $$ = b;
          } 
        | block { 
+               $$ = $1; 
                oob_stack.Push (null);
-               $$ = $1; 
          } 
        ;
 
@@ -5535,6 +5537,10 @@
        parsing_anonymous_method = true;
 }
 
+/*
+ * Completes the anonymous method processing, if lambda_expr is null, this
+ * means that we have a Statement instead of an Expression embedded 
+ */
 AnonymousMethodExpression 
 end_anonymous (ToplevelBlock anon_block, Expression lambda_expr, Location loc)
 {

Modified: trunk/mcs/mcs/ChangeLog
===================================================================
--- trunk/mcs/mcs/ChangeLog     2007-02-11 17:24:37 UTC (rev 72629)
+++ trunk/mcs/mcs/ChangeLog     2007-02-11 20:30:59 UTC (rev 72630)
@@ -1,5 +1,17 @@
 2007-02-11  Miguel de Icaza  <[EMAIL PROTECTED]>
 
+       * lambda.cs (LambdaExpression.Compatible): Remove some early code,
+       now all the heavy lifting to check that embedded statements or
+       expressions have the right form is done in the ContextualReturn.
+
+       (ContextualReturn): New class.  
+
+       * ecore.cs (Error_InvalidExpressionStatement): Make a helper
+       method that can be invoked to report 201, so we do not replicate
+       this everywhere.
+
+       * cs-parser.jay: Reuse Error_InvalidExpressionStatement.
+       
        * cs-tokenizer.cs (xtoken): Correctly compute the column, it was
        treating tabs as spaces. 
 

Modified: trunk/mcs/mcs/cs-parser.jay
===================================================================
--- trunk/mcs/mcs/cs-parser.jay 2007-02-11 17:24:37 UTC (rev 72629)
+++ trunk/mcs/mcs/cs-parser.jay 2007-02-11 20:30:59 UTC (rev 72630)
@@ -3938,7 +3938,7 @@
                Expression expr = (Expression) $1;
                ExpressionStatement s = expr as ExpressionStatement;
                if (s == null) {
-                       Report.Error (201, expr.Location, "Only assignment, 
call, increment, decrement, and new object expressions can be used as a 
statement");
+                       expr.Error_InvalidExpressionStatement ();
                        $$ = null;
                }
                $$ = new StatementExpression (s);
@@ -4773,7 +4773,7 @@
        if (expr is Invocation){
                Report.Error (1002, expr.Location, "Expecting `;'");
        } else {
-               Report.Error (201, expr.Location, "Only assignment, call, 
increment, decrement, and new object expressions can be used as a statement");
+               expr.Error_InvalidExpressionStatement ();
        }
 }
 

Modified: trunk/mcs/mcs/ecore.cs
===================================================================
--- trunk/mcs/mcs/ecore.cs      2007-02-11 17:24:37 UTC (rev 72629)
+++ trunk/mcs/mcs/ecore.cs      2007-02-11 20:30:59 UTC (rev 72630)
@@ -302,6 +302,17 @@
 
                }
 
+               public static void Error_InvalidExpressionStatement (Location 
loc)
+               {
+                       Report.Error (201, loc, "Only assignment, call, 
increment, decrement and new object " +
+                                      "expressions can be used as a 
statement");
+               }
+               
+               public void Error_InvalidExpressionStatement ()
+               {
+                       Error_InvalidExpressionStatement (loc);
+               }
+
                protected void Error_CannotAssign (string to, string roContext)
                {
                        Report.Error (1656, loc, "Cannot assign to `{0}' 
because it is a `{1}'",
@@ -1157,8 +1168,7 @@
 
                        ExpressionStatement es = e as ExpressionStatement;
                        if (es == null)
-                               Error (201, "Only assignment, call, increment, 
decrement and new object " +
-                                      "expressions can be used as a 
statement");
+                               Error_InvalidExpressionStatement ();
 
                        return es;
                }

Modified: trunk/mcs/mcs/expression.cs
===================================================================
--- trunk/mcs/mcs/expression.cs 2007-02-11 17:24:37 UTC (rev 72629)
+++ trunk/mcs/mcs/expression.cs 2007-02-11 20:30:59 UTC (rev 72630)
@@ -5447,12 +5447,6 @@
                        return invocation.Resolve (ec);
                }
 
-               void error201 ()
-               {
-                       Error (201, "Only assignment, call, increment, 
decrement and new object " +
-                              "expressions can be used as a statement");
-               }
-
                public override ExpressionStatement ResolveStatement 
(EmitContext ec)
                {
                        //
@@ -5460,7 +5454,7 @@
                        //
                        TypeExpr te = expr.ResolveAsTypeTerminal (ec, true);
                        if ((te != null) && (te.eclass == ExprClass.Type)) {
-                               error201 ();
+                               Error_InvalidExpressionStatement ();
                                return null;
                        }
 
@@ -5470,7 +5464,7 @@
                        //
                        expr = expr.Resolve (ec, ResolveFlags.Type | 
ResolveFlags.VariableOrValue);
                        if ((expr == null) || (expr.eclass == ExprClass.Type)) {
-                               error201 ();
+                               Error_InvalidExpressionStatement ();
                                return null;
                        }
 

Modified: trunk/mcs/mcs/lambda.cs
===================================================================
--- trunk/mcs/mcs/lambda.cs     2007-02-11 17:24:37 UTC (rev 72629)
+++ trunk/mcs/mcs/lambda.cs     2007-02-11 20:30:59 UTC (rev 72630)
@@ -11,6 +11,7 @@
 using System;
 using System.Collections;
 using System.Reflection;
+using System.Reflection.Emit;
 
 namespace Mono.CSharp {
        public class LambdaExpression : AnonymousMethodExpression {
@@ -121,42 +122,87 @@
                                                invoke_pd.ParameterType (i),
                                                parameters_copy [i].Location);
                        }
-                       
-                               
-                       if (invoke_mb.ReturnType == TypeManager.void_type){
-                               
-                       }
-                       
-                       if (lambda_expr == null){
-                       } else {
-                       }
 
-#if false
                        //
-                       // Second: the return type of the delegate must be 
compatible with 
+                       // 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.
                        //
 
-                       //MethodBuilder builder = method_data.MethodBuilder;
-                       //ILGenerator ig = builder.GetILGenerator ();
-
-                       Report.Debug (64, "COMPATIBLE", this, Parent, 
GenericMethod, Host,
-                                     Container, Block, invoke_mb.ReturnType, 
delegate_type,
-                                     TypeManager.IsGenericType 
(delegate_type), loc);
-
                        anonymous = new AnonymousMethod (
                                Parent != null ? Parent.AnonymousMethod : null, 
RootScope, Host,
-                               GenericMethod, parameters, Container, Block, 
invoke_mb.ReturnType,
+                               GenericMethod, parameters_copy, Container, 
Block, invoke_mb.ReturnType,
                                delegate_type, loc);
 
                        if (!anonymous.Resolve (ec))
                                return null;
 
                        return anonymous.AnonymousDelegate;
-#endif
-                       return null;
                }
        }
+
+       //
+       // 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 : Statement {
+               public Expression Expr;
+               
+               public ContextualReturn (Expression e)
+               {
+                       Expr = e;
+                       loc = Expr.Location;
+               }
+
+               bool unwind_protect;
+
+               public override bool Resolve (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;
+                       }
+
+                       Expr = Expr.Resolve (ec);
+                       if (Expr == null)
+                               return false;
+
+                       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;
+                               }
+                       }
+
+                       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);
+               }
+               
+       }
 }

Added: trunk/mcs/tests/ltest-1.cs
===================================================================
--- trunk/mcs/tests/ltest-1.cs  2007-02-11 17:24:37 UTC (rev 72629)
+++ trunk/mcs/tests/ltest-1.cs  2007-02-11 20:30:59 UTC (rev 72630)
@@ -0,0 +1,61 @@
+//
+// Lambda expression test, basics.
+//
+using System;
+
+delegate int IntFunc (int x);
+delegate void VoidFunc (int x);
+
+class X {
+
+       static IntFunc func, increment;
+       static VoidFunc nothing;
+       
+       static int Main ()
+       {
+               int y = 0;
+               int r;
+               
+               //
+               // The following tests body-style lambda
+               //
+               increment = (int x) => { return x + 1; };
+               r = increment (4);
+               Console.WriteLine ("Shoudl be 5={0}", r);
+               if (r != 5)
+                       return 1;
+
+               //
+               // This tests the body of a lambda being an expression
+               //
+               func = (int x) => x + 1;
+               r = func (10);
+               Console.WriteLine ("Should be 11={0}", r);
+               if (r != 11)
+                       return 2;
+               
+               //
+               // The following tests that the body is a statement
+               //
+               nothing = (int x) => { y = x; };
+               nothing (10);
+               Console.WriteLine ("Should be 10={0}", y);
+               if (y != 10)
+                       return 3;
+
+               nothing = (int x) => { new X (x); };
+               nothing (314);
+               if (instantiated_value != 314)
+                       return 4;
+               
+               Console.WriteLine ("All tests pass");
+               return 0;
+       }
+
+       static int instantiated_value;
+
+       X (int v)
+       {
+               instantiated_value = v;
+       }
+}


Property changes on: trunk/mcs/tests/ltest-1.cs
___________________________________________________________________
Name: svn:eol-style
   + native

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to