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);
106 public string GetSignatureForError ()
108 if (Expr.eclass == ExprClass.MethodGroup)
109 return Expr.ExprClassName;
111 return TypeManager.CSharpName (Expr.Type);
114 public bool ResolveMethodGroup (ResolveContext ec)
116 SimpleName sn = Expr as SimpleName;
118 Expr = sn.GetMethodGroup ();
120 // FIXME: csc doesn't report any error if you try to use `ref' or
121 // `out' in a delegate creation expression.
122 Expr = Expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
129 public void Resolve (ResolveContext ec)
131 // using (ec.With (ResolveContext.Options.DoFlowAnalysis, true)) {
132 // Verify that the argument is readable
133 if (ArgType != AType.Out)
134 Expr = Expr.Resolve (ec);
136 // Verify that the argument is writeable
137 if (Expr != null && IsByRef)
138 Expr = Expr.ResolveLValue (ec, EmptyExpression.OutAccess);
141 Expr = ErrorExpression.Instance;
145 public virtual void Emit (EmitContext ec)
152 AddressOp mode = AddressOp.Store;
153 if (ArgType == AType.Ref)
154 mode |= AddressOp.Load;
156 IMemoryLocation ml = (IMemoryLocation) Expr;
157 ml.AddressOf (ec, mode);
161 public class MovableArgument : Argument
163 LocalTemporary variable;
165 public MovableArgument (Argument arg)
166 : this (arg.Expr, arg.ArgType)
170 protected MovableArgument (Expression expr, AType modifier)
171 : base (expr, modifier)
175 public override void Emit (EmitContext ec)
177 // TODO: Should guard against multiple emits
180 // Release temporary variable when used
181 if (variable != null)
182 variable.Release (ec);
185 public void EmitAssign (EmitContext ec)
187 var type = Expr.Type;
189 var ml = (IMemoryLocation) Expr;
190 ml.AddressOf (ec, AddressOp.LoadStore);
191 type = ReferenceContainer.MakeType (ec.Module, type);
196 variable = new LocalTemporary (type);
203 public class NamedArgument : MovableArgument
205 public readonly string Name;
206 readonly Location loc;
208 public NamedArgument (string name, Location loc, Expression expr)
209 : this (name, loc, expr, AType.None)
213 public NamedArgument (string name, Location loc, Expression expr, AType modifier)
214 : base (expr, modifier)
220 public override Expression CreateExpressionTree (ResolveContext ec)
222 ec.Report.Error (853, loc, "An expression tree cannot contain named argument");
223 return base.CreateExpressionTree (ec);
226 public Location Location {
231 public class Arguments
233 sealed class ArgumentsOrdered : Arguments
235 readonly List<MovableArgument> ordered;
237 public ArgumentsOrdered (Arguments args)
241 ordered = new List<MovableArgument> ();
244 public void AddOrdered (MovableArgument arg)
249 public override Expression[] Emit (EmitContext ec, bool dup_args)
251 foreach (var a in ordered) {
255 return base.Emit (ec, dup_args);
259 // Try not to add any more instances to this class, it's allocated a lot
262 public Arguments (int capacity)
264 args = new List<Argument> (capacity);
267 public void Add (Argument arg)
272 public void AddRange (Arguments args)
274 this.args.AddRange (args.args);
277 public ArrayInitializer CreateDynamicBinderArguments (ResolveContext rc)
279 Location loc = Location.Null;
280 var all = new ArrayInitializer (args.Count, loc);
282 MemberAccess binder = DynamicExpressionStatement.GetBinderNamespace (loc);
284 foreach (Argument a in args) {
285 Arguments dargs = new Arguments (2);
287 // CSharpArgumentInfoFlags.None = 0
288 const string info_flags_enum = "CSharpArgumentInfoFlags";
289 Expression info_flags = new IntLiteral (rc.BuiltinTypes, 0, loc);
291 if (a.Expr is Constant) {
292 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
293 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "Constant", loc), loc);
294 } else if (a.ArgType == Argument.AType.Ref) {
295 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
296 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsRef", loc), loc);
297 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
298 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc), loc);
299 } else if (a.ArgType == Argument.AType.Out) {
300 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
301 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsOut", loc), loc);
302 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
303 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc), loc);
304 } else if (a.ArgType == Argument.AType.DynamicTypeName) {
305 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
306 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsStaticType", loc), loc);
309 var arg_type = a.Expr.Type;
311 if (arg_type.BuiltinType != BuiltinTypeSpec.Type.Dynamic && arg_type != InternalType.NullLiteral) {
312 MethodGroupExpr mg = a.Expr as MethodGroupExpr;
314 rc.Report.Error (1976, a.Expr.Location,
315 "The method group `{0}' cannot be used as an argument of dynamic operation. Consider using parentheses to invoke the method",
317 } else if (arg_type == InternalType.AnonymousMethod) {
318 rc.Report.Error (1977, a.Expr.Location,
319 "An anonymous method or lambda expression cannot be used as an argument of dynamic operation. Consider using a cast");
320 } else if (arg_type.Kind == MemberKind.Void || arg_type == InternalType.Arglist || arg_type.IsPointer) {
321 rc.Report.Error (1978, a.Expr.Location,
322 "An expression of type `{0}' cannot be used as an argument of dynamic operation",
323 TypeManager.CSharpName (arg_type));
326 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
327 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc), loc);
331 NamedArgument na = a as NamedArgument;
333 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
334 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "NamedArgument", loc), loc);
336 named_value = na.Name;
341 dargs.Add (new Argument (info_flags));
342 dargs.Add (new Argument (new StringLiteral (rc.BuiltinTypes, named_value, loc)));
343 all.Add (new Invocation (new MemberAccess (new MemberAccess (binder, "CSharpArgumentInfo", loc), "Create", loc), dargs));
349 public static Arguments CreateForExpressionTree (ResolveContext ec, Arguments args, params Expression[] e)
351 Arguments all = new Arguments ((args == null ? 0 : args.Count) + e.Length);
352 for (int i = 0; i < e.Length; ++i) {
354 all.Add (new Argument (e[i]));
358 foreach (Argument a in args.args) {
359 Expression tree_arg = a.CreateExpressionTree (ec);
360 if (tree_arg != null)
361 all.Add (new Argument (tree_arg));
368 public void CheckArrayAsAttribute (CompilerContext ctx)
370 foreach (Argument arg in args) {
371 // Type is undefined (was error 246)
372 if (arg.Type == null)
375 if (arg.Type.IsArray)
376 ctx.Report.Warning (3016, 1, arg.Expr.Location, "Arrays as attribute arguments are not CLS-compliant");
380 public Arguments Clone (CloneContext ctx)
382 Arguments cloned = new Arguments (args.Count);
383 foreach (Argument a in args)
384 cloned.Add (a.Clone (ctx));
390 get { return args.Count; }
394 // Emits a list of resolved Arguments
396 public void Emit (EmitContext ec)
402 // if `dup_args' is true, a copy of the arguments will be left
403 // on the stack and return value will contain an array of access
405 // NOTE: It's caller responsibility is to release temporary variables
407 public virtual Expression[] Emit (EmitContext ec, bool dup_args)
411 if (dup_args && Count != 0)
412 temps = new Expression [Count];
418 foreach (Argument a in args) {
423 if (a.Expr is Constant || a.Expr is This) {
425 // No need to create a temporary variable for constants
429 ec.Emit (OpCodes.Dup);
430 temps[i] = lt = new LocalTemporary (a.Type);
440 public List<Argument>.Enumerator GetEnumerator ()
442 return args.GetEnumerator ();
446 // At least one argument is of dynamic type
448 public bool HasDynamic {
450 foreach (Argument a in args) {
451 if (a.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic && !a.IsByRef)
460 // At least one argument is named argument
462 public bool HasNamed {
464 foreach (Argument a in args) {
465 if (a is NamedArgument)
474 public void Insert (int index, Argument arg)
476 args.Insert (index, arg);
479 public static System.Linq.Expressions.Expression[] MakeExpression (Arguments args, BuilderContext ctx)
481 if (args == null || args.Count == 0)
484 var exprs = new System.Linq.Expressions.Expression [args.Count];
485 for (int i = 0; i < exprs.Length; ++i) {
486 Argument a = args.args [i];
487 exprs[i] = a.Expr.MakeExpression (ctx);
494 // For named arguments when the order of execution is different
495 // to order of invocation
497 public Arguments MarkOrderedArgument (NamedArgument a)
500 // An expression has no effect on left-to-right execution
502 if (a.Expr.IsSideEffectFree)
505 ArgumentsOrdered ra = this as ArgumentsOrdered;
507 ra = new ArgumentsOrdered (this);
509 for (int i = 0; i < args.Count; ++i) {
514 var ma = la as MovableArgument;
516 ma = new MovableArgument (la);
529 // Returns dynamic when at least one argument is of dynamic type
531 public void Resolve (ResolveContext ec, out bool dynamic)
534 foreach (Argument a in args) {
536 if (a.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic && !a.IsByRef)
541 public void RemoveAt (int index)
543 args.RemoveAt (index);
546 public Argument this [int index] {
547 get { return args [index]; }
548 set { args [index] = value; }