Merge branch 'master' of github.com:mono/mono
[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.ConstructorBlock, 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 AnonymousMethodBody CompatibleMethodFactory (TypeSpec returnType, TypeSpec delegateType, ParametersCompiled p, ToplevelBlock b)
104                 {
105                         return new LambdaMethod (p, b, returnType, delegateType, loc);
106                 }
107
108                 protected override bool DoResolveParameters (ResolveContext rc)
109                 {
110                         //
111                         // Only explicit parameters can be resolved at this point
112                         //
113                         if (HasExplicitParameters) {
114                                 return Parameters.Resolve (rc);
115                         }
116
117                         return true;
118                 }
119
120                 public override string GetSignatureForError ()
121                 {
122                         return "lambda expression";
123                 }
124         }
125
126         class LambdaMethod : AnonymousMethodBody
127         {
128                 public LambdaMethod (ParametersCompiled parameters,
129                                         ToplevelBlock block, TypeSpec return_type, TypeSpec delegate_type,
130                                         Location loc)
131                         : base (parameters, block, return_type, delegate_type, loc)
132                 {
133                 }
134
135                 #region Properties
136
137                 public override string ContainerType {
138                         get {
139                                 return "lambda expression";
140                         }
141                 }
142
143                 #endregion
144
145                 protected override void CloneTo (CloneContext clonectx, Expression target)
146                 {
147                         // TODO: nothing ??
148                 }
149
150                 public override Expression CreateExpressionTree (ResolveContext ec)
151                 {
152                         BlockContext bc = new BlockContext (ec.MemberContext, Block, ReturnType);
153                         Expression args = parameters.CreateExpressionTree (bc, loc);
154                         Expression expr = Block.CreateExpressionTree (ec);
155                         if (expr == null)
156                                 return null;
157
158                         Arguments arguments = new Arguments (2);
159                         arguments.Add (new Argument (expr));
160                         arguments.Add (new Argument (args));
161                         return CreateExpressionFactoryCall (ec, "Lambda",
162                                 new TypeArguments (new TypeExpression (type, loc)),
163                                 arguments);
164                 }
165         }
166
167         //
168         // This is a return statement that is prepended lambda expression bodies that happen
169         // to be expressions.  Depending on the return type of the delegate this will behave
170         // as either { expr (); return (); } or { return expr (); }
171         //
172         public class ContextualReturn : Return
173         {
174                 ExpressionStatement statement;
175
176                 public ContextualReturn (Expression expr)
177                         : base (expr, expr.Location)
178                 {
179                 }
180
181                 public override Expression CreateExpressionTree (ResolveContext ec)
182                 {
183                         return Expr.CreateExpressionTree (ec);
184                 }
185
186                 public override void Emit (EmitContext ec)
187                 {
188                         if (statement != null) {
189                                 statement.EmitStatement (ec);
190                                 ec.Emit (OpCodes.Ret);
191                                 return;
192                         }
193
194                         base.Emit (ec);
195                 }
196
197                 protected override bool DoResolve (BlockContext ec)
198                 {
199                         //
200                         // When delegate returns void, only expression statements can be used
201                         //
202                         if (ec.ReturnType == TypeManager.void_type) {
203                                 Expr = Expr.Resolve (ec);
204                                 if (Expr == null)
205                                         return false;
206
207                                 statement = Expr as ExpressionStatement;
208                                 if (statement == null)
209                                         Expr.Error_InvalidExpressionStatement (ec);
210
211                                 return true;
212                         }
213
214                         return base.DoResolve (ec);
215                 }
216         }
217 }