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.Collections.Generic;
16 using IKVM.Reflection.Emit;
18 using System.Reflection.Emit;
24 // Argument expression used for invocation
28 public enum AType : byte
31 Ref = 1, // ref modifier used
32 Out = 2, // out modifier used
33 Default = 3, // argument created from default parameter value
34 DynamicTypeName = 4, // System.Type argument for dynamic binding
35 ExtensionType = 5, // Instance expression inserted as the first argument
38 public readonly AType ArgType;
39 public Expression Expr;
41 public Argument (Expression expr, AType type)
47 public Argument (Expression expr)
50 throw new ArgumentNullException ();
58 get { return ArgType == AType.Ref || ArgType == AType.Out; }
61 public bool IsDefaultArgument {
62 get { return ArgType == AType.Default; }
65 public Parameter.Modifier Modifier {
69 return Parameter.Modifier.OUT;
72 return Parameter.Modifier.REF;
75 return Parameter.Modifier.NONE;
80 public TypeSpec Type {
81 get { return Expr.Type; }
86 public Argument Clone (Expression expr)
88 Argument a = (Argument) MemberwiseClone ();
93 public Argument Clone (CloneContext clonectx)
95 return Clone (Expr.Clone (clonectx));
98 public virtual Expression CreateExpressionTree (ResolveContext ec)
100 if (ArgType == AType.Default)
101 ec.Report.Error (854, Expr.Location, "An expression tree cannot contain an invocation which uses optional parameter");
103 return Expr.CreateExpressionTree (ec);
107 public virtual void Emit (EmitContext ec)
114 AddressOp mode = AddressOp.Store;
115 if (ArgType == AType.Ref)
116 mode |= AddressOp.Load;
118 IMemoryLocation ml = (IMemoryLocation) Expr;
119 ml.AddressOf (ec, mode);
122 public Argument EmitToField (EmitContext ec)
124 var res = Expr.EmitToField (ec);
125 return res == Expr ? this : new Argument (res, ArgType);
128 public string GetSignatureForError ()
130 if (Expr.eclass == ExprClass.MethodGroup)
131 return Expr.ExprClassName;
133 return TypeManager.CSharpName (Expr.Type);
136 public bool ResolveMethodGroup (ResolveContext ec)
138 SimpleName sn = Expr as SimpleName;
140 Expr = sn.GetMethodGroup ();
142 // FIXME: csc doesn't report any error if you try to use `ref' or
143 // `out' in a delegate creation expression.
144 Expr = Expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
151 public void Resolve (ResolveContext ec)
153 // using (ec.With (ResolveContext.Options.DoFlowAnalysis, true)) {
154 // Verify that the argument is readable
155 if (ArgType != AType.Out)
156 Expr = Expr.Resolve (ec);
158 // Verify that the argument is writeable
159 if (Expr != null && IsByRef)
160 Expr = Expr.ResolveLValue (ec, EmptyExpression.OutAccess);
163 Expr = ErrorExpression.Instance;
168 public class MovableArgument : Argument
170 LocalTemporary variable;
172 public MovableArgument (Argument arg)
173 : this (arg.Expr, arg.ArgType)
177 protected MovableArgument (Expression expr, AType modifier)
178 : base (expr, modifier)
182 public override void Emit (EmitContext ec)
184 // TODO: Should guard against multiple emits
187 // Release temporary variable when used
188 if (variable != null)
189 variable.Release (ec);
192 public void EmitToVariable (EmitContext ec)
194 var type = Expr.Type;
196 var ml = (IMemoryLocation) Expr;
197 ml.AddressOf (ec, AddressOp.LoadStore);
198 type = ReferenceContainer.MakeType (ec.Module, type);
203 variable = new LocalTemporary (type);
210 public class NamedArgument : MovableArgument
212 public readonly string Name;
213 readonly Location loc;
215 public NamedArgument (string name, Location loc, Expression expr)
216 : this (name, loc, expr, AType.None)
220 public NamedArgument (string name, Location loc, Expression expr, AType modifier)
221 : base (expr, modifier)
227 public override Expression CreateExpressionTree (ResolveContext ec)
229 ec.Report.Error (853, loc, "An expression tree cannot contain named argument");
230 return base.CreateExpressionTree (ec);
233 public Location Location {
238 public class Arguments
240 sealed class ArgumentsOrdered : Arguments
242 readonly List<MovableArgument> ordered;
244 public ArgumentsOrdered (Arguments args)
248 ordered = new List<MovableArgument> ();
251 public void AddOrdered (MovableArgument arg)
256 public override Arguments Emit (EmitContext ec, bool dup_args, bool prepareAwait)
258 foreach (var a in ordered) {
262 a.EmitToVariable (ec);
265 return base.Emit (ec, dup_args, prepareAwait);
269 // Try not to add any more instances to this class, it's allocated a lot
272 public Arguments (int capacity)
274 args = new List<Argument> (capacity);
277 private Arguments (List<Argument> args)
282 public void Add (Argument arg)
287 public void AddRange (Arguments args)
289 this.args.AddRange (args.args);
292 public bool ContainsEmitWithAwait ()
294 foreach (var arg in args) {
295 if (arg.Expr.ContainsEmitWithAwait ())
302 public ArrayInitializer CreateDynamicBinderArguments (ResolveContext rc)
304 Location loc = Location.Null;
305 var all = new ArrayInitializer (args.Count, loc);
307 MemberAccess binder = DynamicExpressionStatement.GetBinderNamespace (loc);
309 foreach (Argument a in args) {
310 Arguments dargs = new Arguments (2);
312 // CSharpArgumentInfoFlags.None = 0
313 const string info_flags_enum = "CSharpArgumentInfoFlags";
314 Expression info_flags = new IntLiteral (rc.BuiltinTypes, 0, loc);
316 if (a.Expr is Constant) {
317 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
318 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "Constant", loc), loc);
319 } else if (a.ArgType == Argument.AType.Ref) {
320 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
321 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsRef", loc), loc);
322 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
323 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc), loc);
324 } else if (a.ArgType == Argument.AType.Out) {
325 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
326 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsOut", loc), loc);
327 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
328 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc), loc);
329 } else if (a.ArgType == Argument.AType.DynamicTypeName) {
330 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
331 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsStaticType", loc), loc);
334 var arg_type = a.Expr.Type;
336 if (arg_type.BuiltinType != BuiltinTypeSpec.Type.Dynamic && arg_type != InternalType.NullLiteral) {
337 MethodGroupExpr mg = a.Expr as MethodGroupExpr;
339 rc.Report.Error (1976, a.Expr.Location,
340 "The method group `{0}' cannot be used as an argument of dynamic operation. Consider using parentheses to invoke the method",
342 } else if (arg_type == InternalType.AnonymousMethod) {
343 rc.Report.Error (1977, a.Expr.Location,
344 "An anonymous method or lambda expression cannot be used as an argument of dynamic operation. Consider using a cast");
345 } else if (arg_type.Kind == MemberKind.Void || arg_type == InternalType.Arglist || arg_type.IsPointer) {
346 rc.Report.Error (1978, a.Expr.Location,
347 "An expression of type `{0}' cannot be used as an argument of dynamic operation",
348 TypeManager.CSharpName (arg_type));
351 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
352 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc), loc);
356 NamedArgument na = a as NamedArgument;
358 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
359 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "NamedArgument", loc), loc);
361 named_value = na.Name;
366 dargs.Add (new Argument (info_flags));
367 dargs.Add (new Argument (new StringLiteral (rc.BuiltinTypes, named_value, loc)));
368 all.Add (new Invocation (new MemberAccess (new MemberAccess (binder, "CSharpArgumentInfo", loc), "Create", loc), dargs));
374 public static Arguments CreateForExpressionTree (ResolveContext ec, Arguments args, params Expression[] e)
376 Arguments all = new Arguments ((args == null ? 0 : args.Count) + e.Length);
377 for (int i = 0; i < e.Length; ++i) {
379 all.Add (new Argument (e[i]));
383 foreach (Argument a in args.args) {
384 Expression tree_arg = a.CreateExpressionTree (ec);
385 if (tree_arg != null)
386 all.Add (new Argument (tree_arg));
393 public void CheckArrayAsAttribute (CompilerContext ctx)
395 foreach (Argument arg in args) {
396 // Type is undefined (was error 246)
397 if (arg.Type == null)
400 if (arg.Type.IsArray)
401 ctx.Report.Warning (3016, 1, arg.Expr.Location, "Arrays as attribute arguments are not CLS-compliant");
405 public Arguments Clone (CloneContext ctx)
407 Arguments cloned = new Arguments (args.Count);
408 foreach (Argument a in args)
409 cloned.Add (a.Clone (ctx));
415 get { return args.Count; }
419 // Emits a list of resolved Arguments
421 public void Emit (EmitContext ec)
423 Emit (ec, false, false);
427 // if `dup_args' is true or any of arguments contains await.
428 // A copy of all arguments will be returned to the caller
430 public virtual Arguments Emit (EmitContext ec, bool dup_args, bool prepareAwait)
434 if ((dup_args && Count != 0) || prepareAwait)
435 dups = new List<Argument> (Count);
440 foreach (Argument a in args) {
442 dups.Add (a.EmitToField (ec));
452 if (a.Expr.IsSideEffectFree) {
454 // No need to create a temporary variable for side effect free expressions. I assume
455 // all side-effect free expressions are cheap, this has to be tweaked when we become
456 // more aggressive on detection
460 ec.Emit (OpCodes.Dup);
462 // TODO: Release local temporary on next Emit
463 // Need to add a flag to argument to indicate this
464 lt = new LocalTemporary (a.Type);
467 dups.Add (new Argument (lt, a.ArgType));
472 return new Arguments (dups);
477 public List<Argument>.Enumerator GetEnumerator ()
479 return args.GetEnumerator ();
483 // At least one argument is of dynamic type
485 public bool HasDynamic {
487 foreach (Argument a in args) {
488 if (a.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic && !a.IsByRef)
497 // At least one argument is named argument
499 public bool HasNamed {
501 foreach (Argument a in args) {
502 if (a is NamedArgument)
511 public void Insert (int index, Argument arg)
513 args.Insert (index, arg);
516 public static System.Linq.Expressions.Expression[] MakeExpression (Arguments args, BuilderContext ctx)
518 if (args == null || args.Count == 0)
521 var exprs = new System.Linq.Expressions.Expression [args.Count];
522 for (int i = 0; i < exprs.Length; ++i) {
523 Argument a = args.args [i];
524 exprs[i] = a.Expr.MakeExpression (ctx);
531 // For named arguments when the order of execution is different
532 // to order of invocation
534 public Arguments MarkOrderedArgument (NamedArgument a)
537 // An expression has no effect on left-to-right execution
539 if (a.Expr.IsSideEffectFree)
542 ArgumentsOrdered ra = this as ArgumentsOrdered;
544 ra = new ArgumentsOrdered (this);
546 for (int i = 0; i < args.Count; ++i) {
551 var ma = la as MovableArgument;
553 ma = new MovableArgument (la);
566 // Returns dynamic when at least one argument is of dynamic type
568 public void Resolve (ResolveContext ec, out bool dynamic)
571 foreach (Argument a in args) {
573 if (a.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic && !a.IsByRef)
578 public void RemoveAt (int index)
580 args.RemoveAt (index);
583 public Argument this [int index] {
584 get { return args [index]; }
585 set { args [index] = value; }