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