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)
56 get { return ArgType == AType.Ref || ArgType == AType.Out; }
59 public bool IsDefaultArgument {
60 get { return ArgType == AType.Default; }
63 public Parameter.Modifier Modifier {
67 return Parameter.Modifier.OUT;
70 return Parameter.Modifier.REF;
73 return Parameter.Modifier.NONE;
78 public TypeSpec Type {
79 get { return Expr.Type; }
84 public Argument Clone (Expression expr)
86 Argument a = (Argument) MemberwiseClone ();
91 public Argument Clone (CloneContext clonectx)
93 return Clone (Expr.Clone (clonectx));
96 public virtual Expression CreateExpressionTree (ResolveContext ec)
98 if (ArgType == AType.Default)
99 ec.Report.Error (854, Expr.Location, "An expression tree cannot contain an invocation which uses optional parameter");
101 return Expr.CreateExpressionTree (ec);
105 public virtual void Emit (EmitContext ec)
112 AddressOp mode = AddressOp.Store;
113 if (ArgType == AType.Ref)
114 mode |= AddressOp.Load;
116 IMemoryLocation ml = (IMemoryLocation) Expr;
117 ml.AddressOf (ec, mode);
120 public Argument EmitToField (EmitContext ec, bool cloneResult)
122 var res = Expr.EmitToField (ec);
123 if (cloneResult && res != Expr)
124 return new Argument (res, ArgType);
130 public void FlowAnalysis (FlowAnalysisContext fc)
132 if (ArgType == AType.Out) {
133 var vr = Expr as VariableReference;
135 if (vr.VariableInfo != null)
136 fc.SetVariableAssigned (vr.VariableInfo);
141 var fe = Expr as FieldExpr;
143 fe.SetFieldAssigned (fc);
150 Expr.FlowAnalysis (fc);
153 public string GetSignatureForError ()
155 if (Expr.eclass == ExprClass.MethodGroup)
156 return Expr.ExprClassName;
158 return Expr.Type.GetSignatureForError ();
161 public bool ResolveMethodGroup (ResolveContext ec)
163 SimpleName sn = Expr as SimpleName;
165 Expr = sn.GetMethodGroup ();
167 // FIXME: csc doesn't report any error if you try to use `ref' or
168 // `out' in a delegate creation expression.
169 Expr = Expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
176 public void Resolve (ResolveContext ec)
178 // Verify that the argument is readable
179 if (ArgType != AType.Out)
180 Expr = Expr.Resolve (ec);
182 // Verify that the argument is writeable
183 if (Expr != null && IsByRef)
184 Expr = Expr.ResolveLValue (ec, EmptyExpression.OutAccess);
187 Expr = ErrorExpression.Instance;
191 public class MovableArgument : Argument
193 LocalTemporary variable;
195 public MovableArgument (Argument arg)
196 : this (arg.Expr, arg.ArgType)
200 protected MovableArgument (Expression expr, AType modifier)
201 : base (expr, modifier)
205 public override void Emit (EmitContext ec)
207 // TODO: Should guard against multiple emits
210 // Release temporary variable when used
211 if (variable != null)
212 variable.Release (ec);
215 public void EmitToVariable (EmitContext ec)
217 var type = Expr.Type;
219 var ml = (IMemoryLocation) Expr;
220 ml.AddressOf (ec, AddressOp.LoadStore);
221 type = ReferenceContainer.MakeType (ec.Module, type);
226 variable = new LocalTemporary (type);
233 public class NamedArgument : MovableArgument
235 public readonly string Name;
236 readonly Location loc;
238 public NamedArgument (string name, Location loc, Expression expr)
239 : this (name, loc, expr, AType.None)
243 public NamedArgument (string name, Location loc, Expression expr, AType modifier)
244 : base (expr, modifier)
250 public override Expression CreateExpressionTree (ResolveContext ec)
252 ec.Report.Error (853, loc, "An expression tree cannot contain named argument");
253 return base.CreateExpressionTree (ec);
256 public Location Location {
261 public class Arguments
263 sealed class ArgumentsOrdered : Arguments
265 readonly List<MovableArgument> ordered;
267 public ArgumentsOrdered (Arguments args)
271 ordered = new List<MovableArgument> ();
274 public void AddOrdered (MovableArgument arg)
279 public override Arguments Emit (EmitContext ec, bool dup_args, bool prepareAwait)
281 foreach (var a in ordered) {
283 a.EmitToField (ec, false);
285 a.EmitToVariable (ec);
288 return base.Emit (ec, dup_args, prepareAwait);
292 // Try not to add any more instances to this class, it's allocated a lot
295 public Arguments (int capacity)
297 args = new List<Argument> (capacity);
300 private Arguments (List<Argument> args)
305 public void Add (Argument arg)
310 public void AddRange (Arguments args)
312 this.args.AddRange (args.args);
315 public bool ContainsEmitWithAwait ()
317 foreach (var arg in args) {
318 if (arg.Expr.ContainsEmitWithAwait ())
325 public ArrayInitializer CreateDynamicBinderArguments (ResolveContext rc)
327 Location loc = Location.Null;
328 var all = new ArrayInitializer (args.Count, loc);
330 MemberAccess binder = DynamicExpressionStatement.GetBinderNamespace (loc);
332 foreach (Argument a in args) {
333 Arguments dargs = new Arguments (2);
335 // CSharpArgumentInfoFlags.None = 0
336 const string info_flags_enum = "CSharpArgumentInfoFlags";
337 Expression info_flags = new IntLiteral (rc.BuiltinTypes, 0, loc);
339 if (a.Expr is Constant) {
340 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
341 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "Constant", loc));
342 } else if (a.ArgType == Argument.AType.Ref) {
343 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
344 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsRef", loc));
345 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
346 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc));
347 } else if (a.ArgType == Argument.AType.Out) {
348 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
349 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsOut", loc));
350 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
351 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc));
352 } else if (a.ArgType == Argument.AType.DynamicTypeName) {
353 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
354 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsStaticType", loc));
357 var arg_type = a.Expr.Type;
359 if (arg_type.BuiltinType != BuiltinTypeSpec.Type.Dynamic && arg_type != InternalType.NullLiteral) {
360 MethodGroupExpr mg = a.Expr as MethodGroupExpr;
362 rc.Report.Error (1976, a.Expr.Location,
363 "The method group `{0}' cannot be used as an argument of dynamic operation. Consider using parentheses to invoke the method",
365 } else if (arg_type == InternalType.AnonymousMethod) {
366 rc.Report.Error (1977, a.Expr.Location,
367 "An anonymous method or lambda expression cannot be used as an argument of dynamic operation. Consider using a cast");
368 } else if (arg_type.Kind == MemberKind.Void || arg_type == InternalType.Arglist || arg_type.IsPointer) {
369 rc.Report.Error (1978, a.Expr.Location,
370 "An expression of type `{0}' cannot be used as an argument of dynamic operation",
371 arg_type.GetSignatureForError ());
374 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
375 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc));
379 NamedArgument na = a as NamedArgument;
381 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
382 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "NamedArgument", loc));
384 named_value = na.Name;
389 dargs.Add (new Argument (info_flags));
390 dargs.Add (new Argument (new StringLiteral (rc.BuiltinTypes, named_value, loc)));
391 all.Add (new Invocation (new MemberAccess (new MemberAccess (binder, "CSharpArgumentInfo", loc), "Create", loc), dargs));
397 public static Arguments CreateForExpressionTree (ResolveContext ec, Arguments args, params Expression[] e)
399 Arguments all = new Arguments ((args == null ? 0 : args.Count) + e.Length);
400 for (int i = 0; i < e.Length; ++i) {
402 all.Add (new Argument (e[i]));
406 foreach (Argument a in args.args) {
407 Expression tree_arg = a.CreateExpressionTree (ec);
408 if (tree_arg != null)
409 all.Add (new Argument (tree_arg));
416 public void CheckArrayAsAttribute (CompilerContext ctx)
418 foreach (Argument arg in args) {
419 // Type is undefined (was error 246)
420 if (arg.Type == null)
423 if (arg.Type.IsArray)
424 ctx.Report.Warning (3016, 1, arg.Expr.Location, "Arrays as attribute arguments are not CLS-compliant");
428 public Arguments Clone (CloneContext ctx)
430 Arguments cloned = new Arguments (args.Count);
431 foreach (Argument a in args)
432 cloned.Add (a.Clone (ctx));
438 get { return args.Count; }
442 // Emits a list of resolved Arguments
444 public void Emit (EmitContext ec)
446 Emit (ec, false, false);
450 // if `dup_args' is true or any of arguments contains await.
451 // A copy of all arguments will be returned to the caller
453 public virtual Arguments Emit (EmitContext ec, bool dup_args, bool prepareAwait)
457 if ((dup_args && Count != 0) || prepareAwait)
458 dups = new List<Argument> (Count);
463 foreach (Argument a in args) {
465 dups.Add (a.EmitToField (ec, true));
475 if (a.Expr.IsSideEffectFree) {
477 // No need to create a temporary variable for side effect free expressions. I assume
478 // all side-effect free expressions are cheap, this has to be tweaked when we become
479 // more aggressive on detection
483 ec.Emit (OpCodes.Dup);
485 // TODO: Release local temporary on next Emit
486 // Need to add a flag to argument to indicate this
487 lt = new LocalTemporary (a.Type);
490 dups.Add (new Argument (lt, a.ArgType));
495 return new Arguments (dups);
500 public void FlowAnalysis (FlowAnalysisContext fc)
502 foreach (var arg in args)
503 arg.FlowAnalysis (fc);
506 public List<Argument>.Enumerator GetEnumerator ()
508 return args.GetEnumerator ();
512 // At least one argument is of dynamic type
514 public bool HasDynamic {
516 foreach (Argument a in args) {
517 if (a.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic && !a.IsByRef)
526 // At least one argument is named argument
528 public bool HasNamed {
530 foreach (Argument a in args) {
531 if (a is NamedArgument)
540 public void Insert (int index, Argument arg)
542 args.Insert (index, arg);
545 public static System.Linq.Expressions.Expression[] MakeExpression (Arguments args, BuilderContext ctx)
547 if (args == null || args.Count == 0)
550 var exprs = new System.Linq.Expressions.Expression [args.Count];
551 for (int i = 0; i < exprs.Length; ++i) {
552 Argument a = args.args [i];
553 exprs[i] = a.Expr.MakeExpression (ctx);
560 // For named arguments when the order of execution is different
561 // to order of invocation
563 public Arguments MarkOrderedArgument (NamedArgument a)
566 // An expression has no effect on left-to-right execution
568 if (a.Expr.IsSideEffectFree)
571 ArgumentsOrdered ra = this as ArgumentsOrdered;
573 ra = new ArgumentsOrdered (this);
575 for (int i = 0; i < args.Count; ++i) {
581 // When the argument is filled later by default expression
586 var ma = la as MovableArgument;
588 ma = new MovableArgument (la);
601 // Returns dynamic when at least one argument is of dynamic type
603 public void Resolve (ResolveContext ec, out bool dynamic)
606 foreach (Argument a in args) {
608 if (a.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic && !a.IsByRef)
613 public void RemoveAt (int index)
615 args.RemoveAt (index);
618 public Argument this [int index] {
619 get { return args [index]; }
620 set { args [index] = value; }