2 // argument.cs: Argument expressions
5 // Miguel de Icaza (miguel@ximain.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // Dual licensed under the terms of the MIT X11 or GNU GPL
9 // Copyright 2003-2008 Novell, Inc.
13 using System.Reflection;
14 using System.Reflection.Emit;
15 using System.Collections.Generic;
20 // Argument expression used for invocation
24 public enum AType : byte
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
34 public readonly AType ArgType;
35 public Expression Expr;
37 public Argument (Expression expr, AType type)
43 public Argument (Expression expr)
46 throw new ArgumentNullException ();
54 get { return ArgType == AType.Ref || ArgType == AType.Out; }
57 public bool IsDefaultArgument {
58 get { return ArgType == AType.Default; }
61 public Parameter.Modifier Modifier {
65 return Parameter.Modifier.OUT;
68 return Parameter.Modifier.REF;
71 return Parameter.Modifier.NONE;
76 public TypeSpec Type {
77 get { return Expr.Type; }
82 public Argument Clone (Expression expr)
84 Argument a = (Argument) MemberwiseClone ();
89 public Argument Clone (CloneContext clonectx)
91 return Clone (Expr.Clone (clonectx));
94 public virtual Expression CreateExpressionTree (ResolveContext ec)
96 if (ArgType == AType.Default)
97 ec.Report.Error (854, Expr.Location, "An expression tree cannot contain an invocation which uses optional parameter");
99 return Expr.CreateExpressionTree (ec);
102 public string GetSignatureForError ()
104 if (Expr.eclass == ExprClass.MethodGroup)
105 return Expr.ExprClassName;
107 return TypeManager.CSharpName (Expr.Type);
110 public bool ResolveMethodGroup (ResolveContext ec)
112 SimpleName sn = Expr as SimpleName;
114 Expr = sn.GetMethodGroup ();
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);
125 public void Resolve (ResolveContext ec)
127 if (Expr == EmptyExpression.Null)
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);
135 // Verify that the argument is writeable
136 if (Expr != null && IsByRef)
137 Expr = Expr.ResolveLValue (ec, EmptyExpression.OutAccess.Instance);
140 Expr = EmptyExpression.Null;
144 public virtual void Emit (EmitContext ec)
151 AddressOp mode = AddressOp.Store;
152 if (ArgType == AType.Ref)
153 mode |= AddressOp.Load;
155 IMemoryLocation ml = (IMemoryLocation) Expr;
156 ml.AddressOf (ec, mode);
160 public class NamedArgument : Argument
162 public readonly string Name;
163 readonly Location loc;
164 LocalTemporary variable;
166 public NamedArgument (string name, Location loc, Expression expr)
167 : this (name, loc, expr, AType.None)
171 public NamedArgument (string name, Location loc, Expression expr, AType modifier)
172 : base (expr, modifier)
178 public override Expression CreateExpressionTree (ResolveContext ec)
180 ec.Report.Error (853, loc, "An expression tree cannot contain named argument");
181 return base.CreateExpressionTree (ec);
184 public override void Emit (EmitContext ec)
186 // TODO: Should guard against multiple emits
189 // Release temporary variable when used
190 if (variable != null)
191 variable.Release (ec);
194 public void EmitAssign (EmitContext ec)
196 var type = Expr.Type;
198 var ml = (IMemoryLocation) Expr;
199 ml.AddressOf (ec, AddressOp.Load);
200 type = ReferenceContainer.MakeType (type);
205 variable = new LocalTemporary (type);
211 public Location Location {
216 public class Arguments
218 sealed class ArgumentsOrdered : Arguments
220 List<NamedArgument> ordered;
222 public ArgumentsOrdered (Arguments args)
226 ordered = new List<NamedArgument> ();
229 public void AddOrdered (NamedArgument na)
234 public override Expression[] Emit (EmitContext ec, bool dup_args)
236 foreach (NamedArgument na in ordered)
239 return base.Emit (ec, dup_args);
243 // Try not to add any more instances to this class, it's allocated a lot
246 public Arguments (int capacity)
248 args = new List<Argument> (capacity);
251 public void Add (Argument arg)
256 public void AddRange (Arguments args)
258 this.args.AddRange (args.args);
261 public ArrayInitializer CreateDynamicBinderArguments (ResolveContext rc)
263 Location loc = Location.Null;
264 var all = new ArrayInitializer (args.Count, loc);
266 MemberAccess binder = DynamicExpressionStatement.GetBinderNamespace (loc);
268 foreach (Argument a in args) {
269 Arguments dargs = new Arguments (2);
271 // CSharpArgumentInfoFlags.None = 0
272 const string info_flags_enum = "CSharpArgumentInfoFlags";
273 Expression info_flags = new IntLiteral (0, loc);
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);
293 var arg_type = a.Expr.Type;
295 if (arg_type != InternalType.Dynamic && arg_type != InternalType.Null) {
296 MethodGroupExpr mg = a.Expr as MethodGroupExpr;
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",
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));
310 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
311 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc), loc);
315 NamedArgument na = a as NamedArgument;
317 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
318 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "NamedArgument", loc), loc);
320 named_value = na.Name;
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));
333 public static Arguments CreateForExpressionTree (ResolveContext ec, Arguments args, params Expression[] e)
335 Arguments all = new Arguments ((args == null ? 0 : args.Count) + e.Length);
336 for (int i = 0; i < e.Length; ++i) {
338 all.Add (new Argument (e[i]));
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));
352 public void CheckArrayAsAttribute (CompilerContext ctx)
354 foreach (Argument arg in args) {
355 // Type is undefined (was error 246)
356 if (arg.Type == null)
359 if (arg.Type.IsArray)
360 ctx.Report.Warning (3016, 1, arg.Expr.Location, "Arrays as attribute arguments are not CLS-compliant");
364 public Arguments Clone (CloneContext ctx)
366 Arguments cloned = new Arguments (args.Count);
367 foreach (Argument a in args)
368 cloned.Add (a.Clone (ctx));
374 get { return args.Count; }
378 // Emits a list of resolved Arguments
380 public void Emit (EmitContext ec)
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
389 // NOTE: It's caller responsibility is to release temporary variables
391 public virtual Expression[] Emit (EmitContext ec, bool dup_args)
395 if (dup_args && Count != 0)
396 temps = new Expression [Count];
402 foreach (Argument a in args) {
407 if (a.Expr is Constant || a.Expr is This) {
409 // No need to create a temporary variable for constants
413 ec.Emit (OpCodes.Dup);
414 temps[i] = lt = new LocalTemporary (a.Type);
424 public List<Argument>.Enumerator GetEnumerator ()
426 return args.GetEnumerator ();
430 // At least one argument is of dynamic type
432 public bool HasDynamic {
434 foreach (Argument a in args) {
435 if (a.Type == InternalType.Dynamic && !a.IsByRef)
444 // At least one argument is named argument
446 public bool HasNamed {
448 foreach (Argument a in args) {
449 if (a is NamedArgument)
458 public void Insert (int index, Argument arg)
460 args.Insert (index, arg);
463 public static System.Linq.Expressions.Expression[] MakeExpression (Arguments args, BuilderContext ctx)
465 if (args == null || args.Count == 0)
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);
478 // For named arguments when the order of execution is different
479 // to order of invocation
481 public Arguments MarkOrderedArgument (NamedArgument a)
484 // Constant expression have no effect on left-to-right execution
486 if (a.Expr is Constant)
489 ArgumentsOrdered ra = this as ArgumentsOrdered;
491 ra = new ArgumentsOrdered (this);
498 // Returns dynamic when at least one argument is of dynamic type
500 public void Resolve (ResolveContext ec, out bool dynamic)
503 foreach (Argument a in args) {
505 if (a.Type == InternalType.Dynamic && !a.IsByRef)
510 public void RemoveAt (int index)
512 args.RemoveAt (index);
515 public Argument this [int index] {
516 get { return args [index]; }
517 set { args [index] = value; }