Add testcase for multiple missing IDs
[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.Reflection;
14 using System.Reflection.Emit;
15 using System.Collections.Generic;
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                         ExtensionType = 5,      // Instance expression inserted as the first argument
32                 }
33
34                 public readonly AType ArgType;
35                 public Expression Expr;
36
37                 public Argument (Expression expr, AType type)
38                 {
39                         this.Expr = expr;
40                         this.ArgType = type;
41                 }
42
43                 public Argument (Expression expr)
44                 {
45                         if (expr == null)
46                                 throw new ArgumentNullException ();
47
48                         this.Expr = expr;
49                 }
50
51                 #region Properties
52
53                 public bool IsByRef {
54                         get { return ArgType == AType.Ref || ArgType == AType.Out; }
55                 }
56
57                 public bool IsDefaultArgument {
58                         get { return ArgType == AType.Default; }
59                 }
60
61                 public Parameter.Modifier Modifier {
62                         get {
63                                 switch (ArgType) {
64                                 case AType.Out:
65                                         return Parameter.Modifier.OUT;
66
67                                 case AType.Ref:
68                                         return Parameter.Modifier.REF;
69
70                                 default:
71                                         return Parameter.Modifier.NONE;
72                                 }
73                         }
74                 }
75
76                 public TypeSpec Type {
77                         get { return Expr.Type; }
78                 }
79
80                 #endregion
81
82                 public Argument Clone (Expression expr)
83                 {
84                         Argument a = (Argument) MemberwiseClone ();
85                         a.Expr = expr;
86                         return a;
87                 }
88
89                 public Argument Clone (CloneContext clonectx)
90                 {
91                         return Clone (Expr.Clone (clonectx));
92                 }
93
94                 public virtual Expression CreateExpressionTree (ResolveContext ec)
95                 {
96                         if (ArgType == AType.Default)
97                                 ec.Report.Error (854, Expr.Location, "An expression tree cannot contain an invocation which uses optional parameter");
98
99                         return Expr.CreateExpressionTree (ec);
100                 }
101
102                 public string GetSignatureForError ()
103                 {
104                         if (Expr.eclass == ExprClass.MethodGroup)
105                                 return Expr.ExprClassName;
106
107                         return TypeManager.CSharpName (Expr.Type);
108                 }
109
110                 public bool ResolveMethodGroup (ResolveContext ec)
111                 {
112                         SimpleName sn = Expr as SimpleName;
113                         if (sn != null)
114                                 Expr = sn.GetMethodGroup ();
115
116                         // FIXME: csc doesn't report any error if you try to use `ref' or
117                         //        `out' in a delegate creation expression.
118                         Expr = Expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
119                         if (Expr == null)
120                                 return false;
121
122                         return true;
123                 }
124
125                 public void Resolve (ResolveContext ec)
126                 {
127                         if (Expr == EmptyExpression.Null)
128                                 return;
129
130 //                      using (ec.With (ResolveContext.Options.DoFlowAnalysis, true)) {
131                                 // Verify that the argument is readable
132                                 if (ArgType != AType.Out)
133                                         Expr = Expr.Resolve (ec);
134
135                                 // Verify that the argument is writeable
136                                 if (Expr != null && IsByRef)
137                                         Expr = Expr.ResolveLValue (ec, EmptyExpression.OutAccess.Instance);
138
139                                 if (Expr == null)
140                                         Expr = EmptyExpression.Null;
141 //                      }
142                 }
143
144                 public virtual void Emit (EmitContext ec)
145                 {
146                         if (!IsByRef) {
147                                 Expr.Emit (ec);
148                                 return;
149                         }
150
151                         AddressOp mode = AddressOp.Store;
152                         if (ArgType == AType.Ref)
153                                 mode |= AddressOp.Load;
154
155                         IMemoryLocation ml = (IMemoryLocation) Expr;
156                         ml.AddressOf (ec, mode);
157                 }
158         }
159
160         public class NamedArgument : Argument
161         {
162                 public readonly string Name;
163                 readonly Location loc;
164                 LocalTemporary variable;
165
166                 public NamedArgument (string name, Location loc, Expression expr)
167                         : this (name, loc, expr, AType.None)
168                 {
169                 }
170
171                 public NamedArgument (string name, Location loc, Expression expr, AType modifier)
172                         : base (expr, modifier)
173                 {
174                         this.Name = name;
175                         this.loc = loc;
176                 }
177
178                 public override Expression CreateExpressionTree (ResolveContext ec)
179                 {
180                         ec.Report.Error (853, loc, "An expression tree cannot contain named argument");
181                         return base.CreateExpressionTree (ec);
182                 }
183
184                 public override void Emit (EmitContext ec)
185                 {
186                         // TODO: Should guard against multiple emits
187                         base.Emit (ec);
188
189                         // Release temporary variable when used
190                         if (variable != null)
191                                 variable.Release (ec);
192                 }
193
194                 public void EmitAssign (EmitContext ec)
195                 {
196                         var type = Expr.Type;
197                         if (IsByRef) {
198                                 var ml = (IMemoryLocation) Expr;
199                                 ml.AddressOf (ec, AddressOp.Load);
200                                 type = ReferenceContainer.MakeType (type);
201                         } else {
202                                 Expr.Emit (ec);
203                         }
204
205                         variable = new LocalTemporary (type);
206                         variable.Store (ec);
207
208                         Expr = variable;
209                 }
210
211                 public Location Location {
212                         get { return loc; }
213                 }
214         }
215         
216         public class Arguments
217         {
218                 sealed class ArgumentsOrdered : Arguments
219                 {
220                         List<NamedArgument> ordered;
221
222                         public ArgumentsOrdered (Arguments args)
223                                 : base (args.Count)
224                         {
225                                 AddRange (args);
226                                 ordered = new List<NamedArgument> ();
227                         }
228
229                         public void AddOrdered (NamedArgument na)
230                         {
231                                 ordered.Add (na);
232                         }
233
234                         public override Expression[] Emit (EmitContext ec, bool dup_args)
235                         {
236                                 foreach (NamedArgument na in ordered)
237                                         na.EmitAssign (ec);
238
239                                 return base.Emit (ec, dup_args);
240                         }
241                 }
242
243                 // Try not to add any more instances to this class, it's allocated a lot
244                 List<Argument> args;
245
246                 public Arguments (int capacity)
247                 {
248                         args = new List<Argument> (capacity);
249                 }
250
251                 public void Add (Argument arg)
252                 {
253                         args.Add (arg);
254                 }
255
256                 public void AddRange (Arguments args)
257                 {
258                         this.args.AddRange (args.args);
259                 }
260
261                 public ArrayInitializer CreateDynamicBinderArguments (ResolveContext rc)
262                 {
263                         Location loc = Location.Null;
264                         var all = new ArrayInitializer (args.Count, loc);
265
266                         MemberAccess binder = DynamicExpressionStatement.GetBinderNamespace (loc);
267
268                         foreach (Argument a in args) {
269                                 Arguments dargs = new Arguments (2);
270
271                                 // CSharpArgumentInfoFlags.None = 0
272                                 const string info_flags_enum = "CSharpArgumentInfoFlags";
273                                 Expression info_flags = new IntLiteral (0, loc);
274
275                                 if (a.Expr is Constant) {
276                                         info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
277                                                 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "Constant", loc), loc);
278                                 } else if (a.ArgType == Argument.AType.Ref) {
279                                         info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
280                                                 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsRef", loc), loc);
281                                         info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
282                                                 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc), loc);
283                                 } else if (a.ArgType == Argument.AType.Out) {
284                                         info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
285                                                 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsOut", loc), loc);
286                                         info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
287                                                 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc), loc);
288                                 } else if (a.ArgType == Argument.AType.DynamicTypeName) {
289                                         info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
290                                                 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsStaticType", loc), loc);
291                                 }
292
293                                 var arg_type = a.Expr.Type;
294
295                                 if (arg_type != InternalType.Dynamic && arg_type != InternalType.Null) {
296                                         MethodGroupExpr mg = a.Expr as MethodGroupExpr;
297                                         if (mg != null) {
298                                                 rc.Report.Error (1976, a.Expr.Location,
299                                                         "The method group `{0}' cannot be used as an argument of dynamic operation. Consider using parentheses to invoke the method",
300                                                         mg.Name);
301                                         } else if (arg_type == InternalType.AnonymousMethod) {
302                                                 rc.Report.Error (1977, a.Expr.Location,
303                                                         "An anonymous method or lambda expression cannot be used as an argument of dynamic operation. Consider using a cast");
304                                         } else if (arg_type == TypeManager.void_type || arg_type == InternalType.Arglist || arg_type.IsPointer) {
305                                                 rc.Report.Error (1978, a.Expr.Location,
306                                                         "An expression of type `{0}' cannot be used as an argument of dynamic operation",
307                                                         TypeManager.CSharpName (arg_type));
308                                         }
309
310                                         info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
311                                                 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc), loc);
312                                 }
313
314                                 string named_value;
315                                 NamedArgument na = a as NamedArgument;
316                                 if (na != null) {
317                                         info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
318                                                 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "NamedArgument", loc), loc);
319
320                                         named_value = na.Name;
321                                 } else {
322                                         named_value = null;
323                                 }
324
325                                 dargs.Add (new Argument (info_flags));
326                                 dargs.Add (new Argument (new StringLiteral (named_value, loc)));
327                                 all.Add (new Invocation (new MemberAccess (new MemberAccess (binder, "CSharpArgumentInfo", loc), "Create", loc), dargs));
328                         }
329
330                         return all;
331                 }
332
333                 public static Arguments CreateForExpressionTree (ResolveContext ec, Arguments args, params Expression[] e)
334                 {
335                         Arguments all = new Arguments ((args == null ? 0 : args.Count) + e.Length);
336                         for (int i = 0; i < e.Length; ++i) {
337                                 if (e [i] != null)
338                                         all.Add (new Argument (e[i]));
339                         }
340
341                         if (args != null) {
342                                 foreach (Argument a in args.args) {
343                                         Expression tree_arg = a.CreateExpressionTree (ec);
344                                         if (tree_arg != null)
345                                                 all.Add (new Argument (tree_arg));
346                                 }
347                         }
348
349                         return all;
350                 }
351
352                 public void CheckArrayAsAttribute (CompilerContext ctx)
353                 {
354                         foreach (Argument arg in args) {
355                                 // Type is undefined (was error 246)
356                                 if (arg.Type == null)
357                                         continue;
358
359                                 if (arg.Type.IsArray)
360                                         ctx.Report.Warning (3016, 1, arg.Expr.Location, "Arrays as attribute arguments are not CLS-compliant");
361                         }
362                 }
363
364                 public Arguments Clone (CloneContext ctx)
365                 {
366                         Arguments cloned = new Arguments (args.Count);
367                         foreach (Argument a in args)
368                                 cloned.Add (a.Clone (ctx));
369
370                         return cloned;
371                 }
372
373                 public int Count {
374                         get { return args.Count; }
375                 }
376
377                 //
378                 // Emits a list of resolved Arguments
379                 // 
380                 public void Emit (EmitContext ec)
381                 {
382                         Emit (ec, false);
383                 }
384
385                 //
386                 // if `dup_args' is true, a copy of the arguments will be left
387                 // on the stack and return value will contain an array of access
388                 // expressions
389                 // NOTE: It's caller responsibility is to release temporary variables
390                 //
391                 public virtual Expression[] Emit (EmitContext ec, bool dup_args)
392                 {
393                         Expression[] temps;
394
395                         if (dup_args && Count != 0)
396                                 temps = new Expression [Count];
397                         else
398                                 temps = null;
399
400                         int i = 0;
401                         LocalTemporary lt;
402                         foreach (Argument a in args) {
403                                 a.Emit (ec);
404                                 if (!dup_args)
405                                         continue;
406
407                                 if (a.Expr is Constant || a.Expr is This) {
408                                         //
409                                         // No need to create a temporary variable for constants
410                                         //
411                                         temps[i] = a.Expr;
412                                 } else {
413                                         ec.Emit (OpCodes.Dup);
414                                         temps[i] = lt = new LocalTemporary (a.Type);
415                                         lt.Store (ec);
416                                 }
417
418                                 ++i;
419                         }
420
421                         return temps;
422                 }
423
424                 public List<Argument>.Enumerator GetEnumerator ()
425                 {
426                         return args.GetEnumerator ();
427                 }
428
429                 //
430                 // At least one argument is of dynamic type
431                 //
432                 public bool HasDynamic {
433                         get {
434                                 foreach (Argument a in args) {
435                                         if (a.Type == InternalType.Dynamic && !a.IsByRef)
436                                                 return true;
437                                 }
438                                 
439                                 return false;
440                         }
441                 }
442
443                 //
444                 // At least one argument is named argument
445                 //
446                 public bool HasNamed {
447                         get {
448                                 foreach (Argument a in args) {
449                                         if (a is NamedArgument)
450                                                 return true;
451                                 }
452                                 
453                                 return false;
454                         }
455                 }
456
457
458                 public void Insert (int index, Argument arg)
459                 {
460                         args.Insert (index, arg);
461                 }
462
463                 public static System.Linq.Expressions.Expression[] MakeExpression (Arguments args, BuilderContext ctx)
464                 {
465                         if (args == null || args.Count == 0)
466                                 return null;
467
468                         var exprs = new System.Linq.Expressions.Expression [args.Count];
469                         for (int i = 0; i < exprs.Length; ++i) {
470                                 Argument a = args.args [i];
471                                 exprs[i] = a.Expr.MakeExpression (ctx);
472                         }
473
474                         return exprs;
475                 }
476
477                 //
478                 // For named arguments when the order of execution is different
479                 // to order of invocation
480                 //
481                 public Arguments MarkOrderedArgument (NamedArgument a)
482                 {
483                         //
484                         // Constant expression have no effect on left-to-right execution
485                         //
486                         if (a.Expr is Constant)
487                                 return this;
488
489                         ArgumentsOrdered ra = this as ArgumentsOrdered;
490                         if (ra == null)
491                                 ra = new ArgumentsOrdered (this);
492
493                         ra.AddOrdered (a);
494                         return ra;
495                 }
496
497                 //
498                 // Returns dynamic when at least one argument is of dynamic type
499                 //
500                 public void Resolve (ResolveContext ec, out bool dynamic)
501                 {
502                         dynamic = false;
503                         foreach (Argument a in args) {
504                                 a.Resolve (ec);
505                                 if (a.Type == InternalType.Dynamic && !a.IsByRef)
506                                         dynamic = true;
507                         }
508                 }
509
510                 public void RemoveAt (int index)
511                 {
512                         args.RemoveAt (index);
513                 }
514
515                 public Argument this [int index] {
516                         get { return args [index]; }
517                         set { args [index] = value; }
518                 }
519         }
520 }