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