2009-12-02 Jb Evain <jbevain@novell.com>
[mcs.git] / mcs / argument.cs
blobc75f5b048e97b77d38d81b02281acd85b3eecd0d
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.
12 using System;
13 using System.Collections;
14 using System.Reflection;
15 using System.Reflection.Emit;
16 using System.Collections.Generic;
18 namespace Mono.CSharp
21 // Argument expression used for invocation
23 public class Argument
25 public enum AType : byte
27 None = 0,
28 Ref = 1, // ref modifier used
29 Out = 2, // out modifier used
30 Default = 3, // argument created from default parameter value
31 DynamicTypeName = 4 // System.Type argument for dynamic binding
34 public readonly AType ArgType;
35 public Expression Expr;
37 public Argument (Expression expr, AType type)
39 this.Expr = expr;
40 this.ArgType = type;
43 public Argument (Expression expr)
45 if (expr == null)
46 throw new ArgumentNullException ();
48 this.Expr = expr;
51 public Type Type {
52 get { return Expr.Type; }
55 public Parameter.Modifier Modifier {
56 get {
57 switch (ArgType) {
58 case AType.Out:
59 return Parameter.Modifier.OUT;
61 case AType.Ref:
62 return Parameter.Modifier.REF;
64 default:
65 return Parameter.Modifier.NONE;
70 public virtual Expression CreateExpressionTree (ResolveContext ec)
72 if (ArgType == AType.Default)
73 ec.Report.Error (854, Expr.Location, "An expression tree cannot contain an invocation which uses optional parameter");
75 return Expr.CreateExpressionTree (ec);
78 public string GetSignatureForError ()
80 if (Expr.eclass == ExprClass.MethodGroup)
81 return Expr.ExprClassName;
83 return TypeManager.CSharpName (Expr.Type);
86 public bool IsByRef {
87 get { return ArgType == AType.Ref || ArgType == AType.Out; }
90 public bool IsDefaultArgument {
91 get { return ArgType == AType.Default; }
94 public bool ResolveMethodGroup (ResolveContext ec)
96 SimpleName sn = Expr as SimpleName;
97 if (sn != null)
98 Expr = sn.GetMethodGroup ();
100 // FIXME: csc doesn't report any error if you try to use `ref' or
101 // `out' in a delegate creation expression.
102 Expr = Expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
103 if (Expr == null)
104 return false;
106 return true;
109 public void Resolve (ResolveContext ec)
111 if (Expr == EmptyExpression.Null)
112 return;
114 using (ec.With (ResolveContext.Options.DoFlowAnalysis, true)) {
115 // Verify that the argument is readable
116 if (ArgType != AType.Out)
117 Expr = Expr.Resolve (ec);
119 // Verify that the argument is writeable
120 if (Expr != null && IsByRef)
121 Expr = Expr.ResolveLValue (ec, EmptyExpression.OutAccess.Instance);
123 if (Expr == null)
124 Expr = EmptyExpression.Null;
128 public virtual void Emit (EmitContext ec)
130 if (!IsByRef) {
131 Expr.Emit (ec);
132 return;
135 AddressOp mode = AddressOp.Store;
136 if (ArgType == AType.Ref)
137 mode |= AddressOp.Load;
139 IMemoryLocation ml = (IMemoryLocation) Expr;
140 ml.AddressOf (ec, mode);
143 public Argument Clone (CloneContext clonectx)
145 Argument a = (Argument) MemberwiseClone ();
146 a.Expr = Expr.Clone (clonectx);
147 return a;
151 public class NamedArgument : Argument
153 public readonly string Name;
154 readonly Location loc;
155 LocalTemporary variable;
157 public NamedArgument (string name, Location loc, Expression expr)
158 : this (name, loc, expr, AType.None)
162 public NamedArgument (string name, Location loc, Expression expr, AType modifier)
163 : base (expr, modifier)
165 this.Name = name;
166 this.loc = loc;
169 public override Expression CreateExpressionTree (ResolveContext ec)
171 ec.Report.Error (853, loc, "An expression tree cannot contain named argument");
172 return base.CreateExpressionTree (ec);
175 public override void Emit (EmitContext ec)
177 // TODO: Should guard against multiple emits
178 base.Emit (ec);
180 // Release temporary variable when used
181 if (variable != null)
182 variable.Release (ec);
185 public void EmitAssign (EmitContext ec)
187 var type = Expr.Type;
188 if (IsByRef) {
189 var ml = (IMemoryLocation) Expr;
190 ml.AddressOf (ec, AddressOp.Load);
191 type = TypeManager.GetReferenceType (type);
192 } else {
193 Expr.Emit (ec);
196 variable = new LocalTemporary (type);
197 variable.Store (ec);
199 Expr = variable;
202 public Location Location {
203 get { return loc; }
207 public class Arguments
209 List<Argument> args;
210 ArrayList reordered; // TODO: LinkedList
212 public Arguments (int capacity)
214 args = new List<Argument> (capacity);
217 public void Add (Argument arg)
219 args.Add (arg);
222 public void AddRange (Arguments args)
224 this.args.AddRange (args.args);
227 public ArrayList CreateDynamicBinderArguments (ResolveContext rc)
229 ArrayList all = new ArrayList (args.Count);
230 Location loc = Location.Null;
232 MemberAccess binder = DynamicExpressionStatement.GetBinderNamespace (loc);
234 foreach (Argument a in args) {
235 Arguments dargs = new Arguments (2);
237 // CSharpArgumentInfoFlags.None = 0
238 const string info_flags_enum = "CSharpArgumentInfoFlags";
239 Expression info_flags = new IntLiteral (0, loc);
241 var constant = a.Expr as Constant;
242 if (constant != null && constant.IsLiteral) {
243 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
244 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "LiteralConstant", loc));
245 } else if (a.ArgType == Argument.AType.Ref) {
246 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
247 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsRef", loc));
248 } else if (a.ArgType == Argument.AType.Out) {
249 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
250 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsOut", loc));
251 } else if (a.ArgType == Argument.AType.DynamicTypeName) {
252 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
253 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsStaticType", loc));
256 var arg_type = a.Expr.Type;
258 if (!TypeManager.IsDynamicType (arg_type)) {
259 MethodGroupExpr mg = a.Expr as MethodGroupExpr;
260 if (mg != null) {
261 rc.Report.Error (1976, a.Expr.Location,
262 "The method group `{0}' cannot be used as an argument of dynamic operation. Consider using parentheses to invoke the method",
263 mg.Name);
264 } else if (arg_type == InternalType.AnonymousMethod) {
265 rc.Report.Error (1977, a.Expr.Location,
266 "An anonymous method or lambda expression cannot be used as an argument of dynamic operation. Consider using a cast");
267 } else if (arg_type == TypeManager.void_type || arg_type == InternalType.Arglist || arg_type.IsPointer) {
268 rc.Report.Error (1978, a.Expr.Location,
269 "An expression of type `{0}' cannot be used as an argument of dynamic operation",
270 TypeManager.CSharpName (arg_type));
273 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
274 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc));
277 string named_value;
278 NamedArgument na = a as NamedArgument;
279 if (na != null) {
280 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
281 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "NamedArgument", loc));
283 named_value = na.Name;
284 } else {
285 named_value = null;
288 dargs.Add (new Argument (info_flags));
289 dargs.Add (new Argument (new StringLiteral (named_value, loc)));
290 all.Add (new Invocation (new MemberAccess (new MemberAccess (binder, "CSharpArgumentInfo", loc), "Create", loc), dargs));
293 return all;
296 public static Arguments CreateForExpressionTree (ResolveContext ec, Arguments args, params Expression[] e)
298 Arguments all = new Arguments ((args == null ? 0 : args.Count) + e.Length);
299 for (int i = 0; i < e.Length; ++i) {
300 if (e [i] != null)
301 all.Add (new Argument (e[i]));
304 if (args != null) {
305 foreach (Argument a in args.args) {
306 Expression tree_arg = a.CreateExpressionTree (ec);
307 if (tree_arg != null)
308 all.Add (new Argument (tree_arg));
312 return all;
315 public void CheckArrayAsAttribute (CompilerContext ctx)
317 foreach (Argument arg in args) {
318 // Type is undefined (was error 246)
319 if (arg.Type == null)
320 continue;
322 if (arg.Type.IsArray)
323 ctx.Report.Warning (3016, 1, arg.Expr.Location, "Arrays as attribute arguments are not CLS-compliant");
327 public Arguments Clone (CloneContext ctx)
329 Arguments cloned = new Arguments (args.Count);
330 foreach (Argument a in args)
331 cloned.Add (a.Clone (ctx));
333 return cloned;
336 public int Count {
337 get { return args.Count; }
341 // Emits a list of resolved Arguments
343 public void Emit (EmitContext ec)
345 Emit (ec, false, null);
349 // if `dup_args' is true, a copy of the arguments will be left
350 // on the stack. If `dup_args' is true, you can specify `this_arg'
351 // which will be duplicated before any other args. Only EmitCall
352 // should be using this interface.
354 public void Emit (EmitContext ec, bool dup_args, LocalTemporary this_arg)
356 LocalTemporary[] temps = null;
358 if (dup_args && Count != 0)
359 temps = new LocalTemporary [Count];
361 if (reordered != null && Count > 1) {
362 foreach (NamedArgument na in reordered)
363 na.EmitAssign (ec);
366 int i = 0;
367 foreach (Argument a in args) {
368 a.Emit (ec);
369 if (dup_args) {
370 ec.ig.Emit (OpCodes.Dup);
371 (temps [i++] = new LocalTemporary (a.Type)).Store (ec);
375 if (dup_args) {
376 if (this_arg != null)
377 this_arg.Emit (ec);
379 for (i = 0; i < temps.Length; i++) {
380 temps[i].Emit (ec);
381 temps[i].Release (ec);
386 public bool GetAttributableValue (ResolveContext ec, out object[] values)
388 values = new object [args.Count];
389 for (int j = 0; j < values.Length; ++j) {
390 Argument a = this [j];
391 if (!a.Expr.GetAttributableValue (ec, a.Type, out values[j]))
392 return false;
395 return true;
398 public IEnumerator GetEnumerator ()
400 return args.GetEnumerator ();
404 // At least one argument is of dynamic type
406 public bool HasDynamic {
407 get {
408 foreach (Argument a in args) {
409 if (TypeManager.IsDynamicType (a.Type))
410 return true;
413 return false;
417 public void Insert (int index, Argument arg)
419 args.Insert (index, arg);
422 #if NET_4_0
423 public static System.Linq.Expressions.Expression[] MakeExpression (Arguments args, BuilderContext ctx)
425 if (args == null || args.Count == 0)
426 return null;
428 var exprs = new System.Linq.Expressions.Expression [args.Count];
429 for (int i = 0; i < exprs.Length; ++i) {
430 Argument a = (Argument) args.args [i];
431 exprs[i] = a.Expr.MakeExpression (ctx);
434 return exprs;
436 #endif
438 public void MarkReorderedArgument (NamedArgument a)
441 // Constant expression can have no effect on left-to-right execution
443 if (a.Expr is Constant)
444 return;
446 if (reordered == null)
447 reordered = new ArrayList ();
449 reordered.Add (a);
453 // Returns dynamic when at least one argument is of dynamic type
455 public void Resolve (ResolveContext ec, out bool dynamic)
457 dynamic = false;
458 foreach (Argument a in args) {
459 a.Resolve (ec);
460 dynamic |= TypeManager.IsDynamicType (a.Type);
464 public void MutateHoistedGenericType (AnonymousMethodStorey storey)
466 foreach (Argument a in args)
467 a.Expr.MutateHoistedGenericType (storey);
470 public void RemoveAt (int index)
472 args.RemoveAt (index);
475 public Argument this [int index] {
476 get { return (Argument) args [index]; }
477 set { args [index] = value; }