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-2011 Novell, Inc.
10 // Copyright 2011 Xamarin Inc
14 using System.Collections.Generic;
17 using IKVM.Reflection.Emit;
19 using System.Reflection.Emit;
25 // Argument expression used for invocation
29 public enum AType : byte
32 Ref = 1, // ref modifier used
33 Out = 2, // out modifier used
34 Default = 3, // argument created from default parameter value
35 DynamicTypeName = 4, // System.Type argument for dynamic binding
36 ExtensionType = 5, // Instance expression inserted as the first argument
39 public readonly AType ArgType;
40 public Expression Expr;
42 public Argument (Expression expr, AType type)
48 public Argument (Expression expr)
51 throw new ArgumentNullException ();
59 get { return ArgType == AType.Ref || ArgType == AType.Out; }
62 public bool IsDefaultArgument {
63 get { return ArgType == AType.Default; }
66 public Parameter.Modifier Modifier {
70 return Parameter.Modifier.OUT;
73 return Parameter.Modifier.REF;
76 return Parameter.Modifier.NONE;
81 public TypeSpec Type {
82 get { return Expr.Type; }
87 public Argument Clone (Expression expr)
89 Argument a = (Argument) MemberwiseClone ();
94 public Argument Clone (CloneContext clonectx)
96 return Clone (Expr.Clone (clonectx));
99 public virtual Expression CreateExpressionTree (ResolveContext ec)
101 if (ArgType == AType.Default)
102 ec.Report.Error (854, Expr.Location, "An expression tree cannot contain an invocation which uses optional parameter");
104 return Expr.CreateExpressionTree (ec);
108 public virtual void Emit (EmitContext ec)
115 AddressOp mode = AddressOp.Store;
116 if (ArgType == AType.Ref)
117 mode |= AddressOp.Load;
119 IMemoryLocation ml = (IMemoryLocation) Expr;
120 ml.AddressOf (ec, mode);
123 public Argument EmitToField (EmitContext ec)
125 var res = Expr.EmitToField (ec);
126 return res == Expr ? this : new Argument (res, ArgType);
129 public string GetSignatureForError ()
131 if (Expr.eclass == ExprClass.MethodGroup)
132 return Expr.ExprClassName;
134 return TypeManager.CSharpName (Expr.Type);
137 public bool ResolveMethodGroup (ResolveContext ec)
139 SimpleName sn = Expr as SimpleName;
141 Expr = sn.GetMethodGroup ();
143 // FIXME: csc doesn't report any error if you try to use `ref' or
144 // `out' in a delegate creation expression.
145 Expr = Expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
152 public void Resolve (ResolveContext ec)
154 // using (ec.With (ResolveContext.Options.DoFlowAnalysis, true)) {
155 // Verify that the argument is readable
156 if (ArgType != AType.Out)
157 Expr = Expr.Resolve (ec);
159 // Verify that the argument is writeable
160 if (Expr != null && IsByRef)
161 Expr = Expr.ResolveLValue (ec, EmptyExpression.OutAccess);
164 Expr = ErrorExpression.Instance;
169 public class MovableArgument : Argument
171 LocalTemporary variable;
173 public MovableArgument (Argument arg)
174 : this (arg.Expr, arg.ArgType)
178 protected MovableArgument (Expression expr, AType modifier)
179 : base (expr, modifier)
183 public override void Emit (EmitContext ec)
185 // TODO: Should guard against multiple emits
188 // Release temporary variable when used
189 if (variable != null)
190 variable.Release (ec);
193 public void EmitToVariable (EmitContext ec)
195 var type = Expr.Type;
197 var ml = (IMemoryLocation) Expr;
198 ml.AddressOf (ec, AddressOp.LoadStore);
199 type = ReferenceContainer.MakeType (ec.Module, type);
204 variable = new LocalTemporary (type);
211 public class NamedArgument : MovableArgument
213 public readonly string Name;
214 readonly Location loc;
216 public NamedArgument (string name, Location loc, Expression expr)
217 : this (name, loc, expr, AType.None)
221 public NamedArgument (string name, Location loc, Expression expr, AType modifier)
222 : base (expr, modifier)
228 public override Expression CreateExpressionTree (ResolveContext ec)
230 ec.Report.Error (853, loc, "An expression tree cannot contain named argument");
231 return base.CreateExpressionTree (ec);
234 public Location Location {
239 public class Arguments
241 sealed class ArgumentsOrdered : Arguments
243 readonly List<MovableArgument> ordered;
245 public ArgumentsOrdered (Arguments args)
249 ordered = new List<MovableArgument> ();
252 public void AddOrdered (MovableArgument arg)
257 public override Arguments Emit (EmitContext ec, bool dup_args, bool prepareAwait)
259 foreach (var a in ordered) {
263 a.EmitToVariable (ec);
266 return base.Emit (ec, dup_args, prepareAwait);
270 // Try not to add any more instances to this class, it's allocated a lot
273 public Arguments (int capacity)
275 args = new List<Argument> (capacity);
278 private Arguments (List<Argument> args)
283 public void Add (Argument arg)
288 public void AddRange (Arguments args)
290 this.args.AddRange (args.args);
293 public bool ContainsEmitWithAwait ()
295 foreach (var arg in args) {
296 if (arg.Expr.ContainsEmitWithAwait ())
303 public ArrayInitializer CreateDynamicBinderArguments (ResolveContext rc)
305 Location loc = Location.Null;
306 var all = new ArrayInitializer (args.Count, loc);
308 MemberAccess binder = DynamicExpressionStatement.GetBinderNamespace (loc);
310 foreach (Argument a in args) {
311 Arguments dargs = new Arguments (2);
313 // CSharpArgumentInfoFlags.None = 0
314 const string info_flags_enum = "CSharpArgumentInfoFlags";
315 Expression info_flags = new IntLiteral (rc.BuiltinTypes, 0, loc);
317 if (a.Expr is Constant) {
318 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
319 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "Constant", loc), loc);
320 } else if (a.ArgType == Argument.AType.Ref) {
321 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
322 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsRef", loc), loc);
323 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
324 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc), loc);
325 } else if (a.ArgType == Argument.AType.Out) {
326 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
327 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsOut", loc), loc);
328 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
329 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc), loc);
330 } else if (a.ArgType == Argument.AType.DynamicTypeName) {
331 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
332 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsStaticType", loc), loc);
335 var arg_type = a.Expr.Type;
337 if (arg_type.BuiltinType != BuiltinTypeSpec.Type.Dynamic && arg_type != InternalType.NullLiteral) {
338 MethodGroupExpr mg = a.Expr as MethodGroupExpr;
340 rc.Report.Error (1976, a.Expr.Location,
341 "The method group `{0}' cannot be used as an argument of dynamic operation. Consider using parentheses to invoke the method",
343 } else if (arg_type == InternalType.AnonymousMethod) {
344 rc.Report.Error (1977, a.Expr.Location,
345 "An anonymous method or lambda expression cannot be used as an argument of dynamic operation. Consider using a cast");
346 } else if (arg_type.Kind == MemberKind.Void || arg_type == InternalType.Arglist || arg_type.IsPointer) {
347 rc.Report.Error (1978, a.Expr.Location,
348 "An expression of type `{0}' cannot be used as an argument of dynamic operation",
349 TypeManager.CSharpName (arg_type));
352 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
353 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc), loc);
357 NamedArgument na = a as NamedArgument;
359 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
360 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "NamedArgument", loc), loc);
362 named_value = na.Name;
367 dargs.Add (new Argument (info_flags));
368 dargs.Add (new Argument (new StringLiteral (rc.BuiltinTypes, named_value, loc)));
369 all.Add (new Invocation (new MemberAccess (new MemberAccess (binder, "CSharpArgumentInfo", loc), "Create", loc), dargs));
375 public static Arguments CreateForExpressionTree (ResolveContext ec, Arguments args, params Expression[] e)
377 Arguments all = new Arguments ((args == null ? 0 : args.Count) + e.Length);
378 for (int i = 0; i < e.Length; ++i) {
380 all.Add (new Argument (e[i]));
384 foreach (Argument a in args.args) {
385 Expression tree_arg = a.CreateExpressionTree (ec);
386 if (tree_arg != null)
387 all.Add (new Argument (tree_arg));
394 public void CheckArrayAsAttribute (CompilerContext ctx)
396 foreach (Argument arg in args) {
397 // Type is undefined (was error 246)
398 if (arg.Type == null)
401 if (arg.Type.IsArray)
402 ctx.Report.Warning (3016, 1, arg.Expr.Location, "Arrays as attribute arguments are not CLS-compliant");
406 public Arguments Clone (CloneContext ctx)
408 Arguments cloned = new Arguments (args.Count);
409 foreach (Argument a in args)
410 cloned.Add (a.Clone (ctx));
416 get { return args.Count; }
420 // Emits a list of resolved Arguments
422 public void Emit (EmitContext ec)
424 Emit (ec, false, false);
428 // if `dup_args' is true or any of arguments contains await.
429 // A copy of all arguments will be returned to the caller
431 public virtual Arguments Emit (EmitContext ec, bool dup_args, bool prepareAwait)
435 if ((dup_args && Count != 0) || prepareAwait)
436 dups = new List<Argument> (Count);
441 foreach (Argument a in args) {
443 dups.Add (a.EmitToField (ec));
453 if (a.Expr.IsSideEffectFree) {
455 // No need to create a temporary variable for side effect free expressions. I assume
456 // all side-effect free expressions are cheap, this has to be tweaked when we become
457 // more aggressive on detection
461 ec.Emit (OpCodes.Dup);
463 // TODO: Release local temporary on next Emit
464 // Need to add a flag to argument to indicate this
465 lt = new LocalTemporary (a.Type);
468 dups.Add (new Argument (lt, a.ArgType));
473 return new Arguments (dups);
478 public List<Argument>.Enumerator GetEnumerator ()
480 return args.GetEnumerator ();
484 // At least one argument is of dynamic type
486 public bool HasDynamic {
488 foreach (Argument a in args) {
489 if (a.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic && !a.IsByRef)
498 // At least one argument is named argument
500 public bool HasNamed {
502 foreach (Argument a in args) {
503 if (a is NamedArgument)
512 public void Insert (int index, Argument arg)
514 args.Insert (index, arg);
517 public static System.Linq.Expressions.Expression[] MakeExpression (Arguments args, BuilderContext ctx)
519 if (args == null || args.Count == 0)
522 var exprs = new System.Linq.Expressions.Expression [args.Count];
523 for (int i = 0; i < exprs.Length; ++i) {
524 Argument a = args.args [i];
525 exprs[i] = a.Expr.MakeExpression (ctx);
532 // For named arguments when the order of execution is different
533 // to order of invocation
535 public Arguments MarkOrderedArgument (NamedArgument a)
538 // An expression has no effect on left-to-right execution
540 if (a.Expr.IsSideEffectFree)
543 ArgumentsOrdered ra = this as ArgumentsOrdered;
545 ra = new ArgumentsOrdered (this);
547 for (int i = 0; i < args.Count; ++i) {
553 // When the argument is filled later by default expression
558 var ma = la as MovableArgument;
560 ma = new MovableArgument (la);
573 // Returns dynamic when at least one argument is of dynamic type
575 public void Resolve (ResolveContext ec, out bool dynamic)
578 foreach (Argument a in args) {
580 if (a.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic && !a.IsByRef)
585 public void RemoveAt (int index)
587 args.RemoveAt (index);
590 public Argument this [int index] {
591 get { return args [index]; }
592 set { args [index] = value; }