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 if (Expr == EmptyExpression.Null)
134 // using (ec.With (ResolveContext.Options.DoFlowAnalysis, true)) {
135 // Verify that the argument is readable
136 if (ArgType != AType.Out)
137 Expr = Expr.Resolve (ec);
139 // Verify that the argument is writeable
140 if (Expr != null && IsByRef)
141 Expr = Expr.ResolveLValue (ec, EmptyExpression.OutAccess);
144 Expr = EmptyExpression.Null;
148 public virtual void Emit (EmitContext ec)
155 AddressOp mode = AddressOp.Store;
156 if (ArgType == AType.Ref)
157 mode |= AddressOp.Load;
159 IMemoryLocation ml = (IMemoryLocation) Expr;
160 ml.AddressOf (ec, mode);
164 public class NamedArgument : Argument
166 public readonly string Name;
167 readonly Location loc;
168 LocalTemporary variable;
170 public NamedArgument (string name, Location loc, Expression expr)
171 : this (name, loc, expr, AType.None)
175 public NamedArgument (string name, Location loc, Expression expr, AType modifier)
176 : base (expr, modifier)
182 public override Expression CreateExpressionTree (ResolveContext ec)
184 ec.Report.Error (853, loc, "An expression tree cannot contain named argument");
185 return base.CreateExpressionTree (ec);
188 public override void Emit (EmitContext ec)
190 // TODO: Should guard against multiple emits
193 // Release temporary variable when used
194 if (variable != null)
195 variable.Release (ec);
198 public void EmitAssign (EmitContext ec)
200 var type = Expr.Type;
202 var ml = (IMemoryLocation) Expr;
203 ml.AddressOf (ec, AddressOp.Load);
204 type = ReferenceContainer.MakeType (ec.Module, type);
209 variable = new LocalTemporary (type);
215 public Location Location {
220 public class Arguments
222 sealed class ArgumentsOrdered : Arguments
224 List<NamedArgument> ordered;
226 public ArgumentsOrdered (Arguments args)
230 ordered = new List<NamedArgument> ();
233 public void AddOrdered (NamedArgument na)
238 public override Expression[] Emit (EmitContext ec, bool dup_args)
240 foreach (NamedArgument na in ordered)
243 return base.Emit (ec, dup_args);
247 // Try not to add any more instances to this class, it's allocated a lot
250 public Arguments (int capacity)
252 args = new List<Argument> (capacity);
255 public void Add (Argument arg)
260 public void AddRange (Arguments args)
262 this.args.AddRange (args.args);
265 public ArrayInitializer CreateDynamicBinderArguments (ResolveContext rc)
267 Location loc = Location.Null;
268 var all = new ArrayInitializer (args.Count, loc);
270 MemberAccess binder = DynamicExpressionStatement.GetBinderNamespace (loc);
272 foreach (Argument a in args) {
273 Arguments dargs = new Arguments (2);
275 // CSharpArgumentInfoFlags.None = 0
276 const string info_flags_enum = "CSharpArgumentInfoFlags";
277 Expression info_flags = new IntLiteral (rc.BuildinTypes, 0, loc);
279 if (a.Expr is Constant) {
280 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
281 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "Constant", loc), loc);
282 } else if (a.ArgType == Argument.AType.Ref) {
283 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
284 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsRef", loc), loc);
285 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
286 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc), loc);
287 } else if (a.ArgType == Argument.AType.Out) {
288 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
289 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsOut", loc), loc);
290 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
291 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc), loc);
292 } else if (a.ArgType == Argument.AType.DynamicTypeName) {
293 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
294 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "IsStaticType", loc), loc);
297 var arg_type = a.Expr.Type;
299 if (arg_type.BuildinType != BuildinTypeSpec.Type.Dynamic && arg_type != InternalType.NullLiteral) {
300 MethodGroupExpr mg = a.Expr as MethodGroupExpr;
302 rc.Report.Error (1976, a.Expr.Location,
303 "The method group `{0}' cannot be used as an argument of dynamic operation. Consider using parentheses to invoke the method",
305 } else if (arg_type == InternalType.AnonymousMethod) {
306 rc.Report.Error (1977, a.Expr.Location,
307 "An anonymous method or lambda expression cannot be used as an argument of dynamic operation. Consider using a cast");
308 } else if (arg_type.Kind == MemberKind.Void || arg_type == InternalType.Arglist || arg_type.IsPointer) {
309 rc.Report.Error (1978, a.Expr.Location,
310 "An expression of type `{0}' cannot be used as an argument of dynamic operation",
311 TypeManager.CSharpName (arg_type));
314 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
315 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "UseCompileTimeType", loc), loc);
319 NamedArgument na = a as NamedArgument;
321 info_flags = new Binary (Binary.Operator.BitwiseOr, info_flags,
322 new MemberAccess (new MemberAccess (binder, info_flags_enum, loc), "NamedArgument", loc), loc);
324 named_value = na.Name;
329 dargs.Add (new Argument (info_flags));
330 dargs.Add (new Argument (new StringLiteral (rc.BuildinTypes, named_value, loc)));
331 all.Add (new Invocation (new MemberAccess (new MemberAccess (binder, "CSharpArgumentInfo", loc), "Create", loc), dargs));
337 public static Arguments CreateForExpressionTree (ResolveContext ec, Arguments args, params Expression[] e)
339 Arguments all = new Arguments ((args == null ? 0 : args.Count) + e.Length);
340 for (int i = 0; i < e.Length; ++i) {
342 all.Add (new Argument (e[i]));
346 foreach (Argument a in args.args) {
347 Expression tree_arg = a.CreateExpressionTree (ec);
348 if (tree_arg != null)
349 all.Add (new Argument (tree_arg));
356 public void CheckArrayAsAttribute (CompilerContext ctx)
358 foreach (Argument arg in args) {
359 // Type is undefined (was error 246)
360 if (arg.Type == null)
363 if (arg.Type.IsArray)
364 ctx.Report.Warning (3016, 1, arg.Expr.Location, "Arrays as attribute arguments are not CLS-compliant");
368 public Arguments Clone (CloneContext ctx)
370 Arguments cloned = new Arguments (args.Count);
371 foreach (Argument a in args)
372 cloned.Add (a.Clone (ctx));
378 get { return args.Count; }
382 // Emits a list of resolved Arguments
384 public void Emit (EmitContext ec)
390 // if `dup_args' is true, a copy of the arguments will be left
391 // on the stack and return value will contain an array of access
393 // NOTE: It's caller responsibility is to release temporary variables
395 public virtual Expression[] Emit (EmitContext ec, bool dup_args)
399 if (dup_args && Count != 0)
400 temps = new Expression [Count];
406 foreach (Argument a in args) {
411 if (a.Expr is Constant || a.Expr is This) {
413 // No need to create a temporary variable for constants
417 ec.Emit (OpCodes.Dup);
418 temps[i] = lt = new LocalTemporary (a.Type);
428 public List<Argument>.Enumerator GetEnumerator ()
430 return args.GetEnumerator ();
434 // At least one argument is of dynamic type
436 public bool HasDynamic {
438 foreach (Argument a in args) {
439 if (a.Type.BuildinType == BuildinTypeSpec.Type.Dynamic && !a.IsByRef)
448 // At least one argument is named argument
450 public bool HasNamed {
452 foreach (Argument a in args) {
453 if (a is NamedArgument)
462 public void Insert (int index, Argument arg)
464 args.Insert (index, arg);
467 public static System.Linq.Expressions.Expression[] MakeExpression (Arguments args, BuilderContext ctx)
469 if (args == null || args.Count == 0)
472 var exprs = new System.Linq.Expressions.Expression [args.Count];
473 for (int i = 0; i < exprs.Length; ++i) {
474 Argument a = args.args [i];
475 exprs[i] = a.Expr.MakeExpression (ctx);
482 // For named arguments when the order of execution is different
483 // to order of invocation
485 public Arguments MarkOrderedArgument (NamedArgument a)
488 // Constant expression have no effect on left-to-right execution
490 if (a.Expr is Constant)
493 ArgumentsOrdered ra = this as ArgumentsOrdered;
495 ra = new ArgumentsOrdered (this);
502 // Returns dynamic when at least one argument is of dynamic type
504 public void Resolve (ResolveContext ec, out bool dynamic)
507 foreach (Argument a in args) {
509 if (a.Type.BuildinType == BuildinTypeSpec.Type.Dynamic && !a.IsByRef)
514 public void RemoveAt (int index)
516 args.RemoveAt (index);
519 public Argument this [int index] {
520 get { return args [index]; }
521 set { args [index] = value; }