[xbuild] Implement FileLogger . Fix #676650 .
[mono.git] / mcs / mcs / lambda.cs
index d9766356dd30065ede23826cda3b39c34f7a2989..0b1f769a585389d3316583b557412433a2b3b2e5 100644 (file)
@@ -4,69 +4,67 @@
 // 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;
+#if STATIC
+using IKVM.Reflection.Emit;
+#else
 using System.Reflection.Emit;
+#endif
 
 namespace Mono.CSharp {
        public class LambdaExpression : AnonymousMethodExpression
        {
-               readonly bool explicit_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)
                {
-                       if (parameters.Count > 0)
-                               explicit_parameters = !(parameters.FixedParameters [0] is ImplicitLambdaParameter);
                }
 
-               protected override Expression CreateExpressionTree (EmitContext ec, Type delegate_type)
+               protected override Expression CreateExpressionTree (ResolveContext ec, TypeSpec delegate_type)
                {
                        if (ec.IsInProbingMode)
                                return this;
-                       
-                       Expression args = Parameters.CreateExpressionTree (ec, loc);
+
+                       BlockContext bc = new BlockContext (ec.MemberContext, ec.ConstructorBlock, ec.BuildinTypes.Void) {
+                               CurrentAnonymousMethod = ec.CurrentAnonymousMethod
+                       };
+
+                       Expression args = Parameters.CreateExpressionTree (bc, loc);
                        Expression expr = Block.CreateExpressionTree (ec);
                        if (expr == null)
                                return null;
 
-                       ArrayList arguments = new ArrayList (2);
+                       Arguments arguments = new Arguments (2);
                        arguments.Add (new Argument (expr));
                        arguments.Add (new Argument (args));
-                       return CreateExpressionFactoryCall ("Lambda",
-                               new TypeArguments (loc, new TypeExpression (delegate_type, loc)),
+                       return CreateExpressionFactoryCall (ec, "Lambda",
+                               new TypeArguments (new TypeExpression (delegate_type, loc)),
                                arguments);
                }
 
                public override bool HasExplicitParameters {
                        get {
-                               return explicit_parameters;
+                               return Parameters.Count > 0 && !(Parameters.FixedParameters [0] is ImplicitLambdaParameter);
                        }
                }
 
-               protected override Parameters ResolveParameters (EmitContext ec, TypeInferenceContext tic, Type delegateType)
+               protected override ParametersCompiled ResolveParameters (ResolveContext ec, TypeInferenceContext tic, TypeSpec delegateType)
                {
-                       if (!TypeManager.IsDelegateType (delegateType))
+                       if (!delegateType.IsDelegate)
                                return null;
 
-                       ParameterData d_params = TypeManager.GetDelegateParameters (delegateType);
+                       AParametersCollection d_params = Delegate.GetParameters (delegateType);
 
-                       if (explicit_parameters) {
-                               if (!VerifyExplicitParameters (delegateType, d_params, ec.IsInProbingMode))
+                       if (HasExplicitParameters) {
+                               if (!VerifyExplicitParameters (ec, delegateType, d_params))
                                        return null;
 
                                return Parameters;
@@ -76,54 +74,49 @@ namespace Mono.CSharp {
                        // 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))
+                       if (!VerifyParameterCompatibility (ec, delegateType, d_params, ec.IsInProbingMode))
                                return null;
 
-                       if (Parameters.Types == null)
-                               Parameters.Types = new Type [Parameters.Count];
-
+                       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.ParameterModifier (i) & Parameter.Modifier.ISBYREF) != 0)
+                               if ((d_params.FixedParameters [i].ModFlags & Parameter.Modifier.ISBYREF) != 0)
                                        return null;
 
-                               Type d_param = d_params.Types [i];
+                               TypeSpec 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
+                               //
+                               // When type inference context exists try to apply inferred type arguments
+                               //
                                if (tic != null) {
-                                       d_param = tic.InflateGenericArgument (d_param);
+                                       d_param = tic.InflateGenericArgument (ec, d_param);
                                }
 
-                               Parameters.Types [i] = Parameters.FixedParameters[i].ParameterType = d_param;
+                               ptypes [i] = d_param;
+                               ImplicitLambdaParameter ilp = (ImplicitLambdaParameter) Parameters.FixedParameters [i];
+                               ilp.SetParameterType (d_param);
+                               ilp.Resolve (null, i);
                        }
+
+                       Parameters.Types = ptypes;
                        return Parameters;
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               protected override AnonymousMethodBody CompatibleMethodFactory (TypeSpec returnType, TypeSpec delegateType, ParametersCompiled p, ParametersBlock b)
+               {
+                       return new LambdaMethod (p, b, returnType, delegateType, loc);
+               }
+
+               protected override bool DoResolveParameters (ResolveContext rc)
                {
                        //
                        // Only explicit parameters can be resolved at this point
                        //
-                       if (explicit_parameters) {
-                               if (!Parameters.Resolve (ec))
-                                       return null;
+                       if (HasExplicitParameters) {
+                               return Parameters.Resolve (rc);
                        }
 
-                       eclass = ExprClass.Value;
-                       type = TypeManager.anonymous_method_type;                                               
-                       return this;
-               }
-
-               protected override AnonymousMethod CompatibleMethodFactory (Type returnType, Type delegateType, Parameters p, ToplevelBlock b)
-               {
-                       return new LambdaMethod (RootScope, Host,
-                               GenericMethod, p, Container, b, returnType,
-                               delegateType, loc);
+                       return true;
                }
 
                public override string GetSignatureForError ()
@@ -132,23 +125,45 @@ namespace Mono.CSharp {
                }
        }
 
-       public class LambdaMethod : AnonymousMethod
+       class LambdaMethod : AnonymousMethodBody
        {
-               public LambdaMethod (RootScopeInfo root_scope,
-                                       DeclSpace host, GenericMethod generic,
-                                       Parameters parameters, Block container,
-                                       ToplevelBlock block, Type return_type, Type delegate_type,
+               public LambdaMethod (ParametersCompiled parameters,
+                                       ParametersBlock block, TypeSpec return_type, TypeSpec delegate_type,
                                        Location loc)
-                       : base (root_scope, host, generic, parameters, container, block,
-                               return_type, delegate_type, loc)
+                       : base (parameters, block, return_type, delegate_type, loc)
                {
                }
 
+               #region Properties
+
                public override string ContainerType {
                        get {
                                return "lambda expression";
                        }
                }
+
+               #endregion
+
+               protected override void CloneTo (CloneContext clonectx, Expression target)
+               {
+                       // TODO: nothing ??
+               }
+
+               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;
+
+                       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);
+               }
        }
 
        //
@@ -158,42 +173,47 @@ namespace Mono.CSharp {
        //
        public class ContextualReturn : Return
        {
+               ExpressionStatement statement;
+
                public ContextualReturn (Expression expr)
                        : base (expr, expr.Location)
                {
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        return Expr.CreateExpressionTree (ec);
                }
 
                public override void Emit (EmitContext ec)
                {
-                       if (ec.ReturnType == TypeManager.void_type)
-                               ((ExpressionStatement) Expr).EmitStatement (ec);
-                       else
-                               base.Emit (ec);
+                       if (statement != null) {
+                               statement.EmitStatement (ec);
+                               ec.Emit (OpCodes.Ret);
+                               return;
+                       }
+
+                       base.Emit (ec);
                }
 
-               public override bool Resolve (EmitContext ec)
-               {       
+               protected override bool DoResolve (BlockContext ec)
+               {
                        //
                        // When delegate returns void, only expression statements can be used
                        //
-                       if (ec.ReturnType == TypeManager.void_type) {
+                       if (ec.ReturnType.Kind == MemberKind.Void) {
                                Expr = Expr.Resolve (ec);
                                if (Expr == null)
                                        return false;
-                               
-                               if (Expr is ExpressionStatement && (Expr is Assign || TypeManager.IsEqual (Expr.Type, ec.ReturnType)))
-                                       return true;
-                               
-                               Expr.Error_InvalidExpressionStatement ();
-                               return false;
+
+                               statement = Expr as ExpressionStatement;
+                               if (statement == null)
+                                       Expr.Error_InvalidExpressionStatement (ec);
+
+                               return true;
                        }
 
-                       return base.Resolve (ec);
+                       return base.DoResolve (ec);
                }
        }
 }