New tests.
[mono.git] / mcs / mcs / lambda.cs
1 //
2 // lambda.cs: support for lambda expressions
3 //
4 // Authors: Miguel de Icaza (miguel@gnu.org)
5 //          Marek Safar (marek.safar@gmail.com)
6 //
7 // Dual licensed under the terms of the MIT X11 or GNU GPL
8 //
9 // Copyright 2007-2008 Novell, Inc
10 //
11
12 using System;
13 using System.Reflection;
14 using System.Reflection.Emit;
15
16 namespace Mono.CSharp {
17         public class LambdaExpression : AnonymousMethodExpression
18         {
19                 //
20                 // The parameters can either be:
21                 //    A list of Parameters (explicitly typed parameters)
22                 //    An ImplicitLambdaParameter
23                 //
24                 public LambdaExpression (Location loc)
25                         : base (loc)
26                 {
27                 }
28
29                 protected override Expression CreateExpressionTree (ResolveContext ec, TypeSpec delegate_type)
30                 {
31                         if (ec.IsInProbingMode)
32                                 return this;
33
34                         BlockContext bc = new BlockContext (ec.MemberContext, ec.CurrentBlock.Explicit, TypeManager.void_type) {
35                                 CurrentAnonymousMethod = ec.CurrentAnonymousMethod
36                         };
37
38                         Expression args = Parameters.CreateExpressionTree (bc, loc);
39                         Expression expr = Block.CreateExpressionTree (ec);
40                         if (expr == null)
41                                 return null;
42
43                         Arguments arguments = new Arguments (2);
44                         arguments.Add (new Argument (expr));
45                         arguments.Add (new Argument (args));
46                         return CreateExpressionFactoryCall (ec, "Lambda",
47                                 new TypeArguments (new TypeExpression (delegate_type, loc)),
48                                 arguments);
49                 }
50
51                 public override bool HasExplicitParameters {
52                         get {
53                                 return Parameters.Count > 0 && !(Parameters.FixedParameters [0] is ImplicitLambdaParameter);
54                         }
55                 }
56
57                 protected override ParametersCompiled ResolveParameters (ResolveContext ec, TypeInferenceContext tic, TypeSpec delegateType)
58                 {
59                         if (!delegateType.IsDelegate)
60                                 return null;
61
62                         AParametersCollection d_params = Delegate.GetParameters (ec.Compiler, delegateType);
63
64                         if (HasExplicitParameters) {
65                                 if (!VerifyExplicitParameters (ec, delegateType, d_params))
66                                         return null;
67
68                                 return Parameters;
69                         }
70
71                         //
72                         // If L has an implicitly typed parameter list we make implicit parameters explicit
73                         // Set each parameter of L is given the type of the corresponding parameter in D
74                         //
75                         if (!VerifyParameterCompatibility (ec, delegateType, d_params, ec.IsInProbingMode))
76                                 return null;
77
78                         TypeSpec [] ptypes = new TypeSpec [Parameters.Count];
79                         for (int i = 0; i < d_params.Count; i++) {
80                                 // D has no ref or out parameters
81                                 if ((d_params.FixedParameters [i].ModFlags & Parameter.Modifier.ISBYREF) != 0)
82                                         return null;
83
84                                 TypeSpec d_param = d_params.Types [i];
85
86                                 //
87                                 // When type inference context exists try to apply inferred type arguments
88                                 //
89                                 if (tic != null) {
90                                         d_param = tic.InflateGenericArgument (d_param);
91                                 }
92
93                                 ptypes [i] = d_param;
94                                 ImplicitLambdaParameter ilp = (ImplicitLambdaParameter) Parameters.FixedParameters [i];
95                                 ilp.Type = d_param;
96                                 ilp.Resolve (null, i);
97                         }
98
99                         Parameters.Types = ptypes;
100                         return Parameters;
101                 }
102
103                 protected override Expression DoResolve (ResolveContext ec)
104                 {
105                         //
106                         // Only explicit parameters can be resolved at this point
107                         //
108                         if (HasExplicitParameters) {
109                                 if (!Parameters.Resolve (ec))
110                                         return null;
111                         }
112
113                         eclass = ExprClass.Value;
114                         type = InternalType.AnonymousMethod;
115                         return this;
116                 }
117
118                 protected override AnonymousMethodBody CompatibleMethodFactory (TypeSpec returnType, TypeSpec delegateType, ParametersCompiled p, ToplevelBlock b)
119                 {
120                         return new LambdaMethod (p, b, returnType, delegateType, loc);
121                 }
122
123                 public override string GetSignatureForError ()
124                 {
125                         return "lambda expression";
126                 }
127         }
128
129         public class LambdaMethod : AnonymousMethodBody
130         {
131                 public LambdaMethod (ParametersCompiled parameters,
132                                         ToplevelBlock block, TypeSpec return_type, TypeSpec delegate_type,
133                                         Location loc)
134                         : base (parameters, block, return_type, delegate_type, loc)
135                 {
136                 }
137
138                 protected override void CloneTo (CloneContext clonectx, Expression target)
139                 {
140                         // TODO: nothing ??
141                 }
142
143                 public override string ContainerType {
144                         get {
145                                 return "lambda expression";
146                         }
147                 }
148
149                 public override Expression CreateExpressionTree (ResolveContext ec)
150                 {
151                         BlockContext bc = new BlockContext (ec.MemberContext, Block, ReturnType);
152                         Expression args = parameters.CreateExpressionTree (bc, loc);
153                         Expression expr = Block.CreateExpressionTree (ec);
154                         if (expr == null)
155                                 return null;
156
157                         Arguments arguments = new Arguments (2);
158                         arguments.Add (new Argument (expr));
159                         arguments.Add (new Argument (args));
160                         return CreateExpressionFactoryCall (ec, "Lambda",
161                                 new TypeArguments (new TypeExpression (type, loc)),
162                                 arguments);
163                 }
164         }
165
166         //
167         // This is a return statement that is prepended lambda expression bodies that happen
168         // to be expressions.  Depending on the return type of the delegate this will behave
169         // as either { expr (); return (); } or { return expr (); }
170         //
171         public class ContextualReturn : Return
172         {
173                 ExpressionStatement statement;
174
175                 public ContextualReturn (Expression expr)
176                         : base (expr, expr.Location)
177                 {
178                 }
179
180                 public override Expression CreateExpressionTree (ResolveContext ec)
181                 {
182                         return Expr.CreateExpressionTree (ec);
183                 }
184
185                 public override void Emit (EmitContext ec)
186                 {
187                         if (statement != null) {
188                                 statement.EmitStatement (ec);
189                                 ec.Emit (OpCodes.Ret);
190                                 return;
191                         }
192
193                         base.Emit (ec);
194                 }
195
196                 protected override bool DoResolve (BlockContext ec)
197                 {
198                         //
199                         // When delegate returns void, only expression statements can be used
200                         //
201                         if (ec.ReturnType == TypeManager.void_type) {
202                                 Expr = Expr.Resolve (ec);
203                                 if (Expr == null)
204                                         return false;
205
206                                 statement = Expr as ExpressionStatement;
207                                 if (statement == null)
208                                         Expr.Error_InvalidExpressionStatement (ec);
209
210                                 return true;
211                         }
212
213                         return base.DoResolve (ec);
214                 }
215         }
216 }