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 string GetSignatureForError ()
132 if (Expr.eclass == ExprClass.MethodGroup)
133 return Expr.ExprClassName;
135 return Expr.Type.GetSignatureForError ();
138 public bool ResolveMethodGroup (ResolveContext ec)
140 SimpleName sn = Expr as SimpleName;
142 Expr = sn.GetMethodGroup ();
144 // FIXME: csc doesn't report any error if you try to use `ref' or
145 // `out' in a delegate creation expression.
146 Expr = Expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
153 public void Resolve (ResolveContext ec)
155 // using (ec.With (ResolveContext.Options.DoFlowAnalysis, true)) {
156 // Verify that the argument is readable
157 if (ArgType != AType.Out)
158 Expr = Expr.Resolve (ec);
160 // Verify that the argument is writeable
161 if (Expr != null && IsByRef)
162 Expr = Expr.ResolveLValue (ec, EmptyExpression.OutAccess);
165 Expr = ErrorExpression.Instance;
170 public class MovableArgument : Argument
172 LocalTemporary variable;
174 public MovableArgument (Argument arg)
175 : this (arg.Expr, arg.ArgType)
179 protected MovableArgument (Expression expr, AType modifier)
180 : base (expr, modifier)
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 EmitToVariable (EmitContext ec)
196 var type = Expr.Type;
198 var ml = (IMemoryLocation) Expr;
199 ml.AddressOf (ec, AddressOp.LoadStore);
200 type = ReferenceContainer.MakeType (ec.Module, type);
205 variable = new LocalTemporary (type);
212 public class NamedArgument : MovableArgument
214 public readonly string Name;
215 readonly Location loc;
217 public NamedArgument (string name, Location loc, Expression expr)
218 : this (name, loc, expr, AType.None)
222 public NamedArgument (string name, Location loc, Expression expr, AType modifier)
223 : base (expr, modifier)
229 public override Expression CreateExpressionTree (ResolveContext ec)
231 ec.Report.Error (853, loc, "An expression tree cannot contain named argument");
232 return base.CreateExpressionTree (ec);
235 public Location Location {
240 public class Arguments
242 sealed class ArgumentsOrdered : Arguments
244 readonly List<MovableArgument> ordered;
246 public ArgumentsOrdered (Arguments args)
250 ordered = new List<MovableArgument> ();
253 public void AddOrdered (MovableArgument arg)
258 public override Arguments Emit (EmitContext ec, bool dup_args, bool prepareAwait)
260 foreach (var a in ordered) {
262 a.EmitToField (ec, false);
264 a.EmitToVariable (ec);
267 return base.Emit (ec, dup_args, prepareAwait);
271 // Try not to add any more instances to this class, it's allocated a lot
274 public Arguments (int capacity)
276 args = new List<Argument> (capacity);
279 private Arguments (List<Argument> args)
284 public void Add (Argument arg)
289 public void AddRange (Arguments args)
291 this.args.AddRange (args.args);
294 public bool ContainsEmitWithAwait ()
296 foreach (var arg in args) {
297 if (arg.Expr.ContainsEmitWithAwait ())
304 public ArrayInitializer CreateDynamicBinderArguments (ResolveContext rc)
306 Location loc = Location.Null;
307 var all = new ArrayInitializer (args.Count, loc);
309 MemberAccess binder = DynamicExpressionStatement.GetBinderNamespace (loc);
311 foreach (Argument a in args) {
312 Arguments dargs = new Arguments (2);
314 // CSharpArgumentInfoFlags.None = 0
315 const string info_flags_enum = "CSharpArgumentInfoFlags";
316 Expression info_flags = new IntLiteral (rc.BuiltinTypes, 0, loc);
318 if (a.Expr is Constant) {
319 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
320 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "Constant", loc));
321 } else if (a.ArgType == Argument.AType.Ref) {
322 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
323 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsRef", loc));
324 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
325 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc));
326 } else if (a.ArgType == Argument.AType.Out) {
327 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
328 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsOut", loc));
329 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
330 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc));
331 } else if (a.ArgType == Argument.AType.DynamicTypeName) {
332 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
333 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsStaticType", loc));
336 var arg_type = a.Expr.Type;
338 if (arg_type.BuiltinType != BuiltinTypeSpec.Type.Dynamic && arg_type != InternalType.NullLiteral) {
339 MethodGroupExpr mg = a.Expr as MethodGroupExpr;
341 rc.Report.Error (1976, a.Expr.Location,
342 "The method group `{0}' cannot be used as an argument of dynamic operation. Consider using parentheses to invoke the method",
344 } else if (arg_type == InternalType.AnonymousMethod) {
345 rc.Report.Error (1977, a.Expr.Location,
346 "An anonymous method or lambda expression cannot be used as an argument of dynamic operation. Consider using a cast");
347 } else if (arg_type.Kind == MemberKind.Void || arg_type == InternalType.Arglist || arg_type.IsPointer) {
348 rc.Report.Error (1978, a.Expr.Location,
349 "An expression of type `{0}' cannot be used as an argument of dynamic operation",
350 arg_type.GetSignatureForError ());
353 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
354 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc));
358 NamedArgument na = a as NamedArgument;
360 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
361 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "NamedArgument", loc));
363 named_value = na.Name;
368 dargs.Add (new Argument (info_flags));
369 dargs.Add (new Argument (new StringLiteral (rc.BuiltinTypes, named_value, loc)));
370 all.Add (new Invocation (new MemberAccess (new MemberAccess (binder, "CSharpArgumentInfo", loc), "Create", loc), dargs));
376 public static Arguments CreateForExpressionTree (ResolveContext ec, Arguments args, params Expression[] e)
378 Arguments all = new Arguments ((args == null ? 0 : args.Count) + e.Length);
379 for (int i = 0; i < e.Length; ++i) {
381 all.Add (new Argument (e[i]));
385 foreach (Argument a in args.args) {
386 Expression tree_arg = a.CreateExpressionTree (ec);
387 if (tree_arg != null)
388 all.Add (new Argument (tree_arg));
395 public void CheckArrayAsAttribute (CompilerContext ctx)
397 foreach (Argument arg in args) {
398 // Type is undefined (was error 246)
399 if (arg.Type == null)
402 if (arg.Type.IsArray)
403 ctx.Report.Warning (3016, 1, arg.Expr.Location, "Arrays as attribute arguments are not CLS-compliant");
407 public Arguments Clone (CloneContext ctx)
409 Arguments cloned = new Arguments (args.Count);
410 foreach (Argument a in args)
411 cloned.Add (a.Clone (ctx));
417 get { return args.Count; }
421 // Emits a list of resolved Arguments
423 public void Emit (EmitContext ec)
425 Emit (ec, false, false);
429 // if `dup_args' is true or any of arguments contains await.
430 // A copy of all arguments will be returned to the caller
432 public virtual Arguments Emit (EmitContext ec, bool dup_args, bool prepareAwait)
436 if ((dup_args && Count != 0) || prepareAwait)
437 dups = new List<Argument> (Count);
442 foreach (Argument a in args) {
444 dups.Add (a.EmitToField (ec, true));
454 if (a.Expr.IsSideEffectFree) {
456 // No need to create a temporary variable for side effect free expressions. I assume
457 // all side-effect free expressions are cheap, this has to be tweaked when we become
458 // more aggressive on detection
462 ec.Emit (OpCodes.Dup);
464 // TODO: Release local temporary on next Emit
465 // Need to add a flag to argument to indicate this
466 lt = new LocalTemporary (a.Type);
469 dups.Add (new Argument (lt, a.ArgType));
474 return new Arguments (dups);
479 public List<Argument>.Enumerator GetEnumerator ()
481 return args.GetEnumerator ();
485 // At least one argument is of dynamic type
487 public bool HasDynamic {
489 foreach (Argument a in args) {
490 if (a.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic && !a.IsByRef)
499 // At least one argument is named argument
501 public bool HasNamed {
503 foreach (Argument a in args) {
504 if (a is NamedArgument)
513 public void Insert (int index, Argument arg)
515 args.Insert (index, arg);
518 public static System.Linq.Expressions.Expression[] MakeExpression (Arguments args, BuilderContext ctx)
520 if (args == null || args.Count == 0)
523 var exprs = new System.Linq.Expressions.Expression [args.Count];
524 for (int i = 0; i < exprs.Length; ++i) {
525 Argument a = args.args [i];
526 exprs[i] = a.Expr.MakeExpression (ctx);
533 // For named arguments when the order of execution is different
534 // to order of invocation
536 public Arguments MarkOrderedArgument (NamedArgument a)
539 // An expression has no effect on left-to-right execution
541 if (a.Expr.IsSideEffectFree)
544 ArgumentsOrdered ra = this as ArgumentsOrdered;
546 ra = new ArgumentsOrdered (this);
548 for (int i = 0; i < args.Count; ++i) {
554 // When the argument is filled later by default expression
559 var ma = la as MovableArgument;
561 ma = new MovableArgument (la);
574 // Returns dynamic when at least one argument is of dynamic type
576 public void Resolve (ResolveContext ec, out bool dynamic)
579 foreach (Argument a in args) {
581 if (a.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic && !a.IsByRef)
586 public void RemoveAt (int index)
588 args.RemoveAt (index);
591 public Argument this [int index] {
592 get { return args [index]; }
593 set { args [index] = value; }