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