Moved ProviderCollectionTest.cs from System assembly to System.Configuration.
[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.Collections;
14 using System.Reflection;
15 using System.Reflection.Emit;
16
17 namespace Mono.CSharp {
18         public class LambdaExpression : AnonymousMethodExpression
19         {
20                 readonly bool explicit_parameters;
21
22                 //
23                 // The parameters can either be:
24                 //    A list of Parameters (explicitly typed parameters)
25                 //    An ImplicitLambdaParameter
26                 //
27                 public LambdaExpression (Parameters parameters, Location loc)
28                         : base (parameters, loc)
29                 {
30                         if (parameters.Count > 0)
31                                 explicit_parameters = !(parameters.FixedParameters [0] is ImplicitLambdaParameter);
32                 }
33
34                 protected override Expression CreateExpressionTree (EmitContext ec, Type delegate_type)
35                 {
36                         if (ec.IsInProbingMode)
37                                 return this;
38                         
39                         Expression args = Parameters.CreateExpressionTree (ec, loc);
40                         Expression expr = Block.CreateExpressionTree (ec);
41                         if (expr == null)
42                                 return null;
43
44                         ArrayList arguments = new ArrayList (2);
45                         arguments.Add (new Argument (expr));
46                         arguments.Add (new Argument (args));
47                         return CreateExpressionFactoryCall ("Lambda",
48                                 new TypeArguments (loc, new TypeExpression (delegate_type, loc)),
49                                 arguments);
50                 }
51
52                 public override bool HasExplicitParameters {
53                         get {
54                                 return explicit_parameters;
55                         }
56                 }
57
58                 protected override Parameters ResolveParameters (EmitContext ec, TypeInferenceContext tic, Type delegateType)
59                 {
60                         if (!TypeManager.IsDelegateType (delegateType))
61                                 return null;
62
63                         AParametersCollection d_params = TypeManager.GetDelegateParameters (delegateType);
64
65                         if (explicit_parameters) {
66                                 if (!VerifyExplicitParameters (delegateType, d_params, ec.IsInProbingMode))
67                                         return null;
68
69                                 return Parameters;
70                         }
71
72                         //
73                         // If L has an implicitly typed parameter list we make implicit parameters explicit
74                         // Set each parameter of L is given the type of the corresponding parameter in D
75                         //
76                         if (!VerifyParameterCompatibility (delegateType, d_params, ec.IsInProbingMode))
77                                 return null;
78
79                         Type [] ptypes = new Type [Parameters.Count];
80                         for (int i = 0; i < d_params.Count; i++) {
81                                 // D has no ref or out parameters
82                                 if ((d_params.FixedParameters [i].ModFlags & Parameter.Modifier.ISBYREF) != 0)
83                                         return null;
84
85                                 Type d_param = d_params.Types [i];
86
87 #if MS_COMPATIBLE
88                                 // Blablabla, because reflection does not work with dynamic types
89                                 if (d_param.IsGenericParameter)
90                                         d_param = delegateType.GetGenericArguments () [d_param.GenericParameterPosition];
91 #endif
92                                 //
93                                 // When type inference context exists try to apply inferred type arguments
94                                 //
95                                 if (tic != null) {
96                                         d_param = tic.InflateGenericArgument (d_param);
97                                 }
98
99                                 ptypes [i] = d_param;
100                                 ((ImplicitLambdaParameter) Parameters.FixedParameters [i]).Type = d_param;
101                         }
102
103                         Parameters.Types = ptypes;
104                         return Parameters;
105                 }
106
107                 public override Expression DoResolve (EmitContext ec)
108                 {
109                         //
110                         // Only explicit parameters can be resolved at this point
111                         //
112                         if (explicit_parameters) {
113                                 if (!Parameters.Resolve (ec))
114                                         return null;
115                         }
116
117                         eclass = ExprClass.Value;
118                         type = TypeManager.anonymous_method_type;                                               
119                         return this;
120                 }
121
122                 protected override AnonymousMethodBody CompatibleMethodFactory (Type returnType, Type delegateType, Parameters p, ToplevelBlock b)
123                 {
124                         return new LambdaMethod (p, b, returnType, delegateType, loc);
125                 }
126
127                 public override string GetSignatureForError ()
128                 {
129                         return "lambda expression";
130                 }
131         }
132
133         public class LambdaMethod : AnonymousMethodBody
134         {
135                 public LambdaMethod (Parameters parameters,
136                                         ToplevelBlock block, Type return_type, Type delegate_type,
137                                         Location loc)
138                         : base (parameters, block, return_type, delegate_type, loc)
139                 {
140                 }
141
142                 public override string ContainerType {
143                         get {
144                                 return "lambda expression";
145                         }
146                 }
147
148                 public override Expression CreateExpressionTree (EmitContext ec)
149                 {
150                         Expression args = parameters.CreateExpressionTree (ec, loc);
151                         Expression expr = Block.CreateExpressionTree (ec);
152                         if (expr == null)
153                                 return null;
154
155                         ArrayList arguments = new ArrayList (2);
156                         arguments.Add (new Argument (expr));
157                         arguments.Add (new Argument (args));
158                         return CreateExpressionFactoryCall ("Lambda",
159                                 new TypeArguments (loc, new TypeExpression (type, loc)),
160                                 arguments);
161                 }
162         }
163
164         //
165         // This is a return statement that is prepended lambda expression bodies that happen
166         // to be expressions.  Depending on the return type of the delegate this will behave
167         // as either { expr (); return (); } or { return expr (); }
168         //
169         public class ContextualReturn : Return
170         {
171                 public ContextualReturn (Expression expr)
172                         : base (expr, expr.Location)
173                 {
174                 }
175
176                 public override Expression CreateExpressionTree (EmitContext ec)
177                 {
178                         return Expr.CreateExpressionTree (ec);
179                 }
180
181                 public override void Emit (EmitContext ec)
182                 {
183                         if (ec.ReturnType == TypeManager.void_type) {
184                                 ((ExpressionStatement) Expr).EmitStatement (ec);
185                                 ec.ig.Emit (OpCodes.Ret);
186                                 return;
187                         }
188
189                         base.Emit (ec);
190                 }
191
192                 protected override bool DoResolve (EmitContext ec)
193                 {       
194                         //
195                         // When delegate returns void, only expression statements can be used
196                         //
197                         if (ec.ReturnType == TypeManager.void_type) {
198                                 Expr = Expr.Resolve (ec);
199                                 if (Expr == null)
200                                         return false;
201                                 
202                                 if (Expr is ExpressionStatement)
203                                         return true;
204                                 
205                                 Expr.Error_InvalidExpressionStatement ();
206                                 return false;
207                         }
208
209                         return base.DoResolve (ec);
210                 }
211         }
212 }