New tests.
[mono.git] / mcs / mcs / argument.cs
1 //
2 // argument.cs: Argument expressions
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximain.com)
6 //   Marek Safar (marek.safar@gmail.com)
7 //
8 // Dual licensed under the terms of the MIT X11 or GNU GPL
9 // Copyright 2003-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 {
19         //
20         // Argument expression used for invocation
21         //
22         public class Argument
23         {
24                 public enum AType : byte
25                 {
26                         Ref = 1,                        // ref modifier used
27                         Out = 2,                        // out modifier used
28                         Default = 3,            // argument created from default parameter value
29                         DynamicStatic = 4       // static argument for dynamic binding
30                 }
31
32                 public readonly AType ArgType;
33                 public Expression Expr;
34
35                 public Argument (Expression expr, AType type)
36                 {
37                         this.Expr = expr;
38                         this.ArgType = type;
39                 }
40
41                 public Argument (Expression expr)
42                 {
43                         if (expr == null)
44                                 throw new ArgumentNullException ();
45
46                         this.Expr = expr;
47                 }
48
49                 public Type Type {
50                         get { return Expr.Type; }
51                 }
52
53                 public Parameter.Modifier Modifier {
54                         get {
55                                 switch (ArgType) {
56                                 case AType.Out:
57                                         return Parameter.Modifier.OUT;
58
59                                 case AType.Ref:
60                                         return Parameter.Modifier.REF;
61
62                                 default:
63                                         return Parameter.Modifier.NONE;
64                                 }
65                         }
66                 }
67
68                 public virtual Expression CreateExpressionTree (EmitContext ec)
69                 {
70                         if (ArgType == AType.Default)
71                                 Report.Error (854, Expr.Location, "An expression tree cannot contain an invocation which uses optional parameter");
72
73                         return Expr.CreateExpressionTree (ec);
74                 }
75
76                 public string GetSignatureForError ()
77                 {
78                         if (Expr.eclass == ExprClass.MethodGroup)
79                                 return Expr.ExprClassName;
80
81                         return TypeManager.CSharpName (Expr.Type);
82                 }
83
84                 public bool IsByRef {
85                         get { return ArgType == AType.Ref || ArgType == AType.Out; }
86                 }
87
88                 public bool IsDefaultArgument {
89                         get { return ArgType == AType.Default; }
90                 }
91
92                 public bool ResolveMethodGroup (EmitContext ec)
93                 {
94                         SimpleName sn = Expr as SimpleName;
95                         if (sn != null)
96                                 Expr = sn.GetMethodGroup ();
97
98                         // FIXME: csc doesn't report any error if you try to use `ref' or
99                         //        `out' in a delegate creation expression.
100                         Expr = Expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
101                         if (Expr == null)
102                                 return false;
103
104                         return true;
105                 }
106
107                 public void Resolve (EmitContext ec)
108                 {
109                         if (Expr == EmptyExpression.Null)
110                                 return;
111
112                         using (ec.With (EmitContext.Options.DoFlowAnalysis, true)) {
113                                 // Verify that the argument is readable
114                                 if (ArgType != AType.Out)
115                                         Expr = Expr.Resolve (ec);
116
117                                 // Verify that the argument is writeable
118                                 if (Expr != null && IsByRef)
119                                         Expr = Expr.ResolveLValue (ec, EmptyExpression.OutAccess);
120
121                                 if (Expr == null)
122                                         Expr = EmptyExpression.Null;
123                         }
124                 }
125
126                 public virtual void Emit (EmitContext ec)
127                 {
128                         if (!IsByRef) {
129                                 Expr.Emit (ec);
130                                 return;
131                         }
132
133                         AddressOp mode = AddressOp.Store;
134                         if (ArgType == AType.Ref)
135                                 mode |= AddressOp.Load;
136
137                         IMemoryLocation ml = (IMemoryLocation) Expr;
138                         ParameterReference pr = ml as ParameterReference;
139
140                         //
141                         // ParameterReferences might already be references, so we want
142                         // to pass just the value
143                         //
144                         if (pr != null && pr.IsRef)
145                                 pr.EmitLoad (ec);
146                         else
147                                 ml.AddressOf (ec, mode);
148                 }
149
150                 public Argument Clone (CloneContext clonectx)
151                 {
152                         Argument a = (Argument) MemberwiseClone ();
153                         a.Expr = Expr.Clone (clonectx);
154                         return a;
155                 }
156         }
157
158         public class NamedArgument : Argument
159         {
160                 public readonly LocatedToken Name;
161                 LocalTemporary variable;
162
163                 public NamedArgument (LocatedToken name, Expression expr)
164                         : base (expr)
165                 {
166                         Name = name;
167                 }
168
169                 public override Expression CreateExpressionTree (EmitContext ec)
170                 {
171                         Report.Error (853, Name.Location, "An expression tree cannot contain named argument");
172                         return base.CreateExpressionTree (ec);
173                 }
174
175                 public override void Emit (EmitContext ec)
176                 {
177                         // TODO: Should guard against multiple emits
178                         base.Emit (ec);
179
180                         // Release temporary variable when used
181                         if (variable != null)
182                                 variable.Release (ec);
183                 }
184
185                 public void EmitAssign (EmitContext ec)
186                 {
187                         Expr.Emit (ec);
188                         variable = new LocalTemporary (Expr.Type);
189                         variable.Store (ec);
190
191                         Expr = variable;
192                 }
193         }
194
195         public class Arguments
196         {
197                 ArrayList args;                 // TODO: This should really be linked list
198                 ArrayList reordered;    // TODO: LinkedList
199
200                 public Arguments (int capacity)
201                 {
202                         args = new ArrayList (capacity);
203                 }
204
205                 public int Add (Argument arg)
206                 {
207                         return args.Add (arg);
208                 }
209
210                 public void AddRange (Arguments args)
211                 {
212                         this.args.AddRange (args.args);
213                 }
214
215                 public ArrayList CreateDynamicBinderArguments ()
216                 {
217                         ArrayList all = new ArrayList (args.Count);
218                         Location loc = Location.Null;
219
220                         MemberAccess binder = DynamicExpressionStatement.GetBinderNamespace (loc);
221
222                         foreach (Argument a in args) {
223                                 Arguments dargs = new Arguments (2);
224
225                                 // CSharpArgumentInfoFlags.None = 0
226                                 const string info_flags_enum = "CSharpArgumentInfoFlags";
227                                 Expression info_flags = new IntLiteral (0, loc);
228
229                                 if (a.Expr is Constant) {
230                                         // Any constant is emitted as a literal
231                                         info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
232                                                 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "LiteralConstant", loc));
233                                 } else if (a.ArgType == Argument.AType.Ref) {
234                                         info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
235                                                 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsRef", loc));
236                                 } else if (a.ArgType == Argument.AType.Out) {
237                                         info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
238                                                 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsOut", loc));
239                                 } else if (a.ArgType == Argument.AType.DynamicStatic) {
240                                         info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
241                                                 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsStaticType", loc));
242                                 }
243
244                                 if (!TypeManager.IsDynamicType (a.Expr.Type)) {
245                                         info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
246                                                 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc));
247                                 }
248
249                                 string named_value;
250                                 NamedArgument na = a as NamedArgument;
251                                 if (na != null) {
252                                         info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
253                                                 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "NamedArgument", loc));
254
255                                         named_value = na.Name.Value;
256                                 } else {
257                                         named_value = null;
258                                 }
259
260                                 dargs.Add (new Argument (info_flags));
261                                 dargs.Add (new Argument (new StringLiteral (named_value, loc)));
262                                 all.Add (new New (new MemberAccess (binder, "CSharpArgumentInfo", loc), dargs, loc));
263                         }
264
265                         return all;
266                 }
267
268                 public static Arguments CreateForExpressionTree (EmitContext ec, Arguments args, params Expression[] e)
269                 {
270                         Arguments all = new Arguments ((args == null ? 0 : args.Count) + e.Length);
271                         for (int i = 0; i < e.Length; ++i) {
272                                 if (e [i] != null)
273                                         all.Add (new Argument (e[i]));
274                         }
275
276                         if (args != null) {
277                                 foreach (Argument a in args.args) {
278                                         Expression tree_arg = a.CreateExpressionTree (ec);
279                                         if (tree_arg != null)
280                                                 all.Add (new Argument (tree_arg));
281                                 }
282                         }
283
284                         return all;
285                 }
286
287                 public void CheckArrayAsAttribute ()
288                 {
289                         foreach (Argument arg in args) {
290                                 // Type is undefined (was error 246)
291                                 if (arg.Type == null)
292                                         continue;
293
294                                 if (arg.Type.IsArray)
295                                         Report.Warning (3016, 1, arg.Expr.Location, "Arrays as attribute arguments are not CLS-compliant");
296                         }
297                 }
298
299                 public Arguments Clone (CloneContext ctx)
300                 {
301                         Arguments cloned = new Arguments (args.Count);
302                         foreach (Argument a in args)
303                                 cloned.Add (a.Clone (ctx));
304
305                         return cloned;
306                 }
307
308                 public int Count {
309                         get { return args.Count; }
310                 }
311
312                 //
313                 // Emits a list of resolved Arguments
314                 // 
315                 public void Emit (EmitContext ec)
316                 {
317                         Emit (ec, false, null);
318                 }
319
320                 //
321                 // if `dup_args' is true, a copy of the arguments will be left
322                 // on the stack. If `dup_args' is true, you can specify `this_arg'
323                 // which will be duplicated before any other args. Only EmitCall
324                 // should be using this interface.
325                 //
326                 public void Emit (EmitContext ec, bool dup_args, LocalTemporary this_arg)
327                 {
328                         LocalTemporary[] temps = null;
329
330                         if (dup_args && Count != 0)
331                                 temps = new LocalTemporary [Count];
332
333                         if (reordered != null && Count > 1) {
334                                 foreach (NamedArgument na in reordered)
335                                         na.EmitAssign (ec);
336                         }
337
338                         int i = 0;
339                         foreach (Argument a in args) {
340                                 a.Emit (ec);
341                                 if (dup_args) {
342                                         ec.ig.Emit (OpCodes.Dup);
343                                         (temps [i++] = new LocalTemporary (a.Type)).Store (ec);
344                                 }
345                         }
346
347                         if (dup_args) {
348                                 if (this_arg != null)
349                                         this_arg.Emit (ec);
350
351                                 for (i = 0; i < temps.Length; i++) {
352                                         temps[i].Emit (ec);
353                                         temps[i].Release (ec);
354                                 }
355                         }
356                 }
357
358                 public bool GetAttributableValue (EmitContext ec, out object[] values)
359                 {
360                         values = new object [args.Count];
361                         for (int j = 0; j < values.Length; ++j) {
362                                 Argument a = this [j];
363                                 if (!a.Expr.GetAttributableValue (ec, a.Type, out values[j]))
364                                         return false;
365                         }
366
367                         return true;
368                 }
369
370                 public IEnumerator GetEnumerator ()
371                 {
372                         return args.GetEnumerator ();
373                 }
374
375                 public void Insert (int index, Argument arg)
376                 {
377                         args.Insert (index, arg);
378                 }
379
380                 public void MarkReorderedArgument (NamedArgument a)
381                 {
382                         //
383                         // Constant expression can have no effect on left-to-right execution
384                         //
385                         if (a.Expr is Constant)
386                                 return;
387
388                         if (reordered == null)
389                                 reordered = new ArrayList ();
390
391                         reordered.Add (a);
392                 }
393
394                 //
395                 // Returns dynamic when at least one argument is of dynamic type
396                 //
397                 public void Resolve (EmitContext ec, out bool dynamic)
398                 {
399                         dynamic = false;
400                         foreach (Argument a in args) {
401                                 a.Resolve (ec);
402                                 dynamic |= TypeManager.IsDynamicType (a.Type);
403                         }
404                 }
405
406                 public void MutateHoistedGenericType (AnonymousMethodStorey storey)
407                 {
408                         foreach (Argument a in args)
409                                 a.Expr.MutateHoistedGenericType (storey);
410                 }
411
412                 public void RemoveAt (int index)
413                 {
414                         args.RemoveAt (index);
415                 }
416
417                 public Argument this [int index] {
418                         get { return (Argument) args [index]; }
419                         set { args [index] = value; }
420                 }
421         }
422 }