2010-01-12 Zoltan Varga <vargaz@gmail.com>
[mcs.git] / mcs / expression.cs
blobe6f8c2e6d608dd6e6d7c4726997a518826fefd6a
1 //
2 // expression.cs: Expression representation for the IL tree.
3 //
4 // Author:
5 // Miguel de Icaza (miguel@ximian.com)
6 // Marek Safar (marek.safar@gmail.com)
7 //
8 // Copyright 2001, 2002, 2003 Ximian, Inc.
9 // Copyright 2003-2008 Novell, Inc.
11 #define USE_OLD
13 namespace Mono.CSharp {
14 using System;
15 using System.Collections;
16 using System.Reflection;
17 using System.Reflection.Emit;
18 using System.Text;
20 #if NET_4_0
21 using SLE = System.Linq.Expressions;
22 #endif
25 // This is an user operator expression, automatically created during
26 // resolve phase
28 public class UserOperatorCall : Expression {
29 public delegate Expression ExpressionTreeExpression (ResolveContext ec, MethodGroupExpr mg);
31 protected readonly Arguments arguments;
32 protected readonly MethodGroupExpr mg;
33 readonly ExpressionTreeExpression expr_tree;
35 public UserOperatorCall (MethodGroupExpr mg, Arguments args, ExpressionTreeExpression expr_tree, Location loc)
37 this.mg = mg;
38 this.arguments = args;
39 this.expr_tree = expr_tree;
41 type = TypeManager.TypeToCoreType (((MethodInfo) mg).ReturnType);
42 eclass = ExprClass.Value;
43 this.loc = loc;
46 public override Expression CreateExpressionTree (ResolveContext ec)
48 if (expr_tree != null)
49 return expr_tree (ec, mg);
51 Arguments args = Arguments.CreateForExpressionTree (ec, arguments,
52 new NullLiteral (loc),
53 mg.CreateExpressionTree (ec));
55 return CreateExpressionFactoryCall (ec, "Call", args);
58 protected override void CloneTo (CloneContext context, Expression target)
60 // Nothing to clone
63 public override Expression DoResolve (ResolveContext ec)
66 // We are born fully resolved
68 return this;
71 public override void Emit (EmitContext ec)
73 mg.EmitCall (ec, arguments);
76 #if NET_4_0
77 public override SLE.Expression MakeExpression (BuilderContext ctx)
79 return SLE.Expression.Call ((MethodInfo) mg, Arguments.MakeExpression (arguments, ctx));
81 #endif
83 public MethodGroupExpr Method {
84 get { return mg; }
87 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
89 arguments.MutateHoistedGenericType (storey);
90 mg.MutateHoistedGenericType (storey);
94 public class ParenthesizedExpression : Expression
96 public Expression Expr;
98 public ParenthesizedExpression (Expression expr)
100 Expr = expr;
101 loc = expr.Location;
104 public override Expression CreateExpressionTree (ResolveContext ec)
106 throw new NotSupportedException ("ET");
109 public override Expression DoResolve (ResolveContext ec)
111 Expr = Expr.Resolve (ec);
112 return Expr;
115 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
117 return Expr.DoResolveLValue (ec, right_side);
120 public override void Emit (EmitContext ec)
122 throw new Exception ("Should not happen");
125 protected override void CloneTo (CloneContext clonectx, Expression t)
127 ParenthesizedExpression target = (ParenthesizedExpression) t;
129 target.Expr = Expr.Clone (clonectx);
134 // Unary implements unary expressions.
136 public class Unary : Expression
138 public enum Operator : byte {
139 UnaryPlus, UnaryNegation, LogicalNot, OnesComplement,
140 AddressOf, TOP
143 static Type [] [] predefined_operators;
145 public readonly Operator Oper;
146 public Expression Expr;
147 Expression enum_conversion;
149 public Unary (Operator op, Expression expr)
151 Oper = op;
152 Expr = expr;
153 loc = expr.Location;
156 // <summary>
157 // This routine will attempt to simplify the unary expression when the
158 // argument is a constant.
159 // </summary>
160 Constant TryReduceConstant (ResolveContext ec, Constant e)
162 if (e is EmptyConstantCast)
163 return TryReduceConstant (ec, ((EmptyConstantCast) e).child);
165 if (e is SideEffectConstant) {
166 Constant r = TryReduceConstant (ec, ((SideEffectConstant) e).value);
167 return r == null ? null : new SideEffectConstant (r, e, r.Location);
170 Type expr_type = e.Type;
172 switch (Oper){
173 case Operator.UnaryPlus:
174 // Unary numeric promotions
175 if (expr_type == TypeManager.byte_type)
176 return new IntConstant (((ByteConstant)e).Value, e.Location);
177 if (expr_type == TypeManager.sbyte_type)
178 return new IntConstant (((SByteConstant)e).Value, e.Location);
179 if (expr_type == TypeManager.short_type)
180 return new IntConstant (((ShortConstant)e).Value, e.Location);
181 if (expr_type == TypeManager.ushort_type)
182 return new IntConstant (((UShortConstant)e).Value, e.Location);
183 if (expr_type == TypeManager.char_type)
184 return new IntConstant (((CharConstant)e).Value, e.Location);
186 // Predefined operators
187 if (expr_type == TypeManager.int32_type || expr_type == TypeManager.uint32_type ||
188 expr_type == TypeManager.int64_type || expr_type == TypeManager.uint64_type ||
189 expr_type == TypeManager.float_type || expr_type == TypeManager.double_type ||
190 expr_type == TypeManager.decimal_type) {
191 return e;
194 return null;
196 case Operator.UnaryNegation:
197 // Unary numeric promotions
198 if (expr_type == TypeManager.byte_type)
199 return new IntConstant (-((ByteConstant)e).Value, e.Location);
200 if (expr_type == TypeManager.sbyte_type)
201 return new IntConstant (-((SByteConstant)e).Value, e.Location);
202 if (expr_type == TypeManager.short_type)
203 return new IntConstant (-((ShortConstant)e).Value, e.Location);
204 if (expr_type == TypeManager.ushort_type)
205 return new IntConstant (-((UShortConstant)e).Value, e.Location);
206 if (expr_type == TypeManager.char_type)
207 return new IntConstant (-((CharConstant)e).Value, e.Location);
209 // Predefined operators
210 if (expr_type == TypeManager.int32_type) {
211 int value = ((IntConstant)e).Value;
212 if (value == int.MinValue) {
213 if (ec.ConstantCheckState) {
214 ConstantFold.Error_CompileTimeOverflow (ec, loc);
215 return null;
217 return e;
219 return new IntConstant (-value, e.Location);
221 if (expr_type == TypeManager.int64_type) {
222 long value = ((LongConstant)e).Value;
223 if (value == long.MinValue) {
224 if (ec.ConstantCheckState) {
225 ConstantFold.Error_CompileTimeOverflow (ec, loc);
226 return null;
228 return e;
230 return new LongConstant (-value, e.Location);
233 if (expr_type == TypeManager.uint32_type) {
234 UIntLiteral uil = e as UIntLiteral;
235 if (uil != null) {
236 if (uil.Value == 2147483648)
237 return new IntLiteral (int.MinValue, e.Location);
238 return new LongLiteral (-uil.Value, e.Location);
240 return new LongConstant (-((UIntConstant)e).Value, e.Location);
243 if (expr_type == TypeManager.uint64_type) {
244 ULongLiteral ull = e as ULongLiteral;
245 if (ull != null && ull.Value == 9223372036854775808)
246 return new LongLiteral (long.MinValue, e.Location);
247 return null;
250 if (expr_type == TypeManager.float_type) {
251 FloatLiteral fl = e as FloatLiteral;
252 // For better error reporting
253 if (fl != null)
254 return new FloatLiteral (-fl.Value, e.Location);
256 return new FloatConstant (-((FloatConstant)e).Value, e.Location);
258 if (expr_type == TypeManager.double_type) {
259 DoubleLiteral dl = e as DoubleLiteral;
260 // For better error reporting
261 if (dl != null)
262 return new DoubleLiteral (-dl.Value, e.Location);
264 return new DoubleConstant (-((DoubleConstant)e).Value, e.Location);
266 if (expr_type == TypeManager.decimal_type)
267 return new DecimalConstant (-((DecimalConstant)e).Value, e.Location);
269 return null;
271 case Operator.LogicalNot:
272 if (expr_type != TypeManager.bool_type)
273 return null;
275 bool b = (bool)e.GetValue ();
276 return new BoolConstant (!b, e.Location);
278 case Operator.OnesComplement:
279 // Unary numeric promotions
280 if (expr_type == TypeManager.byte_type)
281 return new IntConstant (~((ByteConstant)e).Value, e.Location);
282 if (expr_type == TypeManager.sbyte_type)
283 return new IntConstant (~((SByteConstant)e).Value, e.Location);
284 if (expr_type == TypeManager.short_type)
285 return new IntConstant (~((ShortConstant)e).Value, e.Location);
286 if (expr_type == TypeManager.ushort_type)
287 return new IntConstant (~((UShortConstant)e).Value, e.Location);
288 if (expr_type == TypeManager.char_type)
289 return new IntConstant (~((CharConstant)e).Value, e.Location);
291 // Predefined operators
292 if (expr_type == TypeManager.int32_type)
293 return new IntConstant (~((IntConstant)e).Value, e.Location);
294 if (expr_type == TypeManager.uint32_type)
295 return new UIntConstant (~((UIntConstant)e).Value, e.Location);
296 if (expr_type == TypeManager.int64_type)
297 return new LongConstant (~((LongConstant)e).Value, e.Location);
298 if (expr_type == TypeManager.uint64_type){
299 return new ULongConstant (~((ULongConstant)e).Value, e.Location);
301 if (e is EnumConstant) {
302 e = TryReduceConstant (ec, ((EnumConstant)e).Child);
303 if (e != null)
304 e = new EnumConstant (e, expr_type);
305 return e;
307 return null;
309 throw new Exception ("Can not constant fold: " + Oper.ToString());
312 protected Expression ResolveOperator (ResolveContext ec, Expression expr)
314 eclass = ExprClass.Value;
316 if (predefined_operators == null)
317 CreatePredefinedOperatorsTable ();
319 Type expr_type = expr.Type;
320 Expression best_expr;
323 // Primitive types first
325 if (TypeManager.IsPrimitiveType (expr_type)) {
326 best_expr = ResolvePrimitivePredefinedType (expr);
327 if (best_expr == null)
328 return null;
330 type = best_expr.Type;
331 Expr = best_expr;
332 return this;
336 // E operator ~(E x);
338 if (Oper == Operator.OnesComplement && TypeManager.IsEnumType (expr_type))
339 return ResolveEnumOperator (ec, expr);
341 return ResolveUserType (ec, expr);
344 protected virtual Expression ResolveEnumOperator (ResolveContext ec, Expression expr)
346 Type underlying_type = TypeManager.GetEnumUnderlyingType (expr.Type);
347 Expression best_expr = ResolvePrimitivePredefinedType (EmptyCast.Create (expr, underlying_type));
348 if (best_expr == null)
349 return null;
351 Expr = best_expr;
352 enum_conversion = Convert.ExplicitNumericConversion (new EmptyExpression (best_expr.Type), underlying_type);
353 type = expr.Type;
354 return EmptyCast.Create (this, type);
357 public override Expression CreateExpressionTree (ResolveContext ec)
359 return CreateExpressionTree (ec, null);
362 Expression CreateExpressionTree (ResolveContext ec, MethodGroupExpr user_op)
364 string method_name;
365 switch (Oper) {
366 case Operator.AddressOf:
367 Error_PointerInsideExpressionTree (ec);
368 return null;
369 case Operator.UnaryNegation:
370 if (ec.HasSet (ResolveContext.Options.CheckedScope) && user_op == null && !IsFloat (type))
371 method_name = "NegateChecked";
372 else
373 method_name = "Negate";
374 break;
375 case Operator.OnesComplement:
376 case Operator.LogicalNot:
377 method_name = "Not";
378 break;
379 case Operator.UnaryPlus:
380 method_name = "UnaryPlus";
381 break;
382 default:
383 throw new InternalErrorException ("Unknown unary operator " + Oper.ToString ());
386 Arguments args = new Arguments (2);
387 args.Add (new Argument (Expr.CreateExpressionTree (ec)));
388 if (user_op != null)
389 args.Add (new Argument (user_op.CreateExpressionTree (ec)));
390 return CreateExpressionFactoryCall (ec, method_name, args);
393 static void CreatePredefinedOperatorsTable ()
395 predefined_operators = new Type [(int) Operator.TOP] [];
398 // 7.6.1 Unary plus operator
400 predefined_operators [(int) Operator.UnaryPlus] = new Type [] {
401 TypeManager.int32_type, TypeManager.uint32_type,
402 TypeManager.int64_type, TypeManager.uint64_type,
403 TypeManager.float_type, TypeManager.double_type,
404 TypeManager.decimal_type
408 // 7.6.2 Unary minus operator
410 predefined_operators [(int) Operator.UnaryNegation] = new Type [] {
411 TypeManager.int32_type,
412 TypeManager.int64_type,
413 TypeManager.float_type, TypeManager.double_type,
414 TypeManager.decimal_type
418 // 7.6.3 Logical negation operator
420 predefined_operators [(int) Operator.LogicalNot] = new Type [] {
421 TypeManager.bool_type
425 // 7.6.4 Bitwise complement operator
427 predefined_operators [(int) Operator.OnesComplement] = new Type [] {
428 TypeManager.int32_type, TypeManager.uint32_type,
429 TypeManager.int64_type, TypeManager.uint64_type
434 // Unary numeric promotions
436 static Expression DoNumericPromotion (Operator op, Expression expr)
438 Type expr_type = expr.Type;
439 if ((op == Operator.UnaryPlus || op == Operator.UnaryNegation || op == Operator.OnesComplement) &&
440 expr_type == TypeManager.byte_type || expr_type == TypeManager.sbyte_type ||
441 expr_type == TypeManager.short_type || expr_type == TypeManager.ushort_type ||
442 expr_type == TypeManager.char_type)
443 return Convert.ImplicitNumericConversion (expr, TypeManager.int32_type);
445 if (op == Operator.UnaryNegation && expr_type == TypeManager.uint32_type)
446 return Convert.ImplicitNumericConversion (expr, TypeManager.int64_type);
448 return expr;
451 public override Expression DoResolve (ResolveContext ec)
453 if (Oper == Operator.AddressOf) {
454 return ResolveAddressOf (ec);
457 Expr = Expr.Resolve (ec);
458 if (Expr == null)
459 return null;
461 if (TypeManager.IsDynamicType (Expr.Type)) {
462 Arguments args = new Arguments (1);
463 args.Add (new Argument (Expr));
464 return new DynamicUnaryConversion (GetOperatorExpressionTypeName (), args, loc).Resolve (ec);
467 if (TypeManager.IsNullableType (Expr.Type))
468 return new Nullable.LiftedUnaryOperator (Oper, Expr).Resolve (ec);
471 // Attempt to use a constant folding operation.
473 Constant cexpr = Expr as Constant;
474 if (cexpr != null) {
475 cexpr = TryReduceConstant (ec, cexpr);
476 if (cexpr != null)
477 return cexpr;
480 Expression expr = ResolveOperator (ec, Expr);
481 if (expr == null)
482 Error_OperatorCannotBeApplied (ec, loc, OperName (Oper), Expr.Type);
485 // Reduce unary operator on predefined types
487 if (expr == this && Oper == Operator.UnaryPlus)
488 return Expr;
490 return expr;
493 public override Expression DoResolveLValue (ResolveContext ec, Expression right)
495 return null;
498 public override void Emit (EmitContext ec)
500 EmitOperator (ec, type);
503 protected void EmitOperator (EmitContext ec, Type type)
505 ILGenerator ig = ec.ig;
507 switch (Oper) {
508 case Operator.UnaryPlus:
509 Expr.Emit (ec);
510 break;
512 case Operator.UnaryNegation:
513 if (ec.HasSet (EmitContext.Options.CheckedScope) && !IsFloat (type)) {
514 ig.Emit (OpCodes.Ldc_I4_0);
515 if (type == TypeManager.int64_type)
516 ig.Emit (OpCodes.Conv_U8);
517 Expr.Emit (ec);
518 ig.Emit (OpCodes.Sub_Ovf);
519 } else {
520 Expr.Emit (ec);
521 ig.Emit (OpCodes.Neg);
524 break;
526 case Operator.LogicalNot:
527 Expr.Emit (ec);
528 ig.Emit (OpCodes.Ldc_I4_0);
529 ig.Emit (OpCodes.Ceq);
530 break;
532 case Operator.OnesComplement:
533 Expr.Emit (ec);
534 ig.Emit (OpCodes.Not);
535 break;
537 case Operator.AddressOf:
538 ((IMemoryLocation)Expr).AddressOf (ec, AddressOp.LoadStore);
539 break;
541 default:
542 throw new Exception ("This should not happen: Operator = "
543 + Oper.ToString ());
547 // Same trick as in Binary expression
549 if (enum_conversion != null)
550 enum_conversion.Emit (ec);
553 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
555 if (Oper == Operator.LogicalNot)
556 Expr.EmitBranchable (ec, target, !on_true);
557 else
558 base.EmitBranchable (ec, target, on_true);
561 public override void EmitSideEffect (EmitContext ec)
563 Expr.EmitSideEffect (ec);
566 public static void Error_OperatorCannotBeApplied (ResolveContext ec, Location loc, string oper, Type t)
568 ec.Report.Error (23, loc, "The `{0}' operator cannot be applied to operand of type `{1}'",
569 oper, TypeManager.CSharpName (t));
573 // Converts operator to System.Linq.Expressions.ExpressionType enum name
575 string GetOperatorExpressionTypeName ()
577 switch (Oper) {
578 case Operator.OnesComplement:
579 return "OnesComplement";
580 case Operator.LogicalNot:
581 return "Not";
582 case Operator.UnaryNegation:
583 return "Negate";
584 case Operator.UnaryPlus:
585 return "UnaryPlus";
586 default:
587 throw new NotImplementedException ("Unknown express type operator " + Oper.ToString ());
591 static bool IsFloat (Type t)
593 return t == TypeManager.float_type || t == TypeManager.double_type;
597 // Returns a stringified representation of the Operator
599 public static string OperName (Operator oper)
601 switch (oper) {
602 case Operator.UnaryPlus:
603 return "+";
604 case Operator.UnaryNegation:
605 return "-";
606 case Operator.LogicalNot:
607 return "!";
608 case Operator.OnesComplement:
609 return "~";
610 case Operator.AddressOf:
611 return "&";
614 throw new NotImplementedException (oper.ToString ());
617 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
619 type = storey.MutateType (type);
620 Expr.MutateHoistedGenericType (storey);
623 Expression ResolveAddressOf (ResolveContext ec)
625 if (!ec.IsUnsafe)
626 UnsafeError (ec, loc);
628 Expr = Expr.DoResolveLValue (ec, EmptyExpression.UnaryAddress);
629 if (Expr == null || Expr.eclass != ExprClass.Variable) {
630 ec.Report.Error (211, loc, "Cannot take the address of the given expression");
631 return null;
634 if (!TypeManager.VerifyUnManaged (Expr.Type, loc)) {
635 return null;
638 IVariableReference vr = Expr as IVariableReference;
639 bool is_fixed;
640 if (vr != null) {
641 VariableInfo vi = vr.VariableInfo;
642 if (vi != null) {
643 if (vi.LocalInfo != null)
644 vi.LocalInfo.Used = true;
647 // A variable is considered definitely assigned if you take its address.
649 vi.SetAssigned (ec);
652 is_fixed = vr.IsFixed;
653 vr.SetHasAddressTaken ();
655 if (vr.IsHoisted) {
656 AnonymousMethodExpression.Error_AddressOfCapturedVar (ec, vr, loc);
658 } else {
659 IFixedExpression fe = Expr as IFixedExpression;
660 is_fixed = fe != null && fe.IsFixed;
663 if (!is_fixed && !ec.HasSet (ResolveContext.Options.FixedInitializerScope)) {
664 ec.Report.Error (212, loc, "You can only take the address of unfixed expression inside of a fixed statement initializer");
667 type = TypeManager.GetPointerType (Expr.Type);
668 eclass = ExprClass.Value;
669 return this;
672 Expression ResolvePrimitivePredefinedType (Expression expr)
674 expr = DoNumericPromotion (Oper, expr);
675 Type expr_type = expr.Type;
676 Type[] predefined = predefined_operators [(int) Oper];
677 foreach (Type t in predefined) {
678 if (t == expr_type)
679 return expr;
681 return null;
685 // Perform user-operator overload resolution
687 protected virtual Expression ResolveUserOperator (ResolveContext ec, Expression expr)
689 CSharp.Operator.OpType op_type;
690 switch (Oper) {
691 case Operator.LogicalNot:
692 op_type = CSharp.Operator.OpType.LogicalNot; break;
693 case Operator.OnesComplement:
694 op_type = CSharp.Operator.OpType.OnesComplement; break;
695 case Operator.UnaryNegation:
696 op_type = CSharp.Operator.OpType.UnaryNegation; break;
697 case Operator.UnaryPlus:
698 op_type = CSharp.Operator.OpType.UnaryPlus; break;
699 default:
700 throw new InternalErrorException (Oper.ToString ());
703 string op_name = CSharp.Operator.GetMetadataName (op_type);
704 MethodGroupExpr user_op = MemberLookup (ec.Compiler, ec.CurrentType, expr.Type, op_name, MemberTypes.Method, AllBindingFlags, expr.Location) as MethodGroupExpr;
705 if (user_op == null)
706 return null;
708 Arguments args = new Arguments (1);
709 args.Add (new Argument (expr));
710 user_op = user_op.OverloadResolve (ec, ref args, false, expr.Location);
712 if (user_op == null)
713 return null;
715 Expr = args [0].Expr;
716 return new UserOperatorCall (user_op, args, CreateExpressionTree, expr.Location);
720 // Unary user type overload resolution
722 Expression ResolveUserType (ResolveContext ec, Expression expr)
724 Expression best_expr = ResolveUserOperator (ec, expr);
725 if (best_expr != null)
726 return best_expr;
728 Type[] predefined = predefined_operators [(int) Oper];
729 foreach (Type t in predefined) {
730 Expression oper_expr = Convert.UserDefinedConversion (ec, expr, t, expr.Location, false, false);
731 if (oper_expr == null)
732 continue;
735 // decimal type is predefined but has user-operators
737 if (oper_expr.Type == TypeManager.decimal_type)
738 oper_expr = ResolveUserType (ec, oper_expr);
739 else
740 oper_expr = ResolvePrimitivePredefinedType (oper_expr);
742 if (oper_expr == null)
743 continue;
745 if (best_expr == null) {
746 best_expr = oper_expr;
747 continue;
750 int result = MethodGroupExpr.BetterTypeConversion (ec, best_expr.Type, t);
751 if (result == 0) {
752 ec.Report.Error (35, loc, "Operator `{0}' is ambiguous on an operand of type `{1}'",
753 OperName (Oper), TypeManager.CSharpName (expr.Type));
754 break;
757 if (result == 2)
758 best_expr = oper_expr;
761 if (best_expr == null)
762 return null;
765 // HACK: Decimal user-operator is included in standard operators
767 if (best_expr.Type == TypeManager.decimal_type)
768 return best_expr;
770 Expr = best_expr;
771 type = best_expr.Type;
772 return this;
775 protected override void CloneTo (CloneContext clonectx, Expression t)
777 Unary target = (Unary) t;
779 target.Expr = Expr.Clone (clonectx);
784 // Unary operators are turned into Indirection expressions
785 // after semantic analysis (this is so we can take the address
786 // of an indirection).
788 public class Indirection : Expression, IMemoryLocation, IAssignMethod, IFixedExpression {
789 Expression expr;
790 LocalTemporary temporary;
791 bool prepared;
793 public Indirection (Expression expr, Location l)
795 this.expr = expr;
796 loc = l;
799 public override Expression CreateExpressionTree (ResolveContext ec)
801 Error_PointerInsideExpressionTree (ec);
802 return null;
805 protected override void CloneTo (CloneContext clonectx, Expression t)
807 Indirection target = (Indirection) t;
808 target.expr = expr.Clone (clonectx);
811 public override void Emit (EmitContext ec)
813 if (!prepared)
814 expr.Emit (ec);
816 LoadFromPtr (ec.ig, Type);
819 public void Emit (EmitContext ec, bool leave_copy)
821 Emit (ec);
822 if (leave_copy) {
823 ec.ig.Emit (OpCodes.Dup);
824 temporary = new LocalTemporary (expr.Type);
825 temporary.Store (ec);
829 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
831 prepared = prepare_for_load;
833 expr.Emit (ec);
835 if (prepare_for_load)
836 ec.ig.Emit (OpCodes.Dup);
838 source.Emit (ec);
839 if (leave_copy) {
840 ec.ig.Emit (OpCodes.Dup);
841 temporary = new LocalTemporary (expr.Type);
842 temporary.Store (ec);
845 StoreFromPtr (ec.ig, type);
847 if (temporary != null) {
848 temporary.Emit (ec);
849 temporary.Release (ec);
853 public void AddressOf (EmitContext ec, AddressOp Mode)
855 expr.Emit (ec);
858 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
860 return DoResolve (ec);
863 public override Expression DoResolve (ResolveContext ec)
865 expr = expr.Resolve (ec);
866 if (expr == null)
867 return null;
869 if (!ec.IsUnsafe)
870 UnsafeError (ec, loc);
872 if (!expr.Type.IsPointer) {
873 ec.Report.Error (193, loc, "The * or -> operator must be applied to a pointer");
874 return null;
877 if (expr.Type == TypeManager.void_ptr_type) {
878 ec.Report.Error (242, loc, "The operation in question is undefined on void pointers");
879 return null;
882 type = TypeManager.GetElementType (expr.Type);
883 eclass = ExprClass.Variable;
884 return this;
887 public bool IsFixed {
888 get { return true; }
891 public override string ToString ()
893 return "*(" + expr + ")";
897 /// <summary>
898 /// Unary Mutator expressions (pre and post ++ and --)
899 /// </summary>
901 /// <remarks>
902 /// UnaryMutator implements ++ and -- expressions. It derives from
903 /// ExpressionStatement becuase the pre/post increment/decrement
904 /// operators can be used in a statement context.
906 /// FIXME: Idea, we could split this up in two classes, one simpler
907 /// for the common case, and one with the extra fields for more complex
908 /// classes (indexers require temporary access; overloaded require method)
910 /// </remarks>
911 public class UnaryMutator : ExpressionStatement {
912 [Flags]
913 public enum Mode : byte {
914 IsIncrement = 0,
915 IsDecrement = 1,
916 IsPre = 0,
917 IsPost = 2,
919 PreIncrement = 0,
920 PreDecrement = IsDecrement,
921 PostIncrement = IsPost,
922 PostDecrement = IsPost | IsDecrement
925 Mode mode;
926 bool is_expr = false;
927 bool recurse = false;
929 Expression expr;
932 // This is expensive for the simplest case.
934 UserOperatorCall method;
936 public UnaryMutator (Mode m, Expression e)
938 mode = m;
939 loc = e.Location;
940 expr = e;
943 static string OperName (Mode mode)
945 return (mode == Mode.PreIncrement || mode == Mode.PostIncrement) ?
946 "++" : "--";
949 /// <summary>
950 /// Returns whether an object of type `t' can be incremented
951 /// or decremented with add/sub (ie, basically whether we can
952 /// use pre-post incr-decr operations on it, but it is not a
953 /// System.Decimal, which we require operator overloading to catch)
954 /// </summary>
955 static bool IsIncrementableNumber (Type t)
957 return (t == TypeManager.sbyte_type) ||
958 (t == TypeManager.byte_type) ||
959 (t == TypeManager.short_type) ||
960 (t == TypeManager.ushort_type) ||
961 (t == TypeManager.int32_type) ||
962 (t == TypeManager.uint32_type) ||
963 (t == TypeManager.int64_type) ||
964 (t == TypeManager.uint64_type) ||
965 (t == TypeManager.char_type) ||
966 (TypeManager.IsSubclassOf (t, TypeManager.enum_type)) ||
967 (t == TypeManager.float_type) ||
968 (t == TypeManager.double_type) ||
969 (t.IsPointer && t != TypeManager.void_ptr_type);
972 Expression ResolveOperator (ResolveContext ec)
974 type = expr.Type;
977 // The operand of the prefix/postfix increment decrement operators
978 // should be an expression that is classified as a variable,
979 // a property access or an indexer access
981 if (expr.eclass == ExprClass.Variable || expr.eclass == ExprClass.IndexerAccess || expr.eclass == ExprClass.PropertyAccess) {
982 expr = expr.ResolveLValue (ec, expr);
983 } else {
984 ec.Report.Error (1059, loc, "The operand of an increment or decrement operator must be a variable, property or indexer");
988 // Step 1: Perform Operator Overload location
990 MethodGroupExpr mg;
991 string op_name;
993 if (mode == Mode.PreIncrement || mode == Mode.PostIncrement)
994 op_name = Operator.GetMetadataName (Operator.OpType.Increment);
995 else
996 op_name = Operator.GetMetadataName (Operator.OpType.Decrement);
998 mg = MemberLookup (ec.Compiler, ec.CurrentType, type, op_name, MemberTypes.Method, AllBindingFlags, loc) as MethodGroupExpr;
1000 if (mg != null) {
1001 Arguments args = new Arguments (1);
1002 args.Add (new Argument (expr));
1003 mg = mg.OverloadResolve (ec, ref args, false, loc);
1004 if (mg == null)
1005 return null;
1007 method = new UserOperatorCall (mg, args, null, loc);
1008 Convert.ImplicitConversionRequired (ec, method, type, loc);
1009 return this;
1012 if (!IsIncrementableNumber (type)) {
1013 ec.Report.Error (187, loc, "No such operator '" + OperName (mode) + "' defined for type '" +
1014 TypeManager.CSharpName (type) + "'");
1015 return null;
1018 return this;
1021 public override Expression CreateExpressionTree (ResolveContext ec)
1023 return new SimpleAssign (this, this).CreateExpressionTree (ec);
1026 public override Expression DoResolve (ResolveContext ec)
1028 expr = expr.Resolve (ec);
1030 if (expr == null)
1031 return null;
1033 if (TypeManager.IsDynamicType (expr.Type)) {
1034 Arguments args = new Arguments (1);
1035 args.Add (new Argument (expr));
1036 return new DynamicUnaryConversion (GetOperatorExpressionTypeName (), args, loc).Resolve (ec);
1039 eclass = ExprClass.Value;
1041 if (TypeManager.IsNullableType (expr.Type))
1042 return new Nullable.LiftedUnaryMutator (mode, expr, loc).Resolve (ec);
1044 return ResolveOperator (ec);
1048 // Loads the proper "1" into the stack based on the type, then it emits the
1049 // opcode for the operation requested
1051 void LoadOneAndEmitOp (EmitContext ec, Type t)
1054 // Measure if getting the typecode and using that is more/less efficient
1055 // that comparing types. t.GetTypeCode() is an internal call.
1057 ILGenerator ig = ec.ig;
1059 if (t == TypeManager.uint64_type || t == TypeManager.int64_type)
1060 LongConstant.EmitLong (ig, 1);
1061 else if (t == TypeManager.double_type)
1062 ig.Emit (OpCodes.Ldc_R8, 1.0);
1063 else if (t == TypeManager.float_type)
1064 ig.Emit (OpCodes.Ldc_R4, 1.0F);
1065 else if (t.IsPointer){
1066 Type et = TypeManager.GetElementType (t);
1067 int n = GetTypeSize (et);
1069 if (n == 0)
1070 ig.Emit (OpCodes.Sizeof, et);
1071 else {
1072 IntConstant.EmitInt (ig, n);
1073 ig.Emit (OpCodes.Conv_I);
1075 } else
1076 ig.Emit (OpCodes.Ldc_I4_1);
1079 // Now emit the operation
1082 Binary.Operator op = (mode & Mode.IsDecrement) != 0 ? Binary.Operator.Subtraction : Binary.Operator.Addition;
1083 Binary.EmitOperatorOpcode (ec, op, t);
1085 if (t == TypeManager.sbyte_type){
1086 if (ec.HasSet (EmitContext.Options.CheckedScope))
1087 ig.Emit (OpCodes.Conv_Ovf_I1);
1088 else
1089 ig.Emit (OpCodes.Conv_I1);
1090 } else if (t == TypeManager.byte_type){
1091 if (ec.HasSet (EmitContext.Options.CheckedScope))
1092 ig.Emit (OpCodes.Conv_Ovf_U1);
1093 else
1094 ig.Emit (OpCodes.Conv_U1);
1095 } else if (t == TypeManager.short_type){
1096 if (ec.HasSet (EmitContext.Options.CheckedScope))
1097 ig.Emit (OpCodes.Conv_Ovf_I2);
1098 else
1099 ig.Emit (OpCodes.Conv_I2);
1100 } else if (t == TypeManager.ushort_type || t == TypeManager.char_type){
1101 if (ec.HasSet (EmitContext.Options.CheckedScope))
1102 ig.Emit (OpCodes.Conv_Ovf_U2);
1103 else
1104 ig.Emit (OpCodes.Conv_U2);
1109 void EmitCode (EmitContext ec, bool is_expr)
1111 recurse = true;
1112 this.is_expr = is_expr;
1113 ((IAssignMethod) expr).EmitAssign (ec, this, is_expr && (mode == Mode.PreIncrement || mode == Mode.PreDecrement), true);
1116 public override void Emit (EmitContext ec)
1119 // We use recurse to allow ourselfs to be the source
1120 // of an assignment. This little hack prevents us from
1121 // having to allocate another expression
1123 if (recurse) {
1124 ((IAssignMethod) expr).Emit (ec, is_expr && (mode == Mode.PostIncrement || mode == Mode.PostDecrement));
1125 if (method == null)
1126 LoadOneAndEmitOp (ec, expr.Type);
1127 else
1128 ec.ig.Emit (OpCodes.Call, (MethodInfo)method.Method);
1129 recurse = false;
1130 return;
1133 EmitCode (ec, true);
1136 public override void EmitStatement (EmitContext ec)
1138 EmitCode (ec, false);
1142 // Converts operator to System.Linq.Expressions.ExpressionType enum name
1144 string GetOperatorExpressionTypeName ()
1146 if ((mode & Mode.IsDecrement) != 0)
1147 return "Decrement";
1149 return "Increment";
1152 protected override void CloneTo (CloneContext clonectx, Expression t)
1154 UnaryMutator target = (UnaryMutator) t;
1156 target.expr = expr.Clone (clonectx);
1160 /// <summary>
1161 /// Base class for the `Is' and `As' classes.
1162 /// </summary>
1164 /// <remarks>
1165 /// FIXME: Split this in two, and we get to save the `Operator' Oper
1166 /// size.
1167 /// </remarks>
1168 public abstract class Probe : Expression {
1169 public Expression ProbeType;
1170 protected Expression expr;
1171 protected TypeExpr probe_type_expr;
1173 public Probe (Expression expr, Expression probe_type, Location l)
1175 ProbeType = probe_type;
1176 loc = l;
1177 this.expr = expr;
1180 public Expression Expr {
1181 get {
1182 return expr;
1186 public override Expression DoResolve (ResolveContext ec)
1188 probe_type_expr = ProbeType.ResolveAsTypeTerminal (ec, false);
1189 if (probe_type_expr == null)
1190 return null;
1192 expr = expr.Resolve (ec);
1193 if (expr == null)
1194 return null;
1196 if ((probe_type_expr.Type.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute) {
1197 ec.Report.Error (-244, loc, "The `{0}' operator cannot be applied to an operand of a static type",
1198 OperatorName);
1201 if (expr.Type.IsPointer || probe_type_expr.Type.IsPointer) {
1202 ec.Report.Error (244, loc, "The `{0}' operator cannot be applied to an operand of pointer type",
1203 OperatorName);
1204 return null;
1207 if (expr.Type == InternalType.AnonymousMethod) {
1208 ec.Report.Error (837, loc, "The `{0}' operator cannot be applied to a lambda expression or anonymous method",
1209 OperatorName);
1210 return null;
1213 return this;
1216 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
1218 expr.MutateHoistedGenericType (storey);
1219 probe_type_expr.MutateHoistedGenericType (storey);
1222 protected abstract string OperatorName { get; }
1224 protected override void CloneTo (CloneContext clonectx, Expression t)
1226 Probe target = (Probe) t;
1228 target.expr = expr.Clone (clonectx);
1229 target.ProbeType = ProbeType.Clone (clonectx);
1234 /// <summary>
1235 /// Implementation of the `is' operator.
1236 /// </summary>
1237 public class Is : Probe {
1238 Nullable.Unwrap expr_unwrap;
1240 public Is (Expression expr, Expression probe_type, Location l)
1241 : base (expr, probe_type, l)
1245 public override Expression CreateExpressionTree (ResolveContext ec)
1247 Arguments args = Arguments.CreateForExpressionTree (ec, null,
1248 expr.CreateExpressionTree (ec),
1249 new TypeOf (probe_type_expr, loc));
1251 return CreateExpressionFactoryCall (ec, "TypeIs", args);
1254 public override void Emit (EmitContext ec)
1256 ILGenerator ig = ec.ig;
1257 if (expr_unwrap != null) {
1258 expr_unwrap.EmitCheck (ec);
1259 return;
1262 expr.Emit (ec);
1263 ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
1264 ig.Emit (OpCodes.Ldnull);
1265 ig.Emit (OpCodes.Cgt_Un);
1268 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
1270 ILGenerator ig = ec.ig;
1271 if (expr_unwrap != null) {
1272 expr_unwrap.EmitCheck (ec);
1273 } else {
1274 expr.Emit (ec);
1275 ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
1277 ig.Emit (on_true ? OpCodes.Brtrue : OpCodes.Brfalse, target);
1280 Expression CreateConstantResult (ResolveContext ec, bool result)
1282 if (result)
1283 ec.Report.Warning (183, 1, loc, "The given expression is always of the provided (`{0}') type",
1284 TypeManager.CSharpName (probe_type_expr.Type));
1285 else
1286 ec.Report.Warning (184, 1, loc, "The given expression is never of the provided (`{0}') type",
1287 TypeManager.CSharpName (probe_type_expr.Type));
1289 return ReducedExpression.Create (new BoolConstant (result, loc), this);
1292 public override Expression DoResolve (ResolveContext ec)
1294 if (base.DoResolve (ec) == null)
1295 return null;
1297 Type d = expr.Type;
1298 bool d_is_nullable = false;
1301 // If E is a method group or the null literal, or if the type of E is a reference
1302 // type or a nullable type and the value of E is null, the result is false
1304 if (expr.IsNull || expr.eclass == ExprClass.MethodGroup)
1305 return CreateConstantResult (ec, false);
1307 if (TypeManager.IsNullableType (d) && !TypeManager.ContainsGenericParameters (d)) {
1308 d = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (d) [0]);
1309 d_is_nullable = true;
1312 type = TypeManager.bool_type;
1313 eclass = ExprClass.Value;
1314 Type t = probe_type_expr.Type;
1315 bool t_is_nullable = false;
1316 if (TypeManager.IsNullableType (t) && !TypeManager.ContainsGenericParameters (t)) {
1317 t = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (t) [0]);
1318 t_is_nullable = true;
1321 if (TypeManager.IsStruct (t)) {
1322 if (d == t) {
1324 // D and T are the same value types but D can be null
1326 if (d_is_nullable && !t_is_nullable) {
1327 expr_unwrap = Nullable.Unwrap.Create (expr, false);
1328 return this;
1332 // The result is true if D and T are the same value types
1334 return CreateConstantResult (ec, true);
1337 if (TypeManager.IsGenericParameter (d))
1338 return ResolveGenericParameter (ec, t, d);
1341 // An unboxing conversion exists
1343 if (Convert.ExplicitReferenceConversionExists (d, t))
1344 return this;
1345 } else {
1346 if (TypeManager.IsGenericParameter (t))
1347 return ResolveGenericParameter (ec, d, t);
1349 if (TypeManager.IsStruct (d)) {
1350 bool temp;
1351 if (Convert.ImplicitBoxingConversionExists (expr, t, out temp))
1352 return CreateConstantResult (ec, true);
1353 } else {
1354 if (TypeManager.IsGenericParameter (d))
1355 return ResolveGenericParameter (ec, t, d);
1357 if (TypeManager.ContainsGenericParameters (d))
1358 return this;
1360 if (Convert.ImplicitReferenceConversionExists (expr, t) ||
1361 Convert.ExplicitReferenceConversionExists (d, t)) {
1362 return this;
1367 return CreateConstantResult (ec, false);
1370 Expression ResolveGenericParameter (ResolveContext ec, Type d, Type t)
1372 GenericConstraints constraints = TypeManager.GetTypeParameterConstraints (t);
1373 if (constraints != null) {
1374 if (constraints.IsReferenceType && TypeManager.IsStruct (d))
1375 return CreateConstantResult (ec, false);
1378 if (TypeManager.IsGenericParameter (expr.Type)) {
1379 if (constraints != null && constraints.IsValueType && expr.Type == t)
1380 return CreateConstantResult (ec, true);
1382 expr = new BoxedCast (expr, d);
1385 return this;
1388 protected override string OperatorName {
1389 get { return "is"; }
1393 /// <summary>
1394 /// Implementation of the `as' operator.
1395 /// </summary>
1396 public class As : Probe {
1397 bool do_isinst;
1398 Expression resolved_type;
1400 public As (Expression expr, Expression probe_type, Location l)
1401 : base (expr, probe_type, l)
1405 public override Expression CreateExpressionTree (ResolveContext ec)
1407 Arguments args = Arguments.CreateForExpressionTree (ec, null,
1408 expr.CreateExpressionTree (ec),
1409 new TypeOf (probe_type_expr, loc));
1411 return CreateExpressionFactoryCall (ec, "TypeAs", args);
1414 public override void Emit (EmitContext ec)
1416 ILGenerator ig = ec.ig;
1418 expr.Emit (ec);
1420 if (do_isinst)
1421 ig.Emit (OpCodes.Isinst, type);
1423 #if GMCS_SOURCE
1424 if (TypeManager.IsGenericParameter (type) || TypeManager.IsNullableType (type))
1425 ig.Emit (OpCodes.Unbox_Any, type);
1426 #endif
1429 public override Expression DoResolve (ResolveContext ec)
1431 // Because expr is modified
1432 if (eclass != ExprClass.Invalid)
1433 return this;
1435 if (resolved_type == null) {
1436 resolved_type = base.DoResolve (ec);
1438 if (resolved_type == null)
1439 return null;
1442 type = probe_type_expr.Type;
1443 eclass = ExprClass.Value;
1444 Type etype = expr.Type;
1446 if (!TypeManager.IsReferenceType (type) && !TypeManager.IsNullableType (type)) {
1447 if (TypeManager.IsGenericParameter (type)) {
1448 ec.Report.Error (413, loc,
1449 "The `as' operator cannot be used with a non-reference type parameter `{0}'. Consider adding `class' or a reference type constraint",
1450 probe_type_expr.GetSignatureForError ());
1451 } else {
1452 ec.Report.Error (77, loc,
1453 "The `as' operator cannot be used with a non-nullable value type `{0}'",
1454 TypeManager.CSharpName (type));
1456 return null;
1459 if (expr.IsNull && TypeManager.IsNullableType (type)) {
1460 return Nullable.LiftedNull.CreateFromExpression (ec, this);
1463 Expression e = Convert.ImplicitConversion (ec, expr, type, loc);
1464 if (e != null){
1465 expr = e;
1466 do_isinst = false;
1467 return this;
1470 if (Convert.ExplicitReferenceConversionExists (etype, type)){
1471 if (TypeManager.IsGenericParameter (etype))
1472 expr = new BoxedCast (expr, etype);
1474 do_isinst = true;
1475 return this;
1478 if (TypeManager.ContainsGenericParameters (etype) ||
1479 TypeManager.ContainsGenericParameters (type)) {
1480 expr = new BoxedCast (expr, etype);
1481 do_isinst = true;
1482 return this;
1485 ec.Report.Error (39, loc, "Cannot convert type `{0}' to `{1}' via a built-in conversion",
1486 TypeManager.CSharpName (etype), TypeManager.CSharpName (type));
1488 return null;
1491 protected override string OperatorName {
1492 get { return "as"; }
1495 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
1497 type = storey.MutateType (type);
1498 base.MutateHoistedGenericType (storey);
1501 public override bool GetAttributableValue (ResolveContext ec, Type value_type, out object value)
1503 return expr.GetAttributableValue (ec, value_type, out value);
1507 /// <summary>
1508 /// This represents a typecast in the source language.
1510 /// FIXME: Cast expressions have an unusual set of parsing
1511 /// rules, we need to figure those out.
1512 /// </summary>
1513 public class Cast : Expression {
1514 Expression target_type;
1515 Expression expr;
1517 public Cast (Expression cast_type, Expression expr)
1518 : this (cast_type, expr, cast_type.Location)
1522 public Cast (Expression cast_type, Expression expr, Location loc)
1524 this.target_type = cast_type;
1525 this.expr = expr;
1526 this.loc = loc;
1529 public Expression TargetType {
1530 get { return target_type; }
1533 public Expression Expr {
1534 get { return expr; }
1537 public override Expression CreateExpressionTree (ResolveContext ec)
1539 throw new NotSupportedException ("ET");
1542 public override Expression DoResolve (ResolveContext ec)
1544 expr = expr.Resolve (ec);
1545 if (expr == null)
1546 return null;
1548 TypeExpr target = target_type.ResolveAsTypeTerminal (ec, false);
1549 if (target == null)
1550 return null;
1552 type = target.Type;
1554 if (type.IsAbstract && type.IsSealed) {
1555 ec.Report.Error (716, loc, "Cannot convert to static type `{0}'", TypeManager.CSharpName (type));
1556 return null;
1559 eclass = ExprClass.Value;
1561 Constant c = expr as Constant;
1562 if (c != null) {
1563 c = c.TryReduce (ec, type, loc);
1564 if (c != null)
1565 return c;
1568 if (type.IsPointer && !ec.IsUnsafe) {
1569 UnsafeError (ec, loc);
1570 } else if (TypeManager.IsDynamicType (expr.Type)) {
1571 Arguments arg = new Arguments (1);
1572 arg.Add (new Argument (expr));
1573 return new DynamicConversion (type, true, arg, loc).Resolve (ec);
1576 expr = Convert.ExplicitConversion (ec, expr, type, loc);
1577 return expr;
1580 public override void Emit (EmitContext ec)
1582 throw new Exception ("Should not happen");
1585 protected override void CloneTo (CloneContext clonectx, Expression t)
1587 Cast target = (Cast) t;
1589 target.target_type = target_type.Clone (clonectx);
1590 target.expr = expr.Clone (clonectx);
1595 // C# 2.0 Default value expression
1597 public class DefaultValueExpression : Expression
1599 sealed class DefaultValueNullLiteral : NullLiteral
1601 public DefaultValueNullLiteral (DefaultValueExpression expr)
1602 : base (expr.type, expr.loc)
1606 public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, Type t, bool expl)
1608 Error_ValueCannotBeConvertedCore (ec, loc, t, expl);
1613 Expression expr;
1615 public DefaultValueExpression (Expression expr, Location loc)
1617 this.expr = expr;
1618 this.loc = loc;
1621 public override Expression CreateExpressionTree (ResolveContext ec)
1623 Arguments args = new Arguments (2);
1624 args.Add (new Argument (this));
1625 args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
1626 return CreateExpressionFactoryCall (ec, "Constant", args);
1629 public override Expression DoResolve (ResolveContext ec)
1631 TypeExpr texpr = expr.ResolveAsTypeTerminal (ec, false);
1632 if (texpr == null)
1633 return null;
1635 type = texpr.Type;
1637 if ((type.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute) {
1638 ec.Report.Error (-244, loc, "The `default value' operator cannot be applied to an operand of a static type");
1641 if (type.IsPointer)
1642 return new NullLiteral (Location).ConvertImplicitly (type);
1644 if (TypeManager.IsReferenceType (type))
1645 return new DefaultValueNullLiteral (this);
1647 Constant c = New.Constantify (type);
1648 if (c != null)
1649 return c;
1651 eclass = ExprClass.Variable;
1652 return this;
1655 public override void Emit (EmitContext ec)
1657 LocalTemporary temp_storage = new LocalTemporary(type);
1659 temp_storage.AddressOf(ec, AddressOp.LoadStore);
1660 ec.ig.Emit(OpCodes.Initobj, type);
1661 temp_storage.Emit(ec);
1664 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
1666 type = storey.MutateType (type);
1669 protected override void CloneTo (CloneContext clonectx, Expression t)
1671 DefaultValueExpression target = (DefaultValueExpression) t;
1673 target.expr = expr.Clone (clonectx);
1677 /// <summary>
1678 /// Binary operators
1679 /// </summary>
1680 public class Binary : Expression, IDynamicBinder
1683 protected class PredefinedOperator {
1684 protected readonly Type left;
1685 protected readonly Type right;
1686 public readonly Operator OperatorsMask;
1687 public Type ReturnType;
1689 public PredefinedOperator (Type ltype, Type rtype, Operator op_mask)
1690 : this (ltype, rtype, op_mask, ltype)
1694 public PredefinedOperator (Type type, Operator op_mask, Type return_type)
1695 : this (type, type, op_mask, return_type)
1699 public PredefinedOperator (Type type, Operator op_mask)
1700 : this (type, type, op_mask, type)
1704 public PredefinedOperator (Type ltype, Type rtype, Operator op_mask, Type return_type)
1706 if ((op_mask & Operator.ValuesOnlyMask) != 0)
1707 throw new InternalErrorException ("Only masked values can be used");
1709 this.left = ltype;
1710 this.right = rtype;
1711 this.OperatorsMask = op_mask;
1712 this.ReturnType = return_type;
1715 public virtual Expression ConvertResult (ResolveContext ec, Binary b)
1717 b.type = ReturnType;
1719 b.left = Convert.ImplicitConversion (ec, b.left, left, b.left.Location);
1720 b.right = Convert.ImplicitConversion (ec, b.right, right, b.right.Location);
1723 // A user operators does not support multiple user conversions, but decimal type
1724 // is considered to be predefined type therefore we apply predefined operators rules
1725 // and then look for decimal user-operator implementation
1727 if (left == TypeManager.decimal_type)
1728 return b.ResolveUserOperator (ec, b.left.Type, b.right.Type);
1730 return b;
1733 public bool IsPrimitiveApplicable (Type ltype, Type rtype)
1736 // We are dealing with primitive types only
1738 return left == ltype && ltype == rtype;
1741 public virtual bool IsApplicable (ResolveContext ec, Expression lexpr, Expression rexpr)
1743 if (TypeManager.IsEqual (left, lexpr.Type) &&
1744 TypeManager.IsEqual (right, rexpr.Type))
1745 return true;
1747 return Convert.ImplicitConversionExists (ec, lexpr, left) &&
1748 Convert.ImplicitConversionExists (ec, rexpr, right);
1751 public PredefinedOperator ResolveBetterOperator (ResolveContext ec, PredefinedOperator best_operator)
1753 int result = 0;
1754 if (left != null && best_operator.left != null) {
1755 result = MethodGroupExpr.BetterTypeConversion (ec, best_operator.left, left);
1759 // When second arguments are same as the first one, the result is same
1761 if (right != null && (left != right || best_operator.left != best_operator.right)) {
1762 result |= MethodGroupExpr.BetterTypeConversion (ec, best_operator.right, right);
1765 if (result == 0 || result > 2)
1766 return null;
1768 return result == 1 ? best_operator : this;
1772 class PredefinedStringOperator : PredefinedOperator {
1773 public PredefinedStringOperator (Type type, Operator op_mask)
1774 : base (type, op_mask, type)
1776 ReturnType = TypeManager.string_type;
1779 public PredefinedStringOperator (Type ltype, Type rtype, Operator op_mask)
1780 : base (ltype, rtype, op_mask)
1782 ReturnType = TypeManager.string_type;
1785 public override Expression ConvertResult (ResolveContext ec, Binary b)
1788 // Use original expression for nullable arguments
1790 Nullable.Unwrap unwrap = b.left as Nullable.Unwrap;
1791 if (unwrap != null)
1792 b.left = unwrap.Original;
1794 unwrap = b.right as Nullable.Unwrap;
1795 if (unwrap != null)
1796 b.right = unwrap.Original;
1798 b.left = Convert.ImplicitConversion (ec, b.left, left, b.left.Location);
1799 b.right = Convert.ImplicitConversion (ec, b.right, right, b.right.Location);
1802 // Start a new concat expression using converted expression
1804 return new StringConcat (b.loc, b.left, b.right).Resolve (ec);
1808 class PredefinedShiftOperator : PredefinedOperator {
1809 public PredefinedShiftOperator (Type ltype, Operator op_mask) :
1810 base (ltype, TypeManager.int32_type, op_mask)
1814 public override Expression ConvertResult (ResolveContext ec, Binary b)
1816 b.left = Convert.ImplicitConversion (ec, b.left, left, b.left.Location);
1818 Expression expr_tree_expr = EmptyCast.Create (b.right, TypeManager.int32_type);
1820 int right_mask = left == TypeManager.int32_type || left == TypeManager.uint32_type ? 0x1f : 0x3f;
1823 // b = b.left >> b.right & (0x1f|0x3f)
1825 b.right = new Binary (Operator.BitwiseAnd,
1826 b.right, new IntConstant (right_mask, b.right.Location)).Resolve (ec);
1829 // Expression tree representation does not use & mask
1831 b.right = ReducedExpression.Create (b.right, expr_tree_expr).Resolve (ec);
1832 b.type = ReturnType;
1833 return b;
1837 class PredefinedPointerOperator : PredefinedOperator {
1838 public PredefinedPointerOperator (Type ltype, Type rtype, Operator op_mask)
1839 : base (ltype, rtype, op_mask)
1843 public PredefinedPointerOperator (Type ltype, Type rtype, Operator op_mask, Type retType)
1844 : base (ltype, rtype, op_mask, retType)
1848 public PredefinedPointerOperator (Type type, Operator op_mask, Type return_type)
1849 : base (type, op_mask, return_type)
1853 public override bool IsApplicable (ResolveContext ec, Expression lexpr, Expression rexpr)
1855 if (left == null) {
1856 if (!lexpr.Type.IsPointer)
1857 return false;
1858 } else {
1859 if (!Convert.ImplicitConversionExists (ec, lexpr, left))
1860 return false;
1863 if (right == null) {
1864 if (!rexpr.Type.IsPointer)
1865 return false;
1866 } else {
1867 if (!Convert.ImplicitConversionExists (ec, rexpr, right))
1868 return false;
1871 return true;
1874 public override Expression ConvertResult (ResolveContext ec, Binary b)
1876 if (left != null) {
1877 b.left = EmptyCast.Create (b.left, left);
1878 } else if (right != null) {
1879 b.right = EmptyCast.Create (b.right, right);
1882 Type r_type = ReturnType;
1883 Expression left_arg, right_arg;
1884 if (r_type == null) {
1885 if (left == null) {
1886 left_arg = b.left;
1887 right_arg = b.right;
1888 r_type = b.left.Type;
1889 } else {
1890 left_arg = b.right;
1891 right_arg = b.left;
1892 r_type = b.right.Type;
1894 } else {
1895 left_arg = b.left;
1896 right_arg = b.right;
1899 return new PointerArithmetic (b.oper, left_arg, right_arg, r_type, b.loc).Resolve (ec);
1903 [Flags]
1904 public enum Operator {
1905 Multiply = 0 | ArithmeticMask,
1906 Division = 1 | ArithmeticMask,
1907 Modulus = 2 | ArithmeticMask,
1908 Addition = 3 | ArithmeticMask | AdditionMask,
1909 Subtraction = 4 | ArithmeticMask | SubtractionMask,
1911 LeftShift = 5 | ShiftMask,
1912 RightShift = 6 | ShiftMask,
1914 LessThan = 7 | ComparisonMask | RelationalMask,
1915 GreaterThan = 8 | ComparisonMask | RelationalMask,
1916 LessThanOrEqual = 9 | ComparisonMask | RelationalMask,
1917 GreaterThanOrEqual = 10 | ComparisonMask | RelationalMask,
1918 Equality = 11 | ComparisonMask | EqualityMask,
1919 Inequality = 12 | ComparisonMask | EqualityMask,
1921 BitwiseAnd = 13 | BitwiseMask,
1922 ExclusiveOr = 14 | BitwiseMask,
1923 BitwiseOr = 15 | BitwiseMask,
1925 LogicalAnd = 16 | LogicalMask,
1926 LogicalOr = 17 | LogicalMask,
1929 // Operator masks
1931 ValuesOnlyMask = ArithmeticMask - 1,
1932 ArithmeticMask = 1 << 5,
1933 ShiftMask = 1 << 6,
1934 ComparisonMask = 1 << 7,
1935 EqualityMask = 1 << 8,
1936 BitwiseMask = 1 << 9,
1937 LogicalMask = 1 << 10,
1938 AdditionMask = 1 << 11,
1939 SubtractionMask = 1 << 12,
1940 RelationalMask = 1 << 13
1943 readonly Operator oper;
1944 protected Expression left, right;
1945 readonly bool is_compound;
1946 Expression enum_conversion;
1948 static PredefinedOperator [] standard_operators;
1949 static PredefinedOperator [] pointer_operators;
1951 public Binary (Operator oper, Expression left, Expression right, bool isCompound)
1952 : this (oper, left, right)
1954 this.is_compound = isCompound;
1957 public Binary (Operator oper, Expression left, Expression right)
1959 this.oper = oper;
1960 this.left = left;
1961 this.right = right;
1962 this.loc = left.Location;
1965 public Operator Oper {
1966 get {
1967 return oper;
1971 /// <summary>
1972 /// Returns a stringified representation of the Operator
1973 /// </summary>
1974 string OperName (Operator oper)
1976 string s;
1977 switch (oper){
1978 case Operator.Multiply:
1979 s = "*";
1980 break;
1981 case Operator.Division:
1982 s = "/";
1983 break;
1984 case Operator.Modulus:
1985 s = "%";
1986 break;
1987 case Operator.Addition:
1988 s = "+";
1989 break;
1990 case Operator.Subtraction:
1991 s = "-";
1992 break;
1993 case Operator.LeftShift:
1994 s = "<<";
1995 break;
1996 case Operator.RightShift:
1997 s = ">>";
1998 break;
1999 case Operator.LessThan:
2000 s = "<";
2001 break;
2002 case Operator.GreaterThan:
2003 s = ">";
2004 break;
2005 case Operator.LessThanOrEqual:
2006 s = "<=";
2007 break;
2008 case Operator.GreaterThanOrEqual:
2009 s = ">=";
2010 break;
2011 case Operator.Equality:
2012 s = "==";
2013 break;
2014 case Operator.Inequality:
2015 s = "!=";
2016 break;
2017 case Operator.BitwiseAnd:
2018 s = "&";
2019 break;
2020 case Operator.BitwiseOr:
2021 s = "|";
2022 break;
2023 case Operator.ExclusiveOr:
2024 s = "^";
2025 break;
2026 case Operator.LogicalOr:
2027 s = "||";
2028 break;
2029 case Operator.LogicalAnd:
2030 s = "&&";
2031 break;
2032 default:
2033 s = oper.ToString ();
2034 break;
2037 if (is_compound)
2038 return s + "=";
2040 return s;
2043 public static void Error_OperatorCannotBeApplied (ResolveContext ec, Expression left, Expression right, Operator oper, Location loc)
2045 new Binary (oper, left, right).Error_OperatorCannotBeApplied (ec, left, right);
2048 public static void Error_OperatorCannotBeApplied (ResolveContext ec, Expression left, Expression right, string oper, Location loc)
2050 string l, r;
2051 l = TypeManager.CSharpName (left.Type);
2052 r = TypeManager.CSharpName (right.Type);
2054 ec.Report.Error (19, loc, "Operator `{0}' cannot be applied to operands of type `{1}' and `{2}'",
2055 oper, l, r);
2058 protected void Error_OperatorCannotBeApplied (ResolveContext ec, Expression left, Expression right)
2060 Error_OperatorCannotBeApplied (ec, left, right, OperName (oper), loc);
2064 // Converts operator to System.Linq.Expressions.ExpressionType enum name
2066 string GetOperatorExpressionTypeName ()
2068 switch (oper) {
2069 case Operator.Addition:
2070 return is_compound ? "AddAssign" : "Add";
2071 case Operator.BitwiseAnd:
2072 return is_compound ? "AndAssign" : "And";
2073 case Operator.BitwiseOr:
2074 return is_compound ? "OrAssign" : "Or";
2075 case Operator.Division:
2076 return is_compound ? "DivideAssign" : "Divide";
2077 case Operator.ExclusiveOr:
2078 return is_compound ? "ExclusiveOrAssign" : "ExclusiveOr";
2079 case Operator.Equality:
2080 return "Equal";
2081 case Operator.GreaterThan:
2082 return "GreaterThan";
2083 case Operator.GreaterThanOrEqual:
2084 return "GreaterThanOrEqual";
2085 case Operator.Inequality:
2086 return "NotEqual";
2087 case Operator.LeftShift:
2088 return is_compound ? "LeftShiftAssign" : "LeftShift";
2089 case Operator.LessThan:
2090 return "LessThan";
2091 case Operator.LessThanOrEqual:
2092 return "LessThanOrEqual";
2093 case Operator.LogicalAnd:
2094 return "And";
2095 case Operator.LogicalOr:
2096 return "Or";
2097 case Operator.Modulus:
2098 return is_compound ? "ModuloAssign" : "Modulo";
2099 case Operator.Multiply:
2100 return is_compound ? "MultiplyAssign" : "Multiply";
2101 case Operator.RightShift:
2102 return is_compound ? "RightShiftAssign" : "RightShift";
2103 case Operator.Subtraction:
2104 return is_compound ? "SubtractAssign" : "Subtract";
2105 default:
2106 throw new NotImplementedException ("Unknown expression type operator " + oper.ToString ());
2110 static string GetOperatorMetadataName (Operator op)
2112 CSharp.Operator.OpType op_type;
2113 switch (op) {
2114 case Operator.Addition:
2115 op_type = CSharp.Operator.OpType.Addition; break;
2116 case Operator.BitwiseAnd:
2117 op_type = CSharp.Operator.OpType.BitwiseAnd; break;
2118 case Operator.BitwiseOr:
2119 op_type = CSharp.Operator.OpType.BitwiseOr; break;
2120 case Operator.Division:
2121 op_type = CSharp.Operator.OpType.Division; break;
2122 case Operator.Equality:
2123 op_type = CSharp.Operator.OpType.Equality; break;
2124 case Operator.ExclusiveOr:
2125 op_type = CSharp.Operator.OpType.ExclusiveOr; break;
2126 case Operator.GreaterThan:
2127 op_type = CSharp.Operator.OpType.GreaterThan; break;
2128 case Operator.GreaterThanOrEqual:
2129 op_type = CSharp.Operator.OpType.GreaterThanOrEqual; break;
2130 case Operator.Inequality:
2131 op_type = CSharp.Operator.OpType.Inequality; break;
2132 case Operator.LeftShift:
2133 op_type = CSharp.Operator.OpType.LeftShift; break;
2134 case Operator.LessThan:
2135 op_type = CSharp.Operator.OpType.LessThan; break;
2136 case Operator.LessThanOrEqual:
2137 op_type = CSharp.Operator.OpType.LessThanOrEqual; break;
2138 case Operator.Modulus:
2139 op_type = CSharp.Operator.OpType.Modulus; break;
2140 case Operator.Multiply:
2141 op_type = CSharp.Operator.OpType.Multiply; break;
2142 case Operator.RightShift:
2143 op_type = CSharp.Operator.OpType.RightShift; break;
2144 case Operator.Subtraction:
2145 op_type = CSharp.Operator.OpType.Subtraction; break;
2146 default:
2147 throw new InternalErrorException (op.ToString ());
2150 return CSharp.Operator.GetMetadataName (op_type);
2153 public static void EmitOperatorOpcode (EmitContext ec, Operator oper, Type l)
2155 OpCode opcode;
2156 ILGenerator ig = ec.ig;
2158 switch (oper){
2159 case Operator.Multiply:
2160 if (ec.HasSet (EmitContext.Options.CheckedScope)) {
2161 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2162 opcode = OpCodes.Mul_Ovf;
2163 else if (!IsFloat (l))
2164 opcode = OpCodes.Mul_Ovf_Un;
2165 else
2166 opcode = OpCodes.Mul;
2167 } else
2168 opcode = OpCodes.Mul;
2170 break;
2172 case Operator.Division:
2173 if (IsUnsigned (l))
2174 opcode = OpCodes.Div_Un;
2175 else
2176 opcode = OpCodes.Div;
2177 break;
2179 case Operator.Modulus:
2180 if (IsUnsigned (l))
2181 opcode = OpCodes.Rem_Un;
2182 else
2183 opcode = OpCodes.Rem;
2184 break;
2186 case Operator.Addition:
2187 if (ec.HasSet (EmitContext.Options.CheckedScope)) {
2188 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2189 opcode = OpCodes.Add_Ovf;
2190 else if (!IsFloat (l))
2191 opcode = OpCodes.Add_Ovf_Un;
2192 else
2193 opcode = OpCodes.Add;
2194 } else
2195 opcode = OpCodes.Add;
2196 break;
2198 case Operator.Subtraction:
2199 if (ec.HasSet (EmitContext.Options.CheckedScope)) {
2200 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2201 opcode = OpCodes.Sub_Ovf;
2202 else if (!IsFloat (l))
2203 opcode = OpCodes.Sub_Ovf_Un;
2204 else
2205 opcode = OpCodes.Sub;
2206 } else
2207 opcode = OpCodes.Sub;
2208 break;
2210 case Operator.RightShift:
2211 if (IsUnsigned (l))
2212 opcode = OpCodes.Shr_Un;
2213 else
2214 opcode = OpCodes.Shr;
2215 break;
2217 case Operator.LeftShift:
2218 opcode = OpCodes.Shl;
2219 break;
2221 case Operator.Equality:
2222 opcode = OpCodes.Ceq;
2223 break;
2225 case Operator.Inequality:
2226 ig.Emit (OpCodes.Ceq);
2227 ig.Emit (OpCodes.Ldc_I4_0);
2229 opcode = OpCodes.Ceq;
2230 break;
2232 case Operator.LessThan:
2233 if (IsUnsigned (l))
2234 opcode = OpCodes.Clt_Un;
2235 else
2236 opcode = OpCodes.Clt;
2237 break;
2239 case Operator.GreaterThan:
2240 if (IsUnsigned (l))
2241 opcode = OpCodes.Cgt_Un;
2242 else
2243 opcode = OpCodes.Cgt;
2244 break;
2246 case Operator.LessThanOrEqual:
2247 if (IsUnsigned (l) || IsFloat (l))
2248 ig.Emit (OpCodes.Cgt_Un);
2249 else
2250 ig.Emit (OpCodes.Cgt);
2251 ig.Emit (OpCodes.Ldc_I4_0);
2253 opcode = OpCodes.Ceq;
2254 break;
2256 case Operator.GreaterThanOrEqual:
2257 if (IsUnsigned (l) || IsFloat (l))
2258 ig.Emit (OpCodes.Clt_Un);
2259 else
2260 ig.Emit (OpCodes.Clt);
2262 ig.Emit (OpCodes.Ldc_I4_0);
2264 opcode = OpCodes.Ceq;
2265 break;
2267 case Operator.BitwiseOr:
2268 opcode = OpCodes.Or;
2269 break;
2271 case Operator.BitwiseAnd:
2272 opcode = OpCodes.And;
2273 break;
2275 case Operator.ExclusiveOr:
2276 opcode = OpCodes.Xor;
2277 break;
2279 default:
2280 throw new InternalErrorException (oper.ToString ());
2283 ig.Emit (opcode);
2286 static bool IsUnsigned (Type t)
2288 if (t.IsPointer)
2289 return true;
2291 return (t == TypeManager.uint32_type || t == TypeManager.uint64_type ||
2292 t == TypeManager.ushort_type || t == TypeManager.byte_type);
2295 static bool IsFloat (Type t)
2297 return t == TypeManager.float_type || t == TypeManager.double_type;
2300 Expression ResolveOperator (ResolveContext ec)
2302 Type l = left.Type;
2303 Type r = right.Type;
2304 Expression expr;
2305 bool primitives_only = false;
2307 if (standard_operators == null)
2308 CreateStandardOperatorsTable ();
2311 // Handles predefined primitive types
2313 if (TypeManager.IsPrimitiveType (l) && TypeManager.IsPrimitiveType (r)) {
2314 if ((oper & Operator.ShiftMask) == 0) {
2315 if (l != TypeManager.bool_type && !DoBinaryOperatorPromotion (ec))
2316 return null;
2318 primitives_only = true;
2320 } else {
2321 // Pointers
2322 if (l.IsPointer || r.IsPointer)
2323 return ResolveOperatorPointer (ec, l, r);
2325 // Enums
2326 bool lenum = TypeManager.IsEnumType (l);
2327 bool renum = TypeManager.IsEnumType (r);
2328 if (lenum || renum) {
2329 expr = ResolveOperatorEnum (ec, lenum, renum, l, r);
2331 // TODO: Can this be ambiguous
2332 if (expr != null)
2333 return expr;
2336 // Delegates
2337 if ((oper == Operator.Addition || oper == Operator.Subtraction || (oper & Operator.EqualityMask) != 0) &&
2338 (TypeManager.IsDelegateType (l) || TypeManager.IsDelegateType (r))) {
2340 expr = ResolveOperatorDelegate (ec, l, r);
2342 // TODO: Can this be ambiguous
2343 if (expr != null)
2344 return expr;
2347 // User operators
2348 expr = ResolveUserOperator (ec, l, r);
2349 if (expr != null)
2350 return expr;
2352 // Predefined reference types equality
2353 if ((oper & Operator.EqualityMask) != 0) {
2354 expr = ResolveOperatorEqualityRerefence (ec, l, r);
2355 if (expr != null)
2356 return expr;
2360 return ResolveOperatorPredefined (ec, standard_operators, primitives_only, null);
2363 // at least one of 'left' or 'right' is an enumeration constant (EnumConstant or SideEffectConstant or ...)
2364 // if 'left' is not an enumeration constant, create one from the type of 'right'
2365 Constant EnumLiftUp (ResolveContext ec, Constant left, Constant right, Location loc)
2367 switch (oper) {
2368 case Operator.BitwiseOr:
2369 case Operator.BitwiseAnd:
2370 case Operator.ExclusiveOr:
2371 case Operator.Equality:
2372 case Operator.Inequality:
2373 case Operator.LessThan:
2374 case Operator.LessThanOrEqual:
2375 case Operator.GreaterThan:
2376 case Operator.GreaterThanOrEqual:
2377 if (TypeManager.IsEnumType (left.Type))
2378 return left;
2380 if (left.IsZeroInteger)
2381 return left.TryReduce (ec, right.Type, loc);
2383 break;
2385 case Operator.Addition:
2386 case Operator.Subtraction:
2387 return left;
2389 case Operator.Multiply:
2390 case Operator.Division:
2391 case Operator.Modulus:
2392 case Operator.LeftShift:
2393 case Operator.RightShift:
2394 if (TypeManager.IsEnumType (right.Type) || TypeManager.IsEnumType (left.Type))
2395 break;
2396 return left;
2398 Error_OperatorCannotBeApplied (ec, this.left, this.right);
2399 return null;
2403 // The `|' operator used on types which were extended is dangerous
2405 void CheckBitwiseOrOnSignExtended (ResolveContext ec)
2407 OpcodeCast lcast = left as OpcodeCast;
2408 if (lcast != null) {
2409 if (IsUnsigned (lcast.UnderlyingType))
2410 lcast = null;
2413 OpcodeCast rcast = right as OpcodeCast;
2414 if (rcast != null) {
2415 if (IsUnsigned (rcast.UnderlyingType))
2416 rcast = null;
2419 if (lcast == null && rcast == null)
2420 return;
2422 // FIXME: consider constants
2424 ec.Report.Warning (675, 3, loc,
2425 "The operator `|' used on the sign-extended type `{0}'. Consider casting to a smaller unsigned type first",
2426 TypeManager.CSharpName (lcast != null ? lcast.UnderlyingType : rcast.UnderlyingType));
2429 static void CreatePointerOperatorsTable ()
2431 ArrayList temp = new ArrayList ();
2434 // Pointer arithmetic:
2436 // T* operator + (T* x, int y); T* operator - (T* x, int y);
2437 // T* operator + (T* x, uint y); T* operator - (T* x, uint y);
2438 // T* operator + (T* x, long y); T* operator - (T* x, long y);
2439 // T* operator + (T* x, ulong y); T* operator - (T* x, ulong y);
2441 temp.Add (new PredefinedPointerOperator (null, TypeManager.int32_type, Operator.AdditionMask | Operator.SubtractionMask));
2442 temp.Add (new PredefinedPointerOperator (null, TypeManager.uint32_type, Operator.AdditionMask | Operator.SubtractionMask));
2443 temp.Add (new PredefinedPointerOperator (null, TypeManager.int64_type, Operator.AdditionMask | Operator.SubtractionMask));
2444 temp.Add (new PredefinedPointerOperator (null, TypeManager.uint64_type, Operator.AdditionMask | Operator.SubtractionMask));
2447 // T* operator + (int y, T* x);
2448 // T* operator + (uint y, T *x);
2449 // T* operator + (long y, T *x);
2450 // T* operator + (ulong y, T *x);
2452 temp.Add (new PredefinedPointerOperator (TypeManager.int32_type, null, Operator.AdditionMask, null));
2453 temp.Add (new PredefinedPointerOperator (TypeManager.uint32_type, null, Operator.AdditionMask, null));
2454 temp.Add (new PredefinedPointerOperator (TypeManager.int64_type, null, Operator.AdditionMask, null));
2455 temp.Add (new PredefinedPointerOperator (TypeManager.uint64_type, null, Operator.AdditionMask, null));
2458 // long operator - (T* x, T *y)
2460 temp.Add (new PredefinedPointerOperator (null, Operator.SubtractionMask, TypeManager.int64_type));
2462 pointer_operators = (PredefinedOperator []) temp.ToArray (typeof (PredefinedOperator));
2465 static void CreateStandardOperatorsTable ()
2467 ArrayList temp = new ArrayList ();
2468 Type bool_type = TypeManager.bool_type;
2470 temp.Add (new PredefinedOperator (TypeManager.int32_type, Operator.ArithmeticMask | Operator.BitwiseMask));
2471 temp.Add (new PredefinedOperator (TypeManager.uint32_type, Operator.ArithmeticMask | Operator.BitwiseMask));
2472 temp.Add (new PredefinedOperator (TypeManager.int64_type, Operator.ArithmeticMask | Operator.BitwiseMask));
2473 temp.Add (new PredefinedOperator (TypeManager.uint64_type, Operator.ArithmeticMask | Operator.BitwiseMask));
2474 temp.Add (new PredefinedOperator (TypeManager.float_type, Operator.ArithmeticMask));
2475 temp.Add (new PredefinedOperator (TypeManager.double_type, Operator.ArithmeticMask));
2476 temp.Add (new PredefinedOperator (TypeManager.decimal_type, Operator.ArithmeticMask));
2478 temp.Add (new PredefinedOperator (TypeManager.int32_type, Operator.ComparisonMask, bool_type));
2479 temp.Add (new PredefinedOperator (TypeManager.uint32_type, Operator.ComparisonMask, bool_type));
2480 temp.Add (new PredefinedOperator (TypeManager.int64_type, Operator.ComparisonMask, bool_type));
2481 temp.Add (new PredefinedOperator (TypeManager.uint64_type, Operator.ComparisonMask, bool_type));
2482 temp.Add (new PredefinedOperator (TypeManager.float_type, Operator.ComparisonMask, bool_type));
2483 temp.Add (new PredefinedOperator (TypeManager.double_type, Operator.ComparisonMask, bool_type));
2484 temp.Add (new PredefinedOperator (TypeManager.decimal_type, Operator.ComparisonMask, bool_type));
2486 temp.Add (new PredefinedOperator (TypeManager.string_type, Operator.EqualityMask, bool_type));
2488 temp.Add (new PredefinedStringOperator (TypeManager.string_type, Operator.AdditionMask));
2489 temp.Add (new PredefinedStringOperator (TypeManager.string_type, TypeManager.object_type, Operator.AdditionMask));
2490 temp.Add (new PredefinedStringOperator (TypeManager.object_type, TypeManager.string_type, Operator.AdditionMask));
2492 temp.Add (new PredefinedOperator (bool_type,
2493 Operator.BitwiseMask | Operator.LogicalMask | Operator.EqualityMask, bool_type));
2495 temp.Add (new PredefinedShiftOperator (TypeManager.int32_type, Operator.ShiftMask));
2496 temp.Add (new PredefinedShiftOperator (TypeManager.uint32_type, Operator.ShiftMask));
2497 temp.Add (new PredefinedShiftOperator (TypeManager.int64_type, Operator.ShiftMask));
2498 temp.Add (new PredefinedShiftOperator (TypeManager.uint64_type, Operator.ShiftMask));
2500 standard_operators = (PredefinedOperator []) temp.ToArray (typeof (PredefinedOperator));
2504 // Rules used during binary numeric promotion
2506 static bool DoNumericPromotion (ref Expression prim_expr, ref Expression second_expr, Type type)
2508 Expression temp;
2509 Type etype;
2511 Constant c = prim_expr as Constant;
2512 if (c != null) {
2513 temp = c.ConvertImplicitly (type);
2514 if (temp != null) {
2515 prim_expr = temp;
2516 return true;
2520 if (type == TypeManager.uint32_type) {
2521 etype = prim_expr.Type;
2522 if (etype == TypeManager.int32_type || etype == TypeManager.short_type || etype == TypeManager.sbyte_type) {
2523 type = TypeManager.int64_type;
2525 if (type != second_expr.Type) {
2526 c = second_expr as Constant;
2527 if (c != null)
2528 temp = c.ConvertImplicitly (type);
2529 else
2530 temp = Convert.ImplicitNumericConversion (second_expr, type);
2531 if (temp == null)
2532 return false;
2533 second_expr = temp;
2536 } else if (type == TypeManager.uint64_type) {
2538 // A compile-time error occurs if the other operand is of type sbyte, short, int, or long
2540 if (type == TypeManager.int32_type || type == TypeManager.int64_type ||
2541 type == TypeManager.sbyte_type || type == TypeManager.sbyte_type)
2542 return false;
2545 temp = Convert.ImplicitNumericConversion (prim_expr, type);
2546 if (temp == null)
2547 return false;
2549 prim_expr = temp;
2550 return true;
2554 // 7.2.6.2 Binary numeric promotions
2556 public bool DoBinaryOperatorPromotion (ResolveContext ec)
2558 Type ltype = left.Type;
2559 Type rtype = right.Type;
2560 Expression temp;
2562 foreach (Type t in ConstantFold.binary_promotions) {
2563 if (t == ltype)
2564 return t == rtype || DoNumericPromotion (ref right, ref left, t);
2566 if (t == rtype)
2567 return t == ltype || DoNumericPromotion (ref left, ref right, t);
2570 Type int32 = TypeManager.int32_type;
2571 if (ltype != int32) {
2572 Constant c = left as Constant;
2573 if (c != null)
2574 temp = c.ConvertImplicitly (int32);
2575 else
2576 temp = Convert.ImplicitNumericConversion (left, int32);
2578 if (temp == null)
2579 return false;
2580 left = temp;
2583 if (rtype != int32) {
2584 Constant c = right as Constant;
2585 if (c != null)
2586 temp = c.ConvertImplicitly (int32);
2587 else
2588 temp = Convert.ImplicitNumericConversion (right, int32);
2590 if (temp == null)
2591 return false;
2592 right = temp;
2595 return true;
2598 public override Expression DoResolve (ResolveContext ec)
2600 if (left == null)
2601 return null;
2603 if ((oper == Operator.Subtraction) && (left is ParenthesizedExpression)) {
2604 left = ((ParenthesizedExpression) left).Expr;
2605 left = left.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.Type);
2606 if (left == null)
2607 return null;
2609 if (left.eclass == ExprClass.Type) {
2610 ec.Report.Error (75, loc, "To cast a negative value, you must enclose the value in parentheses");
2611 return null;
2613 } else
2614 left = left.Resolve (ec);
2616 if (left == null)
2617 return null;
2619 Constant lc = left as Constant;
2621 if (lc != null && lc.Type == TypeManager.bool_type &&
2622 ((oper == Operator.LogicalAnd && lc.IsDefaultValue) ||
2623 (oper == Operator.LogicalOr && !lc.IsDefaultValue))) {
2625 // FIXME: resolve right expression as unreachable
2626 // right.Resolve (ec);
2628 ec.Report.Warning (429, 4, loc, "Unreachable expression code detected");
2629 return left;
2632 right = right.Resolve (ec);
2633 if (right == null)
2634 return null;
2636 eclass = ExprClass.Value;
2637 Constant rc = right as Constant;
2639 // The conversion rules are ignored in enum context but why
2640 if (!ec.HasSet (ResolveContext.Options.EnumScope) && lc != null && rc != null && (TypeManager.IsEnumType (left.Type) || TypeManager.IsEnumType (right.Type))) {
2641 lc = EnumLiftUp (ec, lc, rc, loc);
2642 if (lc != null)
2643 rc = EnumLiftUp (ec, rc, lc, loc);
2646 if (rc != null && lc != null) {
2647 int prev_e = ec.Report.Errors;
2648 Expression e = ConstantFold.BinaryFold (
2649 ec, oper, lc, rc, loc);
2650 if (e != null || ec.Report.Errors != prev_e)
2651 return e;
2652 } else if ((oper == Operator.BitwiseAnd || oper == Operator.LogicalAnd) && !TypeManager.IsDynamicType (left.Type) &&
2653 ((lc != null && lc.IsDefaultValue && !(lc is NullLiteral)) || (rc != null && rc.IsDefaultValue && !(rc is NullLiteral)))) {
2655 if ((ResolveOperator (ec)) == null) {
2656 Error_OperatorCannotBeApplied (ec, left, right);
2657 return null;
2661 // The result is a constant with side-effect
2663 Constant side_effect = rc == null ?
2664 new SideEffectConstant (lc, right, loc) :
2665 new SideEffectConstant (rc, left, loc);
2667 return ReducedExpression.Create (side_effect, this);
2670 // Comparison warnings
2671 if ((oper & Operator.ComparisonMask) != 0) {
2672 if (left.Equals (right)) {
2673 ec.Report.Warning (1718, 3, loc, "A comparison made to same variable. Did you mean to compare something else?");
2675 CheckUselessComparison (ec, lc, right.Type);
2676 CheckUselessComparison (ec, rc, left.Type);
2679 if (TypeManager.IsDynamicType (left.Type) || TypeManager.IsDynamicType (right.Type)) {
2680 Arguments args = new Arguments (2);
2681 args.Add (new Argument (left));
2682 args.Add (new Argument (right));
2683 return new DynamicExpressionStatement (this, args, loc).Resolve (ec);
2686 if (RootContext.Version >= LanguageVersion.ISO_2 &&
2687 ((TypeManager.IsNullableType (left.Type) && (right is NullLiteral || TypeManager.IsNullableType (right.Type) || TypeManager.IsValueType (right.Type))) ||
2688 (TypeManager.IsValueType (left.Type) && right is NullLiteral) ||
2689 (TypeManager.IsNullableType (right.Type) && (left is NullLiteral || TypeManager.IsNullableType (left.Type) || TypeManager.IsValueType (left.Type))) ||
2690 (TypeManager.IsValueType (right.Type) && left is NullLiteral)))
2691 return new Nullable.LiftedBinaryOperator (oper, left, right, loc).Resolve (ec);
2693 return DoResolveCore (ec, left, right);
2696 protected Expression DoResolveCore (ResolveContext ec, Expression left_orig, Expression right_orig)
2698 Expression expr = ResolveOperator (ec);
2699 if (expr == null)
2700 Error_OperatorCannotBeApplied (ec, left_orig, right_orig);
2702 if (left == null || right == null)
2703 throw new InternalErrorException ("Invalid conversion");
2705 if (oper == Operator.BitwiseOr)
2706 CheckBitwiseOrOnSignExtended (ec);
2708 return expr;
2711 #if NET_4_0
2712 public override SLE.Expression MakeExpression (BuilderContext ctx)
2714 var le = left.MakeExpression (ctx);
2715 var re = right.MakeExpression (ctx);
2716 bool is_checked = ctx.HasSet (BuilderContext.Options.CheckedScope);
2718 switch (oper) {
2719 case Operator.Addition:
2720 return is_checked ? SLE.Expression.AddChecked (le, re) : SLE.Expression.Add (le, re);
2721 case Operator.BitwiseAnd:
2722 return SLE.Expression.And (le, re);
2723 case Operator.BitwiseOr:
2724 return SLE.Expression.Or (le, re);
2725 case Operator.Division:
2726 return SLE.Expression.Divide (le, re);
2727 case Operator.Equality:
2728 return SLE.Expression.Equal (le, re);
2729 case Operator.ExclusiveOr:
2730 return SLE.Expression.ExclusiveOr (le, re);
2731 case Operator.GreaterThan:
2732 return SLE.Expression.GreaterThan (le, re);
2733 case Operator.GreaterThanOrEqual:
2734 return SLE.Expression.GreaterThanOrEqual (le, re);
2735 case Operator.Inequality:
2736 return SLE.Expression.NotEqual (le, re);
2737 case Operator.LeftShift:
2738 return SLE.Expression.LeftShift (le, re);
2739 case Operator.LessThan:
2740 return SLE.Expression.LessThan (le, re);
2741 case Operator.LessThanOrEqual:
2742 return SLE.Expression.LessThanOrEqual (le, re);
2743 case Operator.LogicalAnd:
2744 return SLE.Expression.AndAlso (le, re);
2745 case Operator.LogicalOr:
2746 return SLE.Expression.OrElse (le, re);
2747 case Operator.Modulus:
2748 return SLE.Expression.Modulo (le, re);
2749 case Operator.Multiply:
2750 return is_checked ? SLE.Expression.MultiplyChecked (le, re) : SLE.Expression.Multiply (le, re);
2751 case Operator.RightShift:
2752 return SLE.Expression.RightShift (le, re);
2753 case Operator.Subtraction:
2754 return is_checked ? SLE.Expression.SubtractChecked (le, re) : SLE.Expression.Subtract (le, re);
2755 default:
2756 throw new NotImplementedException (oper.ToString ());
2759 #endif
2761 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
2763 left.MutateHoistedGenericType (storey);
2764 right.MutateHoistedGenericType (storey);
2768 // D operator + (D x, D y)
2769 // D operator - (D x, D y)
2770 // bool operator == (D x, D y)
2771 // bool operator != (D x, D y)
2773 Expression ResolveOperatorDelegate (ResolveContext ec, Type l, Type r)
2775 bool is_equality = (oper & Operator.EqualityMask) != 0;
2776 if (!TypeManager.IsEqual (l, r) && !TypeManager.IsVariantOf (r, l)) {
2777 Expression tmp;
2778 if (right.eclass == ExprClass.MethodGroup || (r == InternalType.AnonymousMethod && !is_equality)) {
2779 tmp = Convert.ImplicitConversionRequired (ec, right, l, loc);
2780 if (tmp == null)
2781 return null;
2782 right = tmp;
2783 r = right.Type;
2784 } else if (left.eclass == ExprClass.MethodGroup || (l == InternalType.AnonymousMethod && !is_equality)) {
2785 tmp = Convert.ImplicitConversionRequired (ec, left, r, loc);
2786 if (tmp == null)
2787 return null;
2788 left = tmp;
2789 l = left.Type;
2790 } else {
2791 return null;
2796 // Resolve delegate equality as a user operator
2798 if (is_equality)
2799 return ResolveUserOperator (ec, l, r);
2801 MethodInfo method;
2802 Arguments args = new Arguments (2);
2803 args.Add (new Argument (left));
2804 args.Add (new Argument (right));
2806 if (oper == Operator.Addition) {
2807 if (TypeManager.delegate_combine_delegate_delegate == null) {
2808 TypeManager.delegate_combine_delegate_delegate = TypeManager.GetPredefinedMethod (
2809 TypeManager.delegate_type, "Combine", loc, TypeManager.delegate_type, TypeManager.delegate_type);
2812 method = TypeManager.delegate_combine_delegate_delegate;
2813 } else {
2814 if (TypeManager.delegate_remove_delegate_delegate == null) {
2815 TypeManager.delegate_remove_delegate_delegate = TypeManager.GetPredefinedMethod (
2816 TypeManager.delegate_type, "Remove", loc, TypeManager.delegate_type, TypeManager.delegate_type);
2819 method = TypeManager.delegate_remove_delegate_delegate;
2822 MethodGroupExpr mg = new MethodGroupExpr (new MemberInfo [] { method }, TypeManager.delegate_type, loc);
2823 mg = mg.OverloadResolve (ec, ref args, false, loc);
2825 return new ClassCast (new UserOperatorCall (mg, args, CreateExpressionTree, loc), l);
2829 // Enumeration operators
2831 Expression ResolveOperatorEnum (ResolveContext ec, bool lenum, bool renum, Type ltype, Type rtype)
2834 // bool operator == (E x, E y);
2835 // bool operator != (E x, E y);
2836 // bool operator < (E x, E y);
2837 // bool operator > (E x, E y);
2838 // bool operator <= (E x, E y);
2839 // bool operator >= (E x, E y);
2841 // E operator & (E x, E y);
2842 // E operator | (E x, E y);
2843 // E operator ^ (E x, E y);
2845 // U operator - (E e, E f)
2846 // E operator - (E e, U x)
2848 // E operator + (U x, E e)
2849 // E operator + (E e, U x)
2851 if (!((oper & (Operator.ComparisonMask | Operator.BitwiseMask)) != 0 ||
2852 (oper == Operator.Subtraction && lenum) ||
2853 (oper == Operator.Addition && (lenum != renum || type != null)))) // type != null for lifted null
2854 return null;
2856 Expression ltemp = left;
2857 Expression rtemp = right;
2858 Type underlying_type;
2859 Expression expr;
2861 if ((oper & (Operator.ComparisonMask | Operator.BitwiseMask)) != 0) {
2862 if (renum) {
2863 expr = Convert.ImplicitConversion (ec, left, rtype, loc);
2864 if (expr != null) {
2865 left = expr;
2866 ltype = expr.Type;
2868 } else if (lenum) {
2869 expr = Convert.ImplicitConversion (ec, right, ltype, loc);
2870 if (expr != null) {
2871 right = expr;
2872 rtype = expr.Type;
2877 if (TypeManager.IsEqual (ltype, rtype)) {
2878 underlying_type = TypeManager.GetEnumUnderlyingType (ltype);
2880 if (left is Constant)
2881 left = ((Constant) left).ConvertExplicitly (false, underlying_type);
2882 else
2883 left = EmptyCast.Create (left, underlying_type);
2885 if (right is Constant)
2886 right = ((Constant) right).ConvertExplicitly (false, underlying_type);
2887 else
2888 right = EmptyCast.Create (right, underlying_type);
2889 } else if (lenum) {
2890 underlying_type = TypeManager.GetEnumUnderlyingType (ltype);
2892 if (oper != Operator.Subtraction && oper != Operator.Addition) {
2893 Constant c = right as Constant;
2894 if (c == null || !c.IsDefaultValue)
2895 return null;
2896 } else {
2897 if (!Convert.ImplicitStandardConversionExists (right, underlying_type))
2898 return null;
2900 right = Convert.ImplicitConversionStandard (ec, right, underlying_type, right.Location);
2903 if (left is Constant)
2904 left = ((Constant) left).ConvertExplicitly (false, underlying_type);
2905 else
2906 left = EmptyCast.Create (left, underlying_type);
2908 } else if (renum) {
2909 underlying_type = TypeManager.GetEnumUnderlyingType (rtype);
2911 if (oper != Operator.Addition) {
2912 Constant c = left as Constant;
2913 if (c == null || !c.IsDefaultValue)
2914 return null;
2915 } else {
2916 if (!Convert.ImplicitStandardConversionExists (left, underlying_type))
2917 return null;
2919 left = Convert.ImplicitConversionStandard (ec, left, underlying_type, left.Location);
2922 if (right is Constant)
2923 right = ((Constant) right).ConvertExplicitly (false, underlying_type);
2924 else
2925 right = EmptyCast.Create (right, underlying_type);
2927 } else {
2928 return null;
2932 // C# specification uses explicit cast syntax which means binary promotion
2933 // should happen, however it seems that csc does not do that
2935 if (!DoBinaryOperatorPromotion (ec)) {
2936 left = ltemp;
2937 right = rtemp;
2938 return null;
2941 Type res_type = null;
2942 if ((oper & Operator.BitwiseMask) != 0 || oper == Operator.Subtraction || oper == Operator.Addition) {
2943 Type promoted_type = lenum ? left.Type : right.Type;
2944 enum_conversion = Convert.ExplicitNumericConversion (
2945 new EmptyExpression (promoted_type), underlying_type);
2947 if (oper == Operator.Subtraction && renum && lenum)
2948 res_type = underlying_type;
2949 else if (oper == Operator.Addition && renum)
2950 res_type = rtype;
2951 else
2952 res_type = ltype;
2955 expr = ResolveOperatorPredefined (ec, standard_operators, true, res_type);
2956 if (!is_compound || expr == null)
2957 return expr;
2960 // Section: 7.16.2
2964 // If the return type of the selected operator is implicitly convertible to the type of x
2966 if (Convert.ImplicitConversionExists (ec, expr, ltype))
2967 return expr;
2970 // Otherwise, if the selected operator is a predefined operator, if the return type of the
2971 // selected operator is explicitly convertible to the type of x, and if y is implicitly
2972 // convertible to the type of x or the operator is a shift operator, then the operation
2973 // is evaluated as x = (T)(x op y), where T is the type of x
2975 expr = Convert.ExplicitConversion (ec, expr, ltype, loc);
2976 if (expr == null)
2977 return null;
2979 if (Convert.ImplicitConversionExists (ec, ltemp, ltype))
2980 return expr;
2982 return null;
2986 // 7.9.6 Reference type equality operators
2988 Binary ResolveOperatorEqualityRerefence (ResolveContext ec, Type l, Type r)
2991 // operator != (object a, object b)
2992 // operator == (object a, object b)
2995 // TODO: this method is almost equivalent to Convert.ImplicitReferenceConversion
2997 if (left.eclass == ExprClass.MethodGroup || right.eclass == ExprClass.MethodGroup)
2998 return null;
3000 type = TypeManager.bool_type;
3001 GenericConstraints constraints;
3003 bool lgen = TypeManager.IsGenericParameter (l);
3005 if (TypeManager.IsEqual (l, r)) {
3006 if (lgen) {
3008 // Only allow to compare same reference type parameter
3010 if (TypeManager.IsReferenceType (l)) {
3011 left = new BoxedCast (left, TypeManager.object_type);
3012 right = new BoxedCast (right, TypeManager.object_type);
3013 return this;
3016 return null;
3019 if (l == InternalType.AnonymousMethod)
3020 return null;
3022 if (TypeManager.IsValueType (l))
3023 return null;
3025 return this;
3028 bool rgen = TypeManager.IsGenericParameter (r);
3031 // a, Both operands are reference-type values or the value null
3032 // b, One operand is a value of type T where T is a type-parameter and
3033 // the other operand is the value null. Furthermore T does not have the
3034 // value type constrain
3036 if (left is NullLiteral || right is NullLiteral) {
3037 if (lgen) {
3038 constraints = TypeManager.GetTypeParameterConstraints (l);
3039 if (constraints != null && constraints.HasValueTypeConstraint)
3040 return null;
3042 left = new BoxedCast (left, TypeManager.object_type);
3043 return this;
3046 if (rgen) {
3047 constraints = TypeManager.GetTypeParameterConstraints (r);
3048 if (constraints != null && constraints.HasValueTypeConstraint)
3049 return null;
3051 right = new BoxedCast (right, TypeManager.object_type);
3052 return this;
3057 // An interface is converted to the object before the
3058 // standard conversion is applied. It's not clear from the
3059 // standard but it looks like it works like that.
3061 if (lgen) {
3062 if (!TypeManager.IsReferenceType (l))
3063 return null;
3065 l = TypeManager.object_type;
3066 left = new BoxedCast (left, l);
3067 } else if (l.IsInterface) {
3068 l = TypeManager.object_type;
3069 } else if (TypeManager.IsStruct (l)) {
3070 return null;
3073 if (rgen) {
3074 if (!TypeManager.IsReferenceType (r))
3075 return null;
3077 r = TypeManager.object_type;
3078 right = new BoxedCast (right, r);
3079 } else if (r.IsInterface) {
3080 r = TypeManager.object_type;
3081 } else if (TypeManager.IsStruct (r)) {
3082 return null;
3086 const string ref_comparison = "Possible unintended reference comparison. " +
3087 "Consider casting the {0} side of the expression to `string' to compare the values";
3090 // A standard implicit conversion exists from the type of either
3091 // operand to the type of the other operand
3093 if (Convert.ImplicitReferenceConversionExists (left, r)) {
3094 if (l == TypeManager.string_type)
3095 ec.Report.Warning (253, 2, loc, ref_comparison, "right");
3097 return this;
3100 if (Convert.ImplicitReferenceConversionExists (right, l)) {
3101 if (r == TypeManager.string_type)
3102 ec.Report.Warning (252, 2, loc, ref_comparison, "left");
3104 return this;
3107 return null;
3111 Expression ResolveOperatorPointer (ResolveContext ec, Type l, Type r)
3114 // bool operator == (void* x, void* y);
3115 // bool operator != (void* x, void* y);
3116 // bool operator < (void* x, void* y);
3117 // bool operator > (void* x, void* y);
3118 // bool operator <= (void* x, void* y);
3119 // bool operator >= (void* x, void* y);
3121 if ((oper & Operator.ComparisonMask) != 0) {
3122 Expression temp;
3123 if (!l.IsPointer) {
3124 temp = Convert.ImplicitConversion (ec, left, r, left.Location);
3125 if (temp == null)
3126 return null;
3127 left = temp;
3130 if (!r.IsPointer) {
3131 temp = Convert.ImplicitConversion (ec, right, l, right.Location);
3132 if (temp == null)
3133 return null;
3134 right = temp;
3137 type = TypeManager.bool_type;
3138 return this;
3141 if (pointer_operators == null)
3142 CreatePointerOperatorsTable ();
3144 return ResolveOperatorPredefined (ec, pointer_operators, false, null);
3148 // Build-in operators method overloading
3150 protected virtual Expression ResolveOperatorPredefined (ResolveContext ec, PredefinedOperator [] operators, bool primitives_only, Type enum_type)
3152 PredefinedOperator best_operator = null;
3153 Type l = left.Type;
3154 Type r = right.Type;
3155 Operator oper_mask = oper & ~Operator.ValuesOnlyMask;
3157 foreach (PredefinedOperator po in operators) {
3158 if ((po.OperatorsMask & oper_mask) == 0)
3159 continue;
3161 if (primitives_only) {
3162 if (!po.IsPrimitiveApplicable (l, r))
3163 continue;
3164 } else {
3165 if (!po.IsApplicable (ec, left, right))
3166 continue;
3169 if (best_operator == null) {
3170 best_operator = po;
3171 if (primitives_only)
3172 break;
3174 continue;
3177 best_operator = po.ResolveBetterOperator (ec, best_operator);
3179 if (best_operator == null) {
3180 ec.Report.Error (34, loc, "Operator `{0}' is ambiguous on operands of type `{1}' and `{2}'",
3181 OperName (oper), left.GetSignatureForError (), right.GetSignatureForError ());
3183 best_operator = po;
3184 break;
3188 if (best_operator == null)
3189 return null;
3191 Expression expr = best_operator.ConvertResult (ec, this);
3192 if (enum_type == null)
3193 return expr;
3196 // HACK: required by enum_conversion
3198 expr.Type = enum_type;
3199 return EmptyCast.Create (expr, enum_type);
3203 // Performs user-operator overloading
3205 protected virtual Expression ResolveUserOperator (ResolveContext ec, Type l, Type r)
3207 Operator user_oper;
3208 if (oper == Operator.LogicalAnd)
3209 user_oper = Operator.BitwiseAnd;
3210 else if (oper == Operator.LogicalOr)
3211 user_oper = Operator.BitwiseOr;
3212 else
3213 user_oper = oper;
3215 string op = GetOperatorMetadataName (user_oper);
3217 MethodGroupExpr left_operators = MemberLookup (ec.Compiler, ec.CurrentType, l, op, MemberTypes.Method, AllBindingFlags, loc) as MethodGroupExpr;
3218 MethodGroupExpr right_operators = null;
3220 if (!TypeManager.IsEqual (r, l)) {
3221 right_operators = MemberLookup (ec.Compiler, ec.CurrentType, r, op, MemberTypes.Method, AllBindingFlags, loc) as MethodGroupExpr;
3222 if (right_operators == null && left_operators == null)
3223 return null;
3224 } else if (left_operators == null) {
3225 return null;
3228 Arguments args = new Arguments (2);
3229 Argument larg = new Argument (left);
3230 args.Add (larg);
3231 Argument rarg = new Argument (right);
3232 args.Add (rarg);
3234 MethodGroupExpr union;
3237 // User-defined operator implementations always take precedence
3238 // over predefined operator implementations
3240 if (left_operators != null && right_operators != null) {
3241 if (IsPredefinedUserOperator (l, user_oper)) {
3242 union = right_operators.OverloadResolve (ec, ref args, true, loc);
3243 if (union == null)
3244 union = left_operators;
3245 } else if (IsPredefinedUserOperator (r, user_oper)) {
3246 union = left_operators.OverloadResolve (ec, ref args, true, loc);
3247 if (union == null)
3248 union = right_operators;
3249 } else {
3250 union = MethodGroupExpr.MakeUnionSet (left_operators, right_operators, loc);
3252 } else if (left_operators != null) {
3253 union = left_operators;
3254 } else {
3255 union = right_operators;
3258 union = union.OverloadResolve (ec, ref args, true, loc);
3259 if (union == null)
3260 return null;
3262 Expression oper_expr;
3264 // TODO: CreateExpressionTree is allocated every time
3265 if (user_oper != oper) {
3266 oper_expr = new ConditionalLogicalOperator (union, args, CreateExpressionTree,
3267 oper == Operator.LogicalAnd, loc).Resolve (ec);
3268 } else {
3269 oper_expr = new UserOperatorCall (union, args, CreateExpressionTree, loc);
3272 // This is used to check if a test 'x == null' can be optimized to a reference equals,
3273 // and not invoke user operator
3275 if ((oper & Operator.EqualityMask) != 0) {
3276 if ((left is NullLiteral && IsBuildInEqualityOperator (r)) ||
3277 (right is NullLiteral && IsBuildInEqualityOperator (l))) {
3278 type = TypeManager.bool_type;
3279 if (left is NullLiteral || right is NullLiteral)
3280 oper_expr = ReducedExpression.Create (this, oper_expr).Resolve (ec);
3281 } else if (l != r) {
3282 MethodInfo mi = (MethodInfo) union;
3285 // Two System.Delegate(s) are never equal
3287 if (mi.DeclaringType == TypeManager.multicast_delegate_type)
3288 return null;
3293 left = larg.Expr;
3294 right = rarg.Expr;
3295 return oper_expr;
3298 public override TypeExpr ResolveAsTypeTerminal (IMemberContext ec, bool silent)
3300 return null;
3303 private void CheckUselessComparison (ResolveContext ec, Constant c, Type type)
3305 if (c == null || !IsTypeIntegral (type)
3306 || c is StringConstant
3307 || c is BoolConstant
3308 || c is FloatConstant
3309 || c is DoubleConstant
3310 || c is DecimalConstant
3312 return;
3314 long value = 0;
3316 if (c is ULongConstant) {
3317 ulong uvalue = ((ULongConstant) c).Value;
3318 if (uvalue > long.MaxValue) {
3319 if (type == TypeManager.byte_type ||
3320 type == TypeManager.sbyte_type ||
3321 type == TypeManager.short_type ||
3322 type == TypeManager.ushort_type ||
3323 type == TypeManager.int32_type ||
3324 type == TypeManager.uint32_type ||
3325 type == TypeManager.int64_type ||
3326 type == TypeManager.char_type)
3327 WarnUselessComparison (ec, type);
3328 return;
3330 value = (long) uvalue;
3332 else if (c is ByteConstant)
3333 value = ((ByteConstant) c).Value;
3334 else if (c is SByteConstant)
3335 value = ((SByteConstant) c).Value;
3336 else if (c is ShortConstant)
3337 value = ((ShortConstant) c).Value;
3338 else if (c is UShortConstant)
3339 value = ((UShortConstant) c).Value;
3340 else if (c is IntConstant)
3341 value = ((IntConstant) c).Value;
3342 else if (c is UIntConstant)
3343 value = ((UIntConstant) c).Value;
3344 else if (c is LongConstant)
3345 value = ((LongConstant) c).Value;
3346 else if (c is CharConstant)
3347 value = ((CharConstant)c).Value;
3349 if (value == 0)
3350 return;
3352 if (IsValueOutOfRange (value, type))
3353 WarnUselessComparison (ec, type);
3356 static bool IsValueOutOfRange (long value, Type type)
3358 if (IsTypeUnsigned (type) && value < 0)
3359 return true;
3360 return type == TypeManager.sbyte_type && (value >= 0x80 || value < -0x80) ||
3361 type == TypeManager.byte_type && value >= 0x100 ||
3362 type == TypeManager.short_type && (value >= 0x8000 || value < -0x8000) ||
3363 type == TypeManager.ushort_type && value >= 0x10000 ||
3364 type == TypeManager.int32_type && (value >= 0x80000000 || value < -0x80000000) ||
3365 type == TypeManager.uint32_type && value >= 0x100000000;
3368 static bool IsBuildInEqualityOperator (Type t)
3370 return t == TypeManager.object_type || t == TypeManager.string_type ||
3371 t == TypeManager.delegate_type || TypeManager.IsDelegateType (t);
3374 static bool IsPredefinedUserOperator (Type t, Operator op)
3377 // Some predefined types have user operators
3379 return (op & Operator.EqualityMask) != 0 && (t == TypeManager.string_type || t == TypeManager.decimal_type);
3382 private static bool IsTypeIntegral (Type type)
3384 return type == TypeManager.uint64_type ||
3385 type == TypeManager.int64_type ||
3386 type == TypeManager.uint32_type ||
3387 type == TypeManager.int32_type ||
3388 type == TypeManager.ushort_type ||
3389 type == TypeManager.short_type ||
3390 type == TypeManager.sbyte_type ||
3391 type == TypeManager.byte_type ||
3392 type == TypeManager.char_type;
3395 private static bool IsTypeUnsigned (Type type)
3397 return type == TypeManager.uint64_type ||
3398 type == TypeManager.uint32_type ||
3399 type == TypeManager.ushort_type ||
3400 type == TypeManager.byte_type ||
3401 type == TypeManager.char_type;
3404 private void WarnUselessComparison (ResolveContext ec, Type type)
3406 ec.Report.Warning (652, 2, loc, "A comparison between a constant and a variable is useless. The constant is out of the range of the variable type `{0}'",
3407 TypeManager.CSharpName (type));
3410 /// <remarks>
3411 /// EmitBranchable is called from Statement.EmitBoolExpression in the
3412 /// context of a conditional bool expression. This function will return
3413 /// false if it is was possible to use EmitBranchable, or true if it was.
3415 /// The expression's code is generated, and we will generate a branch to `target'
3416 /// if the resulting expression value is equal to isTrue
3417 /// </remarks>
3418 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
3420 ILGenerator ig = ec.ig;
3423 // This is more complicated than it looks, but its just to avoid
3424 // duplicated tests: basically, we allow ==, !=, >, <, >= and <=
3425 // but on top of that we want for == and != to use a special path
3426 // if we are comparing against null
3428 if ((oper == Operator.Equality || oper == Operator.Inequality) && (left is Constant || right is Constant)) {
3429 bool my_on_true = oper == Operator.Inequality ? on_true : !on_true;
3432 // put the constant on the rhs, for simplicity
3434 if (left is Constant) {
3435 Expression swap = right;
3436 right = left;
3437 left = swap;
3440 if (((Constant) right).IsZeroInteger) {
3441 left.EmitBranchable (ec, target, my_on_true);
3442 return;
3444 if (right.Type == TypeManager.bool_type) {
3445 // right is a boolean, and it's not 'false' => it is 'true'
3446 left.EmitBranchable (ec, target, !my_on_true);
3447 return;
3450 } else if (oper == Operator.LogicalAnd) {
3452 if (on_true) {
3453 Label tests_end = ig.DefineLabel ();
3455 left.EmitBranchable (ec, tests_end, false);
3456 right.EmitBranchable (ec, target, true);
3457 ig.MarkLabel (tests_end);
3458 } else {
3460 // This optimizes code like this
3461 // if (true && i > 4)
3463 if (!(left is Constant))
3464 left.EmitBranchable (ec, target, false);
3466 if (!(right is Constant))
3467 right.EmitBranchable (ec, target, false);
3470 return;
3472 } else if (oper == Operator.LogicalOr){
3473 if (on_true) {
3474 left.EmitBranchable (ec, target, true);
3475 right.EmitBranchable (ec, target, true);
3477 } else {
3478 Label tests_end = ig.DefineLabel ();
3479 left.EmitBranchable (ec, tests_end, true);
3480 right.EmitBranchable (ec, target, false);
3481 ig.MarkLabel (tests_end);
3484 return;
3486 } else if (!(oper == Operator.LessThan || oper == Operator.GreaterThan ||
3487 oper == Operator.LessThanOrEqual || oper == Operator.GreaterThanOrEqual ||
3488 oper == Operator.Equality || oper == Operator.Inequality)) {
3489 base.EmitBranchable (ec, target, on_true);
3490 return;
3493 left.Emit (ec);
3494 right.Emit (ec);
3496 Type t = left.Type;
3497 bool is_float = IsFloat (t);
3498 bool is_unsigned = is_float || IsUnsigned (t);
3500 switch (oper){
3501 case Operator.Equality:
3502 if (on_true)
3503 ig.Emit (OpCodes.Beq, target);
3504 else
3505 ig.Emit (OpCodes.Bne_Un, target);
3506 break;
3508 case Operator.Inequality:
3509 if (on_true)
3510 ig.Emit (OpCodes.Bne_Un, target);
3511 else
3512 ig.Emit (OpCodes.Beq, target);
3513 break;
3515 case Operator.LessThan:
3516 if (on_true)
3517 if (is_unsigned && !is_float)
3518 ig.Emit (OpCodes.Blt_Un, target);
3519 else
3520 ig.Emit (OpCodes.Blt, target);
3521 else
3522 if (is_unsigned)
3523 ig.Emit (OpCodes.Bge_Un, target);
3524 else
3525 ig.Emit (OpCodes.Bge, target);
3526 break;
3528 case Operator.GreaterThan:
3529 if (on_true)
3530 if (is_unsigned && !is_float)
3531 ig.Emit (OpCodes.Bgt_Un, target);
3532 else
3533 ig.Emit (OpCodes.Bgt, target);
3534 else
3535 if (is_unsigned)
3536 ig.Emit (OpCodes.Ble_Un, target);
3537 else
3538 ig.Emit (OpCodes.Ble, target);
3539 break;
3541 case Operator.LessThanOrEqual:
3542 if (on_true)
3543 if (is_unsigned && !is_float)
3544 ig.Emit (OpCodes.Ble_Un, target);
3545 else
3546 ig.Emit (OpCodes.Ble, target);
3547 else
3548 if (is_unsigned)
3549 ig.Emit (OpCodes.Bgt_Un, target);
3550 else
3551 ig.Emit (OpCodes.Bgt, target);
3552 break;
3555 case Operator.GreaterThanOrEqual:
3556 if (on_true)
3557 if (is_unsigned && !is_float)
3558 ig.Emit (OpCodes.Bge_Un, target);
3559 else
3560 ig.Emit (OpCodes.Bge, target);
3561 else
3562 if (is_unsigned)
3563 ig.Emit (OpCodes.Blt_Un, target);
3564 else
3565 ig.Emit (OpCodes.Blt, target);
3566 break;
3567 default:
3568 throw new InternalErrorException (oper.ToString ());
3572 public override void Emit (EmitContext ec)
3574 EmitOperator (ec, left.Type);
3577 protected virtual void EmitOperator (EmitContext ec, Type l)
3579 ILGenerator ig = ec.ig;
3582 // Handle short-circuit operators differently
3583 // than the rest
3585 if ((oper & Operator.LogicalMask) != 0) {
3586 Label load_result = ig.DefineLabel ();
3587 Label end = ig.DefineLabel ();
3589 bool is_or = oper == Operator.LogicalOr;
3590 left.EmitBranchable (ec, load_result, is_or);
3591 right.Emit (ec);
3592 ig.Emit (OpCodes.Br_S, end);
3594 ig.MarkLabel (load_result);
3595 ig.Emit (is_or ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
3596 ig.MarkLabel (end);
3597 return;
3600 left.Emit (ec);
3603 // Optimize zero-based operations
3605 // TODO: Implement more optimizations, but it should probably go to PredefinedOperators
3607 if ((oper & Operator.ShiftMask) != 0 || oper == Operator.Addition || oper == Operator.Subtraction) {
3608 Constant rc = right as Constant;
3609 if (rc != null && rc.IsDefaultValue) {
3610 return;
3614 right.Emit (ec);
3615 EmitOperatorOpcode (ec, oper, l);
3618 // Nullable enum could require underlying type cast and we cannot simply wrap binary
3619 // expression because that would wrap lifted binary operation
3621 if (enum_conversion != null)
3622 enum_conversion.Emit (ec);
3625 public override void EmitSideEffect (EmitContext ec)
3627 if ((oper & Operator.LogicalMask) != 0 ||
3628 (ec.HasSet (EmitContext.Options.CheckedScope) && (oper == Operator.Multiply || oper == Operator.Addition || oper == Operator.Subtraction))) {
3629 base.EmitSideEffect (ec);
3630 } else {
3631 left.EmitSideEffect (ec);
3632 right.EmitSideEffect (ec);
3636 protected override void CloneTo (CloneContext clonectx, Expression t)
3638 Binary target = (Binary) t;
3640 target.left = left.Clone (clonectx);
3641 target.right = right.Clone (clonectx);
3644 public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
3646 Arguments binder_args = new Arguments (4);
3648 MemberAccess sle = new MemberAccess (new MemberAccess (
3649 new QualifiedAliasMember (QualifiedAliasMember.GlobalAlias, "System", loc), "Linq", loc), "Expressions", loc);
3651 MemberAccess binder = DynamicExpressionStatement.GetBinderNamespace (loc);
3653 binder_args.Add (new Argument (new MemberAccess (new MemberAccess (sle, "ExpressionType", loc), GetOperatorExpressionTypeName (), loc)));
3654 binder_args.Add (new Argument (new BoolLiteral (ec.HasSet (ResolveContext.Options.CheckedScope), loc)));
3656 bool member_access = left is DynamicMemberBinder || right is DynamicMemberBinder;
3657 binder_args.Add (new Argument (new BoolLiteral (member_access, loc)));
3658 binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation ("[]", args.CreateDynamicBinderArguments (), loc)));
3660 return new New (new MemberAccess (binder, "CSharpBinaryOperationBinder", loc), binder_args, loc);
3663 public override Expression CreateExpressionTree (ResolveContext ec)
3665 return CreateExpressionTree (ec, null);
3668 Expression CreateExpressionTree (ResolveContext ec, MethodGroupExpr method)
3670 string method_name;
3671 bool lift_arg = false;
3673 switch (oper) {
3674 case Operator.Addition:
3675 if (method == null && ec.HasSet (ResolveContext.Options.CheckedScope) && !IsFloat (type))
3676 method_name = "AddChecked";
3677 else
3678 method_name = "Add";
3679 break;
3680 case Operator.BitwiseAnd:
3681 method_name = "And";
3682 break;
3683 case Operator.BitwiseOr:
3684 method_name = "Or";
3685 break;
3686 case Operator.Division:
3687 method_name = "Divide";
3688 break;
3689 case Operator.Equality:
3690 method_name = "Equal";
3691 lift_arg = true;
3692 break;
3693 case Operator.ExclusiveOr:
3694 method_name = "ExclusiveOr";
3695 break;
3696 case Operator.GreaterThan:
3697 method_name = "GreaterThan";
3698 lift_arg = true;
3699 break;
3700 case Operator.GreaterThanOrEqual:
3701 method_name = "GreaterThanOrEqual";
3702 lift_arg = true;
3703 break;
3704 case Operator.Inequality:
3705 method_name = "NotEqual";
3706 lift_arg = true;
3707 break;
3708 case Operator.LeftShift:
3709 method_name = "LeftShift";
3710 break;
3711 case Operator.LessThan:
3712 method_name = "LessThan";
3713 lift_arg = true;
3714 break;
3715 case Operator.LessThanOrEqual:
3716 method_name = "LessThanOrEqual";
3717 lift_arg = true;
3718 break;
3719 case Operator.LogicalAnd:
3720 method_name = "AndAlso";
3721 break;
3722 case Operator.LogicalOr:
3723 method_name = "OrElse";
3724 break;
3725 case Operator.Modulus:
3726 method_name = "Modulo";
3727 break;
3728 case Operator.Multiply:
3729 if (method == null && ec.HasSet (ResolveContext.Options.CheckedScope) && !IsFloat (type))
3730 method_name = "MultiplyChecked";
3731 else
3732 method_name = "Multiply";
3733 break;
3734 case Operator.RightShift:
3735 method_name = "RightShift";
3736 break;
3737 case Operator.Subtraction:
3738 if (method == null && ec.HasSet (ResolveContext.Options.CheckedScope) && !IsFloat (type))
3739 method_name = "SubtractChecked";
3740 else
3741 method_name = "Subtract";
3742 break;
3744 default:
3745 throw new InternalErrorException ("Unknown expression tree binary operator " + oper);
3748 Arguments args = new Arguments (2);
3749 args.Add (new Argument (left.CreateExpressionTree (ec)));
3750 args.Add (new Argument (right.CreateExpressionTree (ec)));
3751 if (method != null) {
3752 if (lift_arg)
3753 args.Add (new Argument (new BoolConstant (false, loc)));
3755 args.Add (new Argument (method.CreateExpressionTree (ec)));
3758 return CreateExpressionFactoryCall (ec, method_name, args);
3763 // Represents the operation a + b [+ c [+ d [+ ...]]], where a is a string
3764 // b, c, d... may be strings or objects.
3766 public class StringConcat : Expression {
3767 Arguments arguments;
3769 public StringConcat (Location loc, Expression left, Expression right)
3771 this.loc = loc;
3772 type = TypeManager.string_type;
3773 eclass = ExprClass.Value;
3775 arguments = new Arguments (2);
3776 Append (left);
3777 Append (right);
3780 public override Expression CreateExpressionTree (ResolveContext ec)
3782 Argument arg = arguments [0];
3783 return CreateExpressionAddCall (ec, arg, arg.CreateExpressionTree (ec), 1);
3787 // Creates nested calls tree from an array of arguments used for IL emit
3789 Expression CreateExpressionAddCall (ResolveContext ec, Argument left, Expression left_etree, int pos)
3791 Arguments concat_args = new Arguments (2);
3792 Arguments add_args = new Arguments (3);
3794 concat_args.Add (left);
3795 add_args.Add (new Argument (left_etree));
3797 concat_args.Add (arguments [pos]);
3798 add_args.Add (new Argument (arguments [pos].CreateExpressionTree (ec)));
3800 MethodGroupExpr method = CreateConcatMemberExpression ().Resolve (ec) as MethodGroupExpr;
3801 if (method == null)
3802 return null;
3804 method = method.OverloadResolve (ec, ref concat_args, false, loc);
3805 if (method == null)
3806 return null;
3808 add_args.Add (new Argument (method.CreateExpressionTree (ec)));
3810 Expression expr = CreateExpressionFactoryCall (ec, "Add", add_args);
3811 if (++pos == arguments.Count)
3812 return expr;
3814 left = new Argument (new EmptyExpression (((MethodInfo)method).ReturnType));
3815 return CreateExpressionAddCall (ec, left, expr, pos);
3818 public override Expression DoResolve (ResolveContext ec)
3820 return this;
3823 public void Append (Expression operand)
3826 // Constant folding
3828 StringConstant sc = operand as StringConstant;
3829 if (sc != null) {
3830 if (arguments.Count != 0) {
3831 Argument last_argument = arguments [arguments.Count - 1];
3832 StringConstant last_expr_constant = last_argument.Expr as StringConstant;
3833 if (last_expr_constant != null) {
3834 last_argument.Expr = new StringConstant (
3835 last_expr_constant.Value + sc.Value, sc.Location);
3836 return;
3839 } else {
3841 // Multiple (3+) concatenation are resolved as multiple StringConcat instances
3843 StringConcat concat_oper = operand as StringConcat;
3844 if (concat_oper != null) {
3845 arguments.AddRange (concat_oper.arguments);
3846 return;
3850 arguments.Add (new Argument (operand));
3853 Expression CreateConcatMemberExpression ()
3855 return new MemberAccess (new MemberAccess (new QualifiedAliasMember ("global", "System", loc), "String", loc), "Concat", loc);
3858 public override void Emit (EmitContext ec)
3860 Expression concat = new Invocation (CreateConcatMemberExpression (), arguments, true);
3861 concat = concat.Resolve (new ResolveContext (ec.MemberContext));
3862 if (concat != null)
3863 concat.Emit (ec);
3866 #if NET_4_0
3867 public override SLE.Expression MakeExpression (BuilderContext ctx)
3869 if (arguments.Count != 2)
3870 throw new NotImplementedException ("arguments.Count != 2");
3872 var concat = TypeManager.string_type.GetMethod ("Concat", new[] { typeof (object), typeof (object) });
3873 return SLE.Expression.Add (arguments[0].Expr.MakeExpression (ctx), arguments[1].Expr.MakeExpression (ctx), concat);
3875 #endif
3877 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
3879 arguments.MutateHoistedGenericType (storey);
3884 // User-defined conditional logical operator
3886 public class ConditionalLogicalOperator : UserOperatorCall {
3887 readonly bool is_and;
3888 Expression oper;
3890 public ConditionalLogicalOperator (MethodGroupExpr oper_method, Arguments arguments,
3891 ExpressionTreeExpression expr_tree, bool is_and, Location loc)
3892 : base (oper_method, arguments, expr_tree, loc)
3894 this.is_and = is_and;
3897 public override Expression DoResolve (ResolveContext ec)
3899 MethodInfo method = (MethodInfo)mg;
3900 type = TypeManager.TypeToCoreType (method.ReturnType);
3901 AParametersCollection pd = TypeManager.GetParameterData (method);
3902 if (!TypeManager.IsEqual (type, type) || !TypeManager.IsEqual (type, pd.Types [0]) || !TypeManager.IsEqual (type, pd.Types [1])) {
3903 ec.Report.Error (217, loc,
3904 "A user-defined operator `{0}' must have parameters and return values of the same type in order to be applicable as a short circuit operator",
3905 TypeManager.CSharpSignature (method));
3906 return null;
3909 Expression left_dup = new EmptyExpression (type);
3910 Expression op_true = GetOperatorTrue (ec, left_dup, loc);
3911 Expression op_false = GetOperatorFalse (ec, left_dup, loc);
3912 if (op_true == null || op_false == null) {
3913 ec.Report.Error (218, loc,
3914 "The type `{0}' must have operator `true' and operator `false' defined when `{1}' is used as a short circuit operator",
3915 TypeManager.CSharpName (type), TypeManager.CSharpSignature (method));
3916 return null;
3919 oper = is_and ? op_false : op_true;
3920 eclass = ExprClass.Value;
3921 return this;
3924 public override void Emit (EmitContext ec)
3926 ILGenerator ig = ec.ig;
3927 Label end_target = ig.DefineLabel ();
3930 // Emit and duplicate left argument
3932 arguments [0].Expr.Emit (ec);
3933 ig.Emit (OpCodes.Dup);
3934 arguments.RemoveAt (0);
3936 oper.EmitBranchable (ec, end_target, true);
3937 base.Emit (ec);
3938 ig.MarkLabel (end_target);
3942 public class PointerArithmetic : Expression {
3943 Expression left, right;
3944 Binary.Operator op;
3947 // We assume that `l' is always a pointer
3949 public PointerArithmetic (Binary.Operator op, Expression l, Expression r, Type t, Location loc)
3951 type = t;
3952 this.loc = loc;
3953 left = l;
3954 right = r;
3955 this.op = op;
3958 public override Expression CreateExpressionTree (ResolveContext ec)
3960 Error_PointerInsideExpressionTree (ec);
3961 return null;
3964 public override Expression DoResolve (ResolveContext ec)
3966 eclass = ExprClass.Variable;
3968 if (left.Type == TypeManager.void_ptr_type) {
3969 ec.Report.Error (242, loc, "The operation in question is undefined on void pointers");
3970 return null;
3973 return this;
3976 public override void Emit (EmitContext ec)
3978 Type op_type = left.Type;
3979 ILGenerator ig = ec.ig;
3981 // It must be either array or fixed buffer
3982 Type element;
3983 if (TypeManager.HasElementType (op_type)) {
3984 element = TypeManager.GetElementType (op_type);
3985 } else {
3986 FieldExpr fe = left as FieldExpr;
3987 if (fe != null)
3988 element = AttributeTester.GetFixedBuffer (fe.FieldInfo).ElementType;
3989 else
3990 element = op_type;
3993 int size = GetTypeSize (element);
3994 Type rtype = right.Type;
3996 if ((op & Binary.Operator.SubtractionMask) != 0 && rtype.IsPointer){
3998 // handle (pointer - pointer)
4000 left.Emit (ec);
4001 right.Emit (ec);
4002 ig.Emit (OpCodes.Sub);
4004 if (size != 1){
4005 if (size == 0)
4006 ig.Emit (OpCodes.Sizeof, element);
4007 else
4008 IntLiteral.EmitInt (ig, size);
4009 ig.Emit (OpCodes.Div);
4011 ig.Emit (OpCodes.Conv_I8);
4012 } else {
4014 // handle + and - on (pointer op int)
4016 Constant left_const = left as Constant;
4017 if (left_const != null) {
4019 // Optimize ((T*)null) pointer operations
4021 if (left_const.IsDefaultValue) {
4022 left = EmptyExpression.Null;
4023 } else {
4024 left_const = null;
4028 left.Emit (ec);
4030 Constant right_const = right as Constant;
4031 if (right_const != null) {
4033 // Optimize 0-based arithmetic
4035 if (right_const.IsDefaultValue)
4036 return;
4038 if (size != 0) {
4039 // TODO: Should be the checks resolve context sensitive?
4040 ResolveContext rc = new ResolveContext (ec.MemberContext);
4041 right = ConstantFold.BinaryFold (rc, Binary.Operator.Multiply, new IntConstant (size, right.Location), right_const, loc);
4042 if (right == null)
4043 return;
4044 } else {
4045 ig.Emit (OpCodes.Sizeof, element);
4046 right = EmptyExpression.Null;
4050 right.Emit (ec);
4051 if (rtype == TypeManager.sbyte_type || rtype == TypeManager.byte_type ||
4052 rtype == TypeManager.short_type || rtype == TypeManager.ushort_type) {
4053 ig.Emit (OpCodes.Conv_I);
4054 } else if (rtype == TypeManager.uint32_type) {
4055 ig.Emit (OpCodes.Conv_U);
4058 if (right_const == null && size != 1){
4059 if (size == 0)
4060 ig.Emit (OpCodes.Sizeof, element);
4061 else
4062 IntLiteral.EmitInt (ig, size);
4063 if (rtype == TypeManager.int64_type || rtype == TypeManager.uint64_type)
4064 ig.Emit (OpCodes.Conv_I8);
4066 Binary.EmitOperatorOpcode (ec, Binary.Operator.Multiply, rtype);
4069 if (left_const == null) {
4070 if (rtype == TypeManager.int64_type)
4071 ig.Emit (OpCodes.Conv_I);
4072 else if (rtype == TypeManager.uint64_type)
4073 ig.Emit (OpCodes.Conv_U);
4075 Binary.EmitOperatorOpcode (ec, op, op_type);
4081 /// <summary>
4082 /// Implements the ternary conditional operator (?:)
4083 /// </summary>
4084 public class Conditional : Expression {
4085 Expression expr, true_expr, false_expr;
4087 public Conditional (Expression expr, Expression true_expr, Expression false_expr)
4089 this.expr = expr;
4090 this.true_expr = true_expr;
4091 this.false_expr = false_expr;
4092 this.loc = expr.Location;
4095 public Expression Expr {
4096 get {
4097 return expr;
4101 public Expression TrueExpr {
4102 get {
4103 return true_expr;
4107 public Expression FalseExpr {
4108 get {
4109 return false_expr;
4113 public override Expression CreateExpressionTree (ResolveContext ec)
4115 Arguments args = new Arguments (3);
4116 args.Add (new Argument (expr.CreateExpressionTree (ec)));
4117 args.Add (new Argument (true_expr.CreateExpressionTree (ec)));
4118 args.Add (new Argument (false_expr.CreateExpressionTree (ec)));
4119 return CreateExpressionFactoryCall (ec, "Condition", args);
4122 public override Expression DoResolve (ResolveContext ec)
4124 expr = Expression.ResolveBoolean (ec, expr, loc);
4126 Assign ass = expr as Assign;
4127 if (ass != null && ass.Source is Constant) {
4128 ec.Report.Warning (665, 3, loc, "Assignment in conditional expression is always constant; did you mean to use == instead of = ?");
4131 true_expr = true_expr.Resolve (ec);
4132 false_expr = false_expr.Resolve (ec);
4134 if (true_expr == null || false_expr == null || expr == null)
4135 return null;
4137 eclass = ExprClass.Value;
4138 Type true_type = true_expr.Type;
4139 Type false_type = false_expr.Type;
4140 type = true_type;
4143 // First, if an implicit conversion exists from true_expr
4144 // to false_expr, then the result type is of type false_expr.Type
4146 if (!TypeManager.IsEqual (true_type, false_type)) {
4147 Expression conv = Convert.ImplicitConversion (ec, true_expr, false_type, loc);
4148 if (conv != null) {
4150 // Check if both can convert implicitl to each other's type
4152 if (Convert.ImplicitConversion (ec, false_expr, true_type, loc) != null) {
4153 ec.Report.Error (172, loc,
4154 "Can not compute type of conditional expression " +
4155 "as `" + TypeManager.CSharpName (true_expr.Type) +
4156 "' and `" + TypeManager.CSharpName (false_expr.Type) +
4157 "' convert implicitly to each other");
4158 return null;
4160 type = false_type;
4161 true_expr = conv;
4162 } else if ((conv = Convert.ImplicitConversion (ec, false_expr, true_type, loc)) != null) {
4163 false_expr = conv;
4164 } else {
4165 ec.Report.Error (173, loc,
4166 "Type of conditional expression cannot be determined because there is no implicit conversion between `{0}' and `{1}'",
4167 true_expr.GetSignatureForError (), false_expr.GetSignatureForError ());
4168 return null;
4172 // Dead code optimalization
4173 Constant c = expr as Constant;
4174 if (c != null){
4175 bool is_false = c.IsDefaultValue;
4176 ec.Report.Warning (429, 4, is_false ? true_expr.Location : false_expr.Location, "Unreachable expression code detected");
4177 return ReducedExpression.Create (is_false ? false_expr : true_expr, this).Resolve (ec);
4180 return this;
4183 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
4185 expr.MutateHoistedGenericType (storey);
4186 true_expr.MutateHoistedGenericType (storey);
4187 false_expr.MutateHoistedGenericType (storey);
4188 type = storey.MutateType (type);
4191 public override TypeExpr ResolveAsTypeTerminal (IMemberContext ec, bool silent)
4193 return null;
4196 public override void Emit (EmitContext ec)
4198 ILGenerator ig = ec.ig;
4199 Label false_target = ig.DefineLabel ();
4200 Label end_target = ig.DefineLabel ();
4202 expr.EmitBranchable (ec, false_target, false);
4203 true_expr.Emit (ec);
4205 if (type.IsInterface) {
4206 LocalBuilder temp = ec.GetTemporaryLocal (type);
4207 ig.Emit (OpCodes.Stloc, temp);
4208 ig.Emit (OpCodes.Ldloc, temp);
4209 ec.FreeTemporaryLocal (temp, type);
4212 ig.Emit (OpCodes.Br, end_target);
4213 ig.MarkLabel (false_target);
4214 false_expr.Emit (ec);
4215 ig.MarkLabel (end_target);
4218 protected override void CloneTo (CloneContext clonectx, Expression t)
4220 Conditional target = (Conditional) t;
4222 target.expr = expr.Clone (clonectx);
4223 target.true_expr = true_expr.Clone (clonectx);
4224 target.false_expr = false_expr.Clone (clonectx);
4228 public abstract class VariableReference : Expression, IAssignMethod, IMemoryLocation, IVariableReference {
4229 LocalTemporary temp;
4231 #region Abstract
4232 public abstract HoistedVariable GetHoistedVariable (AnonymousExpression ae);
4233 public abstract bool IsFixed { get; }
4234 public abstract bool IsRef { get; }
4235 public abstract string Name { get; }
4236 public abstract void SetHasAddressTaken ();
4239 // Variable IL data, it has to be protected to encapsulate hoisted variables
4241 protected abstract ILocalVariable Variable { get; }
4244 // Variable flow-analysis data
4246 public abstract VariableInfo VariableInfo { get; }
4247 #endregion
4249 public void AddressOf (EmitContext ec, AddressOp mode)
4251 HoistedVariable hv = GetHoistedVariable (ec);
4252 if (hv != null) {
4253 hv.AddressOf (ec, mode);
4254 return;
4257 Variable.EmitAddressOf (ec);
4260 public HoistedVariable GetHoistedVariable (ResolveContext rc)
4262 return GetHoistedVariable (rc.CurrentAnonymousMethod);
4265 public HoistedVariable GetHoistedVariable (EmitContext ec)
4267 return GetHoistedVariable (ec.CurrentAnonymousMethod);
4270 public override void Emit (EmitContext ec)
4272 Emit (ec, false);
4275 public override void EmitSideEffect (EmitContext ec)
4277 // do nothing
4281 // This method is used by parameters that are references, that are
4282 // being passed as references: we only want to pass the pointer (that
4283 // is already stored in the parameter, not the address of the pointer,
4284 // and not the value of the variable).
4286 public void EmitLoad (EmitContext ec)
4288 Variable.Emit (ec);
4291 public void Emit (EmitContext ec, bool leave_copy)
4293 Report.Debug (64, "VARIABLE EMIT", this, Variable, type, IsRef, loc);
4295 HoistedVariable hv = GetHoistedVariable (ec);
4296 if (hv != null) {
4297 hv.Emit (ec, leave_copy);
4298 return;
4301 EmitLoad (ec);
4303 if (IsRef) {
4305 // If we are a reference, we loaded on the stack a pointer
4306 // Now lets load the real value
4308 LoadFromPtr (ec.ig, type);
4311 if (leave_copy) {
4312 ec.ig.Emit (OpCodes.Dup);
4314 if (IsRef) {
4315 temp = new LocalTemporary (Type);
4316 temp.Store (ec);
4321 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy,
4322 bool prepare_for_load)
4324 HoistedVariable hv = GetHoistedVariable (ec);
4325 if (hv != null) {
4326 hv.EmitAssign (ec, source, leave_copy, prepare_for_load);
4327 return;
4330 New n_source = source as New;
4331 if (n_source != null) {
4332 if (!n_source.Emit (ec, this)) {
4333 if (leave_copy)
4334 EmitLoad (ec);
4335 return;
4337 } else {
4338 if (IsRef)
4339 EmitLoad (ec);
4341 source.Emit (ec);
4344 if (leave_copy) {
4345 ec.ig.Emit (OpCodes.Dup);
4346 if (IsRef) {
4347 temp = new LocalTemporary (Type);
4348 temp.Store (ec);
4352 if (IsRef)
4353 StoreFromPtr (ec.ig, type);
4354 else
4355 Variable.EmitAssign (ec);
4357 if (temp != null) {
4358 temp.Emit (ec);
4359 temp.Release (ec);
4363 public bool IsHoisted {
4364 get { return GetHoistedVariable ((AnonymousExpression) null) != null; }
4367 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
4369 type = storey.MutateType (type);
4373 /// <summary>
4374 /// Local variables
4375 /// </summary>
4376 public class LocalVariableReference : VariableReference {
4377 readonly string name;
4378 public Block Block;
4379 public LocalInfo local_info;
4380 bool is_readonly;
4381 bool resolved; // TODO: merge with eclass
4383 public LocalVariableReference (Block block, string name, Location l)
4385 Block = block;
4386 this.name = name;
4387 loc = l;
4391 // Setting `is_readonly' to false will allow you to create a writable
4392 // reference to a read-only variable. This is used by foreach and using.
4394 public LocalVariableReference (Block block, string name, Location l,
4395 LocalInfo local_info, bool is_readonly)
4396 : this (block, name, l)
4398 this.local_info = local_info;
4399 this.is_readonly = is_readonly;
4402 public override VariableInfo VariableInfo {
4403 get { return local_info.VariableInfo; }
4406 public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
4408 return local_info.HoistedVariableReference;
4412 // A local variable is always fixed
4414 public override bool IsFixed {
4415 get { return true; }
4418 public override bool IsRef {
4419 get { return false; }
4422 public bool IsReadOnly {
4423 get { return is_readonly; }
4426 public override string Name {
4427 get { return name; }
4430 public bool VerifyAssigned (ResolveContext ec)
4432 VariableInfo variable_info = local_info.VariableInfo;
4433 return variable_info == null || variable_info.IsAssigned (ec, loc);
4436 void ResolveLocalInfo ()
4438 if (local_info == null) {
4439 local_info = Block.GetLocalInfo (Name);
4440 type = local_info.VariableType;
4441 is_readonly = local_info.ReadOnly;
4445 public override void SetHasAddressTaken ()
4447 local_info.AddressTaken = true;
4450 public override Expression CreateExpressionTree (ResolveContext ec)
4452 HoistedVariable hv = GetHoistedVariable (ec);
4453 if (hv != null)
4454 return hv.CreateExpressionTree (ec);
4456 Arguments arg = new Arguments (1);
4457 arg.Add (new Argument (this));
4458 return CreateExpressionFactoryCall (ec, "Constant", arg);
4461 Expression DoResolveBase (ResolveContext ec)
4463 type = local_info.VariableType;
4465 Expression e = Block.GetConstantExpression (Name);
4466 if (e != null)
4467 return e.Resolve (ec);
4469 VerifyAssigned (ec);
4472 // If we are referencing a variable from the external block
4473 // flag it for capturing
4475 if (ec.MustCaptureVariable (local_info)) {
4476 if (local_info.AddressTaken)
4477 AnonymousMethodExpression.Error_AddressOfCapturedVar (ec, this, loc);
4479 if (ec.IsVariableCapturingRequired) {
4480 AnonymousMethodStorey storey = local_info.Block.Explicit.CreateAnonymousMethodStorey (ec);
4481 storey.CaptureLocalVariable (ec, local_info);
4485 resolved |= ec.DoFlowAnalysis;
4486 eclass = ExprClass.Variable;
4487 return this;
4490 public override Expression DoResolve (ResolveContext ec)
4492 if (resolved)
4493 return this;
4495 ResolveLocalInfo ();
4496 local_info.Used = true;
4498 if (type == null && local_info.Type is VarExpr) {
4499 local_info.VariableType = TypeManager.object_type;
4500 Error_VariableIsUsedBeforeItIsDeclared (ec.Report, Name);
4501 return null;
4504 return DoResolveBase (ec);
4507 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
4509 ResolveLocalInfo ();
4511 // is out param
4512 if (right_side == EmptyExpression.OutAccess)
4513 local_info.Used = true;
4515 // Infer implicitly typed local variable
4516 if (type == null) {
4517 VarExpr ve = local_info.Type as VarExpr;
4518 if (ve != null) {
4519 if (!ve.InferType (ec, right_side))
4520 return null;
4521 type = local_info.VariableType = ve.Type;
4525 if (is_readonly) {
4526 int code;
4527 string msg;
4528 if (right_side == EmptyExpression.OutAccess) {
4529 code = 1657; msg = "Cannot pass `{0}' as a ref or out argument because it is a `{1}'";
4530 } else if (right_side == EmptyExpression.LValueMemberAccess) {
4531 code = 1654; msg = "Cannot assign to members of `{0}' because it is a `{1}'";
4532 } else if (right_side == EmptyExpression.LValueMemberOutAccess) {
4533 code = 1655; msg = "Cannot pass members of `{0}' as ref or out arguments because it is a `{1}'";
4534 } else if (right_side == EmptyExpression.UnaryAddress) {
4535 code = 459; msg = "Cannot take the address of {1} `{0}'";
4536 } else {
4537 code = 1656; msg = "Cannot assign to `{0}' because it is a `{1}'";
4539 ec.Report.Error (code, loc, msg, Name, local_info.GetReadOnlyContext ());
4540 } else if (VariableInfo != null) {
4541 VariableInfo.SetAssigned (ec);
4544 return DoResolveBase (ec);
4547 public override int GetHashCode ()
4549 return Name.GetHashCode ();
4552 public override bool Equals (object obj)
4554 LocalVariableReference lvr = obj as LocalVariableReference;
4555 if (lvr == null)
4556 return false;
4558 return Name == lvr.Name && Block == lvr.Block;
4561 protected override ILocalVariable Variable {
4562 get { return local_info; }
4565 public override string ToString ()
4567 return String.Format ("{0} ({1}:{2})", GetType (), Name, loc);
4570 protected override void CloneTo (CloneContext clonectx, Expression t)
4572 LocalVariableReference target = (LocalVariableReference) t;
4574 target.Block = clonectx.LookupBlock (Block);
4575 if (local_info != null)
4576 target.local_info = clonectx.LookupVariable (local_info);
4580 /// <summary>
4581 /// This represents a reference to a parameter in the intermediate
4582 /// representation.
4583 /// </summary>
4584 public class ParameterReference : VariableReference {
4585 readonly ToplevelParameterInfo pi;
4587 public ParameterReference (ToplevelParameterInfo pi, Location loc)
4589 this.pi = pi;
4590 this.loc = loc;
4593 public override bool IsRef {
4594 get { return (pi.Parameter.ModFlags & Parameter.Modifier.ISBYREF) != 0; }
4597 bool HasOutModifier {
4598 get { return pi.Parameter.ModFlags == Parameter.Modifier.OUT; }
4601 public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
4603 return pi.Parameter.HoistedVariableReference;
4607 // A ref or out parameter is classified as a moveable variable, even
4608 // if the argument given for the parameter is a fixed variable
4610 public override bool IsFixed {
4611 get { return !IsRef; }
4614 public override string Name {
4615 get { return Parameter.Name; }
4618 public Parameter Parameter {
4619 get { return pi.Parameter; }
4622 public override VariableInfo VariableInfo {
4623 get { return pi.VariableInfo; }
4626 protected override ILocalVariable Variable {
4627 get { return Parameter; }
4630 public bool IsAssigned (ResolveContext ec, Location loc)
4632 // HACK: Variables are not captured in probing mode
4633 if (ec.IsInProbingMode)
4634 return true;
4636 if (!ec.DoFlowAnalysis || !HasOutModifier || ec.CurrentBranching.IsAssigned (VariableInfo))
4637 return true;
4639 ec.Report.Error (269, loc, "Use of unassigned out parameter `{0}'", Name);
4640 return false;
4643 public override void SetHasAddressTaken ()
4645 Parameter.HasAddressTaken = true;
4648 void SetAssigned (ResolveContext ec)
4650 if (HasOutModifier && ec.DoFlowAnalysis)
4651 ec.CurrentBranching.SetAssigned (VariableInfo);
4654 bool DoResolveBase (ResolveContext ec)
4656 type = pi.ParameterType;
4657 eclass = ExprClass.Variable;
4659 AnonymousExpression am = ec.CurrentAnonymousMethod;
4660 if (am == null)
4661 return true;
4663 Block b = ec.CurrentBlock;
4664 while (b != null) {
4665 IParameterData[] p = b.Toplevel.Parameters.FixedParameters;
4666 for (int i = 0; i < p.Length; ++i) {
4667 if (p [i] != Parameter)
4668 continue;
4671 // Skip closest anonymous method parameters
4673 if (b == ec.CurrentBlock && !am.IsIterator)
4674 return true;
4676 if (IsRef) {
4677 ec.Report.Error (1628, loc,
4678 "Parameter `{0}' cannot be used inside `{1}' when using `ref' or `out' modifier",
4679 Name, am.ContainerType);
4682 b = null;
4683 break;
4686 if (b != null)
4687 b = b.Toplevel.Parent;
4690 if (pi.Parameter.HasAddressTaken)
4691 AnonymousMethodExpression.Error_AddressOfCapturedVar (ec, this, loc);
4693 if (ec.IsVariableCapturingRequired) {
4694 AnonymousMethodStorey storey = pi.Block.CreateAnonymousMethodStorey (ec);
4695 storey.CaptureParameter (ec, this);
4698 return true;
4701 public override int GetHashCode ()
4703 return Name.GetHashCode ();
4706 public override bool Equals (object obj)
4708 ParameterReference pr = obj as ParameterReference;
4709 if (pr == null)
4710 return false;
4712 return Name == pr.Name;
4715 protected override void CloneTo (CloneContext clonectx, Expression target)
4717 // Nothing to clone
4720 public override Expression CreateExpressionTree (ResolveContext ec)
4722 HoistedVariable hv = GetHoistedVariable (ec);
4723 if (hv != null)
4724 return hv.CreateExpressionTree (ec);
4726 return Parameter.ExpressionTreeVariableReference ();
4730 // Notice that for ref/out parameters, the type exposed is not the
4731 // same type exposed externally.
4733 // for "ref int a":
4734 // externally we expose "int&"
4735 // here we expose "int".
4737 // We record this in "is_ref". This means that the type system can treat
4738 // the type as it is expected, but when we generate the code, we generate
4739 // the alternate kind of code.
4741 public override Expression DoResolve (ResolveContext ec)
4743 if (!DoResolveBase (ec))
4744 return null;
4746 // HACK: Variables are not captured in probing mode
4747 if (ec.IsInProbingMode)
4748 return this;
4750 if (HasOutModifier && ec.DoFlowAnalysis &&
4751 (!ec.OmitStructFlowAnalysis || !VariableInfo.TypeInfo.IsStruct) && !IsAssigned (ec, loc))
4752 return null;
4754 return this;
4757 override public Expression DoResolveLValue (ResolveContext ec, Expression right_side)
4759 if (!DoResolveBase (ec))
4760 return null;
4762 // HACK: parameters are not captured when probing is on
4763 if (!ec.IsInProbingMode)
4764 SetAssigned (ec);
4766 return this;
4769 static public void EmitLdArg (ILGenerator ig, int x)
4771 switch (x) {
4772 case 0: ig.Emit (OpCodes.Ldarg_0); break;
4773 case 1: ig.Emit (OpCodes.Ldarg_1); break;
4774 case 2: ig.Emit (OpCodes.Ldarg_2); break;
4775 case 3: ig.Emit (OpCodes.Ldarg_3); break;
4776 default:
4777 if (x > byte.MaxValue)
4778 ig.Emit (OpCodes.Ldarg, x);
4779 else
4780 ig.Emit (OpCodes.Ldarg_S, (byte) x);
4781 break;
4786 /// <summary>
4787 /// Invocation of methods or delegates.
4788 /// </summary>
4789 public class Invocation : ExpressionStatement
4791 protected Arguments arguments;
4792 protected Expression expr;
4793 protected MethodGroupExpr mg;
4794 bool arguments_resolved;
4797 // arguments is an ArrayList, but we do not want to typecast,
4798 // as it might be null.
4800 public Invocation (Expression expr, Arguments arguments)
4802 SimpleName sn = expr as SimpleName;
4803 if (sn != null)
4804 this.expr = sn.GetMethodGroup ();
4805 else
4806 this.expr = expr;
4808 this.arguments = arguments;
4809 if (expr != null)
4810 loc = expr.Location;
4813 public Invocation (Expression expr, Arguments arguments, bool arguments_resolved)
4814 : this (expr, arguments)
4816 this.arguments_resolved = arguments_resolved;
4819 public override Expression CreateExpressionTree (ResolveContext ec)
4821 Arguments args;
4824 // Special conversion for nested expression trees
4826 if (TypeManager.DropGenericTypeArguments (type) == TypeManager.expression_type) {
4827 args = new Arguments (1);
4828 args.Add (new Argument (this));
4829 return CreateExpressionFactoryCall (ec, "Quote", args);
4832 Expression instance = mg.IsInstance ?
4833 mg.InstanceExpression.CreateExpressionTree (ec) :
4834 new NullLiteral (loc);
4836 args = Arguments.CreateForExpressionTree (ec, arguments,
4837 instance,
4838 mg.CreateExpressionTree (ec));
4840 if (mg.IsBase)
4841 MemberExpr.Error_BaseAccessInExpressionTree (ec, loc);
4843 return CreateExpressionFactoryCall (ec, "Call", args);
4846 public override Expression DoResolve (ResolveContext ec)
4848 // Don't resolve already resolved expression
4849 if (eclass != ExprClass.Invalid)
4850 return this;
4852 Expression expr_resolved = expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
4853 if (expr_resolved == null)
4854 return null;
4857 // Next, evaluate all the expressions in the argument list
4859 bool dynamic_arg = false;
4860 if (arguments != null && !arguments_resolved)
4861 arguments.Resolve (ec, out dynamic_arg);
4863 Type expr_type = expr_resolved.Type;
4864 mg = expr_resolved as MethodGroupExpr;
4866 if (dynamic_arg || TypeManager.IsDynamicType (expr_type)) {
4867 Arguments args;
4868 DynamicMemberBinder dmb = expr_resolved as DynamicMemberBinder;
4869 if (dmb != null) {
4870 args = dmb.Arguments;
4871 if (arguments != null)
4872 args.AddRange (arguments);
4873 } else if (mg == null) {
4874 if (arguments == null)
4875 args = new Arguments (1);
4876 else
4877 args = arguments;
4879 args.Insert (0, new Argument (expr_resolved));
4880 expr = null;
4881 } else {
4882 if (mg.IsBase) {
4883 ec.Report.Error (1971, loc,
4884 "The base call to method `{0}' cannot be dynamically dispatched. Consider casting the dynamic arguments or eliminating the base access",
4885 mg.Name);
4886 return null;
4889 args = arguments;
4891 if (mg.IsStatic != mg.IsInstance) {
4892 if (args == null)
4893 args = new Arguments (1);
4895 if (mg.IsStatic) {
4896 args.Insert (0, new Argument (new TypeOf (new TypeExpression (mg.DeclaringType, loc), loc).Resolve (ec), Argument.AType.DynamicStatic));
4897 } else {
4898 MemberAccess ma = expr as MemberAccess;
4899 if (ma != null)
4900 args.Insert (0, new Argument (ma.Left.Resolve (ec)));
4901 else
4902 args.Insert (0, new Argument (new This (loc).Resolve (ec)));
4907 return new DynamicInvocation (expr as ATypeNameExpression, args, loc).Resolve (ec);
4910 if (mg == null) {
4911 if (expr_type != null && TypeManager.IsDelegateType (expr_type)){
4912 return (new DelegateInvocation (
4913 expr_resolved, arguments, loc)).Resolve (ec);
4916 MemberExpr me = expr_resolved as MemberExpr;
4917 if (me == null) {
4918 expr_resolved.Error_UnexpectedKind (ec, ResolveFlags.MethodGroup, loc);
4919 return null;
4922 mg = ec.LookupExtensionMethod (me.Type, me.Name, loc);
4923 if (mg == null) {
4924 ec.Report.Error (1955, loc, "The member `{0}' cannot be used as method or delegate",
4925 expr_resolved.GetSignatureForError ());
4926 return null;
4929 ((ExtensionMethodGroupExpr)mg).ExtensionExpression = me.InstanceExpression;
4932 mg = DoResolveOverload (ec);
4933 if (mg == null)
4934 return null;
4936 MethodInfo method = (MethodInfo)mg;
4937 if (method != null) {
4938 type = TypeManager.TypeToCoreType (method.ReturnType);
4940 // TODO: this is a copy of mg.ResolveMemberAccess method
4941 Expression iexpr = mg.InstanceExpression;
4942 if (method.IsStatic) {
4943 if (iexpr == null ||
4944 iexpr is This || iexpr is EmptyExpression ||
4945 mg.IdenticalTypeName) {
4946 mg.InstanceExpression = null;
4947 } else {
4948 MemberExpr.error176 (ec, loc, mg.GetSignatureForError ());
4949 return null;
4951 } else {
4952 if (iexpr == null || iexpr == EmptyExpression.Null) {
4953 SimpleName.Error_ObjectRefRequired (ec, loc, mg.GetSignatureForError ());
4958 if (type.IsPointer){
4959 if (!ec.IsUnsafe){
4960 UnsafeError (ec, loc);
4961 return null;
4966 // Only base will allow this invocation to happen.
4968 if (mg.IsBase && method.IsAbstract){
4969 Error_CannotCallAbstractBase (ec, TypeManager.CSharpSignature (method));
4970 return null;
4973 if (arguments == null && method.DeclaringType == TypeManager.object_type && method.Name == Destructor.MetadataName) {
4974 if (mg.IsBase)
4975 ec.Report.Error (250, loc, "Do not directly call your base class Finalize method. It is called automatically from your destructor");
4976 else
4977 ec.Report.Error (245, loc, "Destructors and object.Finalize cannot be called directly. Consider calling IDisposable.Dispose if available");
4978 return null;
4981 IsSpecialMethodInvocation (ec, method, loc);
4983 if (mg.InstanceExpression != null)
4984 mg.InstanceExpression.CheckMarshalByRefAccess (ec);
4986 eclass = ExprClass.Value;
4987 return this;
4990 protected virtual MethodGroupExpr DoResolveOverload (ResolveContext ec)
4992 return mg.OverloadResolve (ec, ref arguments, false, loc);
4995 public static bool IsSpecialMethodInvocation (ResolveContext ec, MethodBase method, Location loc)
4997 if (!TypeManager.IsSpecialMethod (method))
4998 return false;
5000 ec.Report.SymbolRelatedToPreviousError (method);
5001 ec.Report.Error (571, loc, "`{0}': cannot explicitly call operator or accessor",
5002 TypeManager.CSharpSignature (method, true));
5004 return true;
5007 static Type[] GetVarargsTypes (MethodBase mb, Arguments arguments)
5009 AParametersCollection pd = TypeManager.GetParameterData (mb);
5011 Argument a = arguments [pd.Count - 1];
5012 Arglist list = (Arglist) a.Expr;
5014 return list.ArgumentTypes;
5017 /// <summary>
5018 /// This checks the ConditionalAttribute on the method
5019 /// </summary>
5020 public static bool IsMethodExcluded (MethodBase method, Location loc)
5022 if (method.IsConstructor)
5023 return false;
5025 method = TypeManager.DropGenericMethodArguments (method);
5026 if (method.DeclaringType.Module == RootContext.ToplevelTypes.Builder) {
5027 IMethodData md = TypeManager.GetMethod (method);
5028 if (md != null)
5029 return md.IsExcluded ();
5031 // For some methods (generated by delegate class) GetMethod returns null
5032 // because they are not included in builder_to_method table
5033 return false;
5036 return AttributeTester.IsConditionalMethodExcluded (method, loc);
5039 /// <remarks>
5040 /// is_base tells whether we want to force the use of the `call'
5041 /// opcode instead of using callvirt. Call is required to call
5042 /// a specific method, while callvirt will always use the most
5043 /// recent method in the vtable.
5045 /// is_static tells whether this is an invocation on a static method
5047 /// instance_expr is an expression that represents the instance
5048 /// it must be non-null if is_static is false.
5050 /// method is the method to invoke.
5052 /// Arguments is the list of arguments to pass to the method or constructor.
5053 /// </remarks>
5054 public static void EmitCall (EmitContext ec, bool is_base,
5055 Expression instance_expr,
5056 MethodBase method, Arguments Arguments, Location loc)
5058 EmitCall (ec, is_base, instance_expr, method, Arguments, loc, false, false);
5061 // `dup_args' leaves an extra copy of the arguments on the stack
5062 // `omit_args' does not leave any arguments at all.
5063 // So, basically, you could make one call with `dup_args' set to true,
5064 // and then another with `omit_args' set to true, and the two calls
5065 // would have the same set of arguments. However, each argument would
5066 // only have been evaluated once.
5067 public static void EmitCall (EmitContext ec, bool is_base,
5068 Expression instance_expr,
5069 MethodBase method, Arguments Arguments, Location loc,
5070 bool dup_args, bool omit_args)
5072 ILGenerator ig = ec.ig;
5073 bool struct_call = false;
5074 bool this_call = false;
5075 LocalTemporary this_arg = null;
5077 Type decl_type = method.DeclaringType;
5079 if (IsMethodExcluded (method, loc))
5080 return;
5082 bool is_static = method.IsStatic;
5083 if (!is_static){
5084 this_call = instance_expr is This;
5085 if (TypeManager.IsStruct (decl_type) || TypeManager.IsEnumType (decl_type))
5086 struct_call = true;
5089 // If this is ourselves, push "this"
5091 if (!omit_args) {
5092 Type t = null;
5093 Type iexpr_type = instance_expr.Type;
5096 // Push the instance expression
5098 if (TypeManager.IsValueType (iexpr_type) || TypeManager.IsGenericParameter (iexpr_type)) {
5100 // Special case: calls to a function declared in a
5101 // reference-type with a value-type argument need
5102 // to have their value boxed.
5103 if (TypeManager.IsStruct (decl_type) ||
5104 TypeManager.IsGenericParameter (iexpr_type)) {
5106 // If the expression implements IMemoryLocation, then
5107 // we can optimize and use AddressOf on the
5108 // return.
5110 // If not we have to use some temporary storage for
5111 // it.
5112 if (instance_expr is IMemoryLocation) {
5113 ((IMemoryLocation)instance_expr).
5114 AddressOf (ec, AddressOp.LoadStore);
5115 } else {
5116 LocalTemporary temp = new LocalTemporary (iexpr_type);
5117 instance_expr.Emit (ec);
5118 temp.Store (ec);
5119 temp.AddressOf (ec, AddressOp.Load);
5122 // avoid the overhead of doing this all the time.
5123 if (dup_args)
5124 t = TypeManager.GetReferenceType (iexpr_type);
5125 } else {
5126 instance_expr.Emit (ec);
5128 // FIXME: should use instance_expr is IMemoryLocation + constraint.
5129 // to help JIT to produce better code
5130 ig.Emit (OpCodes.Box, instance_expr.Type);
5131 t = TypeManager.object_type;
5133 } else {
5134 instance_expr.Emit (ec);
5135 t = instance_expr.Type;
5138 if (dup_args) {
5139 ig.Emit (OpCodes.Dup);
5140 if (Arguments != null && Arguments.Count != 0) {
5141 this_arg = new LocalTemporary (t);
5142 this_arg.Store (ec);
5148 if (!omit_args && Arguments != null)
5149 Arguments.Emit (ec, dup_args, this_arg);
5151 OpCode call_op;
5152 if (is_static || struct_call || is_base || (this_call && !method.IsVirtual)) {
5153 call_op = OpCodes.Call;
5154 } else {
5155 call_op = OpCodes.Callvirt;
5157 #if GMCS_SOURCE
5158 if ((instance_expr != null) && (instance_expr.Type.IsGenericParameter))
5159 ig.Emit (OpCodes.Constrained, instance_expr.Type);
5160 #endif
5163 if ((method.CallingConvention & CallingConventions.VarArgs) != 0) {
5164 Type[] varargs_types = GetVarargsTypes (method, Arguments);
5165 ig.EmitCall (call_op, (MethodInfo) method, varargs_types);
5166 return;
5170 // If you have:
5171 // this.DoFoo ();
5172 // and DoFoo is not virtual, you can omit the callvirt,
5173 // because you don't need the null checking behavior.
5175 if (method is MethodInfo)
5176 ig.Emit (call_op, (MethodInfo) method);
5177 else
5178 ig.Emit (call_op, (ConstructorInfo) method);
5181 public override void Emit (EmitContext ec)
5183 mg.EmitCall (ec, arguments);
5186 public override void EmitStatement (EmitContext ec)
5188 Emit (ec);
5191 // Pop the return value if there is one
5193 if (TypeManager.TypeToCoreType (type) != TypeManager.void_type)
5194 ec.ig.Emit (OpCodes.Pop);
5197 protected override void CloneTo (CloneContext clonectx, Expression t)
5199 Invocation target = (Invocation) t;
5201 if (arguments != null)
5202 target.arguments = arguments.Clone (clonectx);
5204 target.expr = expr.Clone (clonectx);
5207 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
5209 mg.MutateHoistedGenericType (storey);
5210 type = storey.MutateType (type);
5211 if (arguments != null) {
5212 arguments.MutateHoistedGenericType (storey);
5218 // It's either a cast or delegate invocation
5220 public class InvocationOrCast : ExpressionStatement
5222 Expression expr;
5223 Expression argument;
5225 public InvocationOrCast (Expression expr, Expression argument)
5227 this.expr = expr;
5228 this.argument = argument;
5229 this.loc = expr.Location;
5232 public override Expression CreateExpressionTree (ResolveContext ec)
5234 throw new NotSupportedException ("ET");
5237 public override Expression DoResolve (ResolveContext ec)
5239 Expression e = ResolveCore (ec);
5240 if (e == null)
5241 return null;
5243 return e.Resolve (ec);
5246 Expression ResolveCore (EmitContext ec)
5249 // First try to resolve it as a cast.
5251 TypeExpr te = expr.ResolveAsBaseTerminal (ec, true);
5252 if (te != null) {
5253 return new Cast (te, argument, loc);
5257 // This can either be a type or a delegate invocation.
5258 // Let's just resolve it and see what we'll get.
5260 expr = expr.Resolve (ec, ResolveFlags.Type | ResolveFlags.VariableOrValue);
5261 if (expr == null)
5262 return null;
5265 // Ok, so it's a Cast.
5267 if (expr.eclass == ExprClass.Type || expr.eclass == ExprClass.TypeParameter) {
5268 return new Cast (expr, argument, loc);
5271 if (expr.eclass == ExprClass.Namespace) {
5272 expr.Error_UnexpectedKind (null, "type", loc);
5273 return null;
5277 // It's a delegate invocation.
5279 if (!TypeManager.IsDelegateType (expr.Type)) {
5280 Error (149, "Method name expected");
5281 return null;
5284 ArrayList args = new ArrayList (1);
5285 args.Add (new Argument (argument, Argument.AType.Expression));
5286 return new DelegateInvocation (expr, args, loc);
5289 public override ExpressionStatement ResolveStatement (EmitContext ec)
5291 Expression e = ResolveCore (ec);
5292 if (e == null)
5293 return null;
5295 ExpressionStatement s = e as ExpressionStatement;
5296 if (s == null) {
5297 Error_InvalidExpressionStatement ();
5298 return null;
5301 return s.ResolveStatement (ec);
5304 public override void Emit (EmitContext ec)
5306 throw new Exception ("Cannot happen");
5309 public override void EmitStatement (EmitContext ec)
5311 throw new Exception ("Cannot happen");
5314 protected override void CloneTo (CloneContext clonectx, Expression t)
5316 InvocationOrCast target = (InvocationOrCast) t;
5318 target.expr = expr.Clone (clonectx);
5319 target.argument = argument.Clone (clonectx);
5324 /// <summary>
5325 /// Implements the new expression
5326 /// </summary>
5327 public class New : ExpressionStatement, IMemoryLocation {
5328 protected Arguments Arguments;
5331 // During bootstrap, it contains the RequestedType,
5332 // but if `type' is not null, it *might* contain a NewDelegate
5333 // (because of field multi-initialization)
5335 protected Expression RequestedType;
5337 protected MethodGroupExpr method;
5339 bool is_type_parameter;
5341 public New (Expression requested_type, Arguments arguments, Location l)
5343 RequestedType = requested_type;
5344 Arguments = arguments;
5345 loc = l;
5348 /// <summary>
5349 /// Converts complex core type syntax like 'new int ()' to simple constant
5350 /// </summary>
5351 public static Constant Constantify (Type t)
5353 if (t == TypeManager.int32_type)
5354 return new IntConstant (0, Location.Null);
5355 if (t == TypeManager.uint32_type)
5356 return new UIntConstant (0, Location.Null);
5357 if (t == TypeManager.int64_type)
5358 return new LongConstant (0, Location.Null);
5359 if (t == TypeManager.uint64_type)
5360 return new ULongConstant (0, Location.Null);
5361 if (t == TypeManager.float_type)
5362 return new FloatConstant (0, Location.Null);
5363 if (t == TypeManager.double_type)
5364 return new DoubleConstant (0, Location.Null);
5365 if (t == TypeManager.short_type)
5366 return new ShortConstant (0, Location.Null);
5367 if (t == TypeManager.ushort_type)
5368 return new UShortConstant (0, Location.Null);
5369 if (t == TypeManager.sbyte_type)
5370 return new SByteConstant (0, Location.Null);
5371 if (t == TypeManager.byte_type)
5372 return new ByteConstant (0, Location.Null);
5373 if (t == TypeManager.char_type)
5374 return new CharConstant ('\0', Location.Null);
5375 if (t == TypeManager.bool_type)
5376 return new BoolConstant (false, Location.Null);
5377 if (t == TypeManager.decimal_type)
5378 return new DecimalConstant (0, Location.Null);
5379 if (TypeManager.IsEnumType (t))
5380 return new EnumConstant (Constantify (TypeManager.GetEnumUnderlyingType (t)), t);
5381 if (TypeManager.IsNullableType (t))
5382 return Nullable.LiftedNull.Create (t, Location.Null);
5384 return null;
5388 // Checks whether the type is an interface that has the
5389 // [ComImport, CoClass] attributes and must be treated
5390 // specially
5392 public Expression CheckComImport (ResolveContext ec)
5394 if (!type.IsInterface)
5395 return null;
5398 // Turn the call into:
5399 // (the-interface-stated) (new class-referenced-in-coclassattribute ())
5401 Type real_class = AttributeTester.GetCoClassAttribute (type);
5402 if (real_class == null)
5403 return null;
5405 New proxy = new New (new TypeExpression (real_class, loc), Arguments, loc);
5406 Cast cast = new Cast (new TypeExpression (type, loc), proxy, loc);
5407 return cast.Resolve (ec);
5410 public override Expression CreateExpressionTree (ResolveContext ec)
5412 Arguments args;
5413 if (method == null) {
5414 args = new Arguments (1);
5415 args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
5416 } else {
5417 args = Arguments.CreateForExpressionTree (ec,
5418 Arguments,
5419 method.CreateExpressionTree (ec));
5422 return CreateExpressionFactoryCall (ec, "New", args);
5425 public override Expression DoResolve (ResolveContext ec)
5428 // The New DoResolve might be called twice when initializing field
5429 // expressions (see EmitFieldInitializers, the call to
5430 // GetInitializerExpression will perform a resolve on the expression,
5431 // and later the assign will trigger another resolution
5433 // This leads to bugs (#37014)
5435 if (type != null){
5436 if (RequestedType is NewDelegate)
5437 return RequestedType;
5438 return this;
5441 TypeExpr texpr = RequestedType.ResolveAsTypeTerminal (ec, false);
5442 if (texpr == null)
5443 return null;
5445 type = texpr.Type;
5447 if (type.IsPointer) {
5448 ec.Report.Error (1919, loc, "Unsafe type `{0}' cannot be used in an object creation expression",
5449 TypeManager.CSharpName (type));
5450 return null;
5453 if (Arguments == null) {
5454 Constant c = Constantify (type);
5455 if (c != null)
5456 return ReducedExpression.Create (c, this);
5459 if (TypeManager.IsDelegateType (type)) {
5460 return (new NewDelegate (type, Arguments, loc)).Resolve (ec);
5463 if (TypeManager.IsGenericParameter (type)) {
5464 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (type);
5466 if ((gc == null) || (!gc.HasConstructorConstraint && !gc.IsValueType)) {
5467 ec.Report.Error (304, loc,
5468 "Cannot create an instance of the variable type '{0}' because it doesn't have the new() constraint",
5469 TypeManager.CSharpName (type));
5470 return null;
5473 if ((Arguments != null) && (Arguments.Count != 0)) {
5474 ec.Report.Error (417, loc,
5475 "`{0}': cannot provide arguments when creating an instance of a variable type",
5476 TypeManager.CSharpName (type));
5477 return null;
5480 if (TypeManager.activator_create_instance == null) {
5481 Type activator_type = TypeManager.CoreLookupType (ec.Compiler, "System", "Activator", Kind.Class, true);
5482 if (activator_type != null) {
5483 TypeManager.activator_create_instance = TypeManager.GetPredefinedMethod (
5484 activator_type, "CreateInstance", loc, Type.EmptyTypes);
5488 is_type_parameter = true;
5489 eclass = ExprClass.Value;
5490 return this;
5493 if (type.IsAbstract && type.IsSealed) {
5494 ec.Report.SymbolRelatedToPreviousError (type);
5495 ec.Report.Error (712, loc, "Cannot create an instance of the static class `{0}'", TypeManager.CSharpName (type));
5496 return null;
5499 if (type.IsInterface || type.IsAbstract){
5500 if (!TypeManager.IsGenericType (type)) {
5501 RequestedType = CheckComImport (ec);
5502 if (RequestedType != null)
5503 return RequestedType;
5506 ec.Report.SymbolRelatedToPreviousError (type);
5507 ec.Report.Error (144, loc, "Cannot create an instance of the abstract class or interface `{0}'", TypeManager.CSharpName (type));
5508 return null;
5511 bool is_struct = TypeManager.IsStruct (type);
5512 eclass = ExprClass.Value;
5515 // SRE returns a match for .ctor () on structs (the object constructor),
5516 // so we have to manually ignore it.
5518 if (is_struct && Arguments == null)
5519 return this;
5521 // For member-lookup, treat 'new Foo (bar)' as call to 'foo.ctor (bar)', where 'foo' is of type 'Foo'.
5522 Expression ml = MemberLookupFinal (ec, type, type, ConstructorInfo.ConstructorName,
5523 MemberTypes.Constructor, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
5525 if (Arguments != null) {
5526 bool dynamic;
5527 Arguments.Resolve (ec, out dynamic);
5529 if (dynamic) {
5530 Arguments.Insert (0, new Argument (new TypeOf (texpr, loc).Resolve (ec)));
5531 return new DynamicInvocation (new SimpleName (ConstructorInfo.ConstructorName, loc), Arguments, type, loc).Resolve (ec);
5535 if (ml == null)
5536 return null;
5538 method = ml as MethodGroupExpr;
5539 if (method == null) {
5540 ml.Error_UnexpectedKind (ec, ResolveFlags.MethodGroup, loc);
5541 return null;
5544 method = method.OverloadResolve (ec, ref Arguments, false, loc);
5545 if (method == null)
5546 return null;
5548 return this;
5551 bool DoEmitTypeParameter (EmitContext ec)
5553 #if GMCS_SOURCE
5554 ILGenerator ig = ec.ig;
5555 // IMemoryLocation ml;
5557 MethodInfo ci = TypeManager.activator_create_instance.MakeGenericMethod (
5558 new Type [] { type });
5560 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (type);
5561 if (gc.HasReferenceTypeConstraint || gc.HasClassConstraint) {
5562 ig.Emit (OpCodes.Call, ci);
5563 return true;
5566 // Allow DoEmit() to be called multiple times.
5567 // We need to create a new LocalTemporary each time since
5568 // you can't share LocalBuilders among ILGeneators.
5569 LocalTemporary temp = new LocalTemporary (type);
5571 Label label_activator = ig.DefineLabel ();
5572 Label label_end = ig.DefineLabel ();
5574 temp.AddressOf (ec, AddressOp.Store);
5575 ig.Emit (OpCodes.Initobj, type);
5577 temp.Emit (ec);
5578 ig.Emit (OpCodes.Box, type);
5579 ig.Emit (OpCodes.Brfalse, label_activator);
5581 temp.AddressOf (ec, AddressOp.Store);
5582 ig.Emit (OpCodes.Initobj, type);
5583 temp.Emit (ec);
5584 ig.Emit (OpCodes.Br_S, label_end);
5586 ig.MarkLabel (label_activator);
5588 ig.Emit (OpCodes.Call, ci);
5589 ig.MarkLabel (label_end);
5590 return true;
5591 #else
5592 throw new InternalErrorException ();
5593 #endif
5597 // This Emit can be invoked in two contexts:
5598 // * As a mechanism that will leave a value on the stack (new object)
5599 // * As one that wont (init struct)
5601 // If we are dealing with a ValueType, we have a few
5602 // situations to deal with:
5604 // * The target is a ValueType, and we have been provided
5605 // the instance (this is easy, we are being assigned).
5607 // * The target of New is being passed as an argument,
5608 // to a boxing operation or a function that takes a
5609 // ValueType.
5611 // In this case, we need to create a temporary variable
5612 // that is the argument of New.
5614 // Returns whether a value is left on the stack
5616 // *** Implementation note ***
5618 // To benefit from this optimization, each assignable expression
5619 // has to manually cast to New and call this Emit.
5621 // TODO: It's worth to implement it for arrays and fields
5623 public virtual bool Emit (EmitContext ec, IMemoryLocation target)
5625 bool is_value_type = TypeManager.IsValueType (type);
5626 ILGenerator ig = ec.ig;
5627 VariableReference vr = target as VariableReference;
5629 if (target != null && is_value_type && (vr != null || method == null)) {
5630 target.AddressOf (ec, AddressOp.Store);
5631 } else if (vr != null && vr.IsRef) {
5632 vr.EmitLoad (ec);
5635 if (Arguments != null)
5636 Arguments.Emit (ec);
5638 if (is_value_type) {
5639 if (method == null) {
5640 ig.Emit (OpCodes.Initobj, type);
5641 return false;
5644 if (vr != null) {
5645 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
5646 return false;
5650 if (is_type_parameter)
5651 return DoEmitTypeParameter (ec);
5653 ConstructorInfo ci = (ConstructorInfo) method;
5654 #if MS_COMPATIBLE
5655 if (TypeManager.IsGenericType (type) && type.IsGenericTypeDefinition)
5656 ci = TypeBuilder.GetConstructor (type, ci);
5657 #endif
5659 ig.Emit (OpCodes.Newobj, ci);
5660 return true;
5663 public override void Emit (EmitContext ec)
5665 LocalTemporary v = null;
5666 if (method == null && TypeManager.IsValueType (type)) {
5667 // TODO: Use temporary variable from pool
5668 v = new LocalTemporary (type);
5671 if (!Emit (ec, v))
5672 v.Emit (ec);
5675 public override void EmitStatement (EmitContext ec)
5677 LocalTemporary v = null;
5678 if (method == null && TypeManager.IsValueType (type)) {
5679 // TODO: Use temporary variable from pool
5680 v = new LocalTemporary (type);
5683 if (Emit (ec, v))
5684 ec.ig.Emit (OpCodes.Pop);
5687 public bool IsDefaultValueType {
5688 get {
5689 return TypeManager.IsValueType (type) && !HasInitializer && Arguments == null;
5693 public virtual bool HasInitializer {
5694 get {
5695 return false;
5699 public void AddressOf (EmitContext ec, AddressOp mode)
5701 EmitAddressOf (ec, mode);
5704 protected virtual IMemoryLocation EmitAddressOf (EmitContext ec, AddressOp mode)
5706 LocalTemporary value_target = new LocalTemporary (type);
5708 if (is_type_parameter) {
5709 DoEmitTypeParameter (ec);
5710 value_target.Store (ec);
5711 value_target.AddressOf (ec, mode);
5712 return value_target;
5715 if (!TypeManager.IsStruct (type)){
5717 // We throw an exception. So far, I believe we only need to support
5718 // value types:
5719 // foreach (int j in new StructType ())
5720 // see bug 42390
5722 throw new Exception ("AddressOf should not be used for classes");
5725 value_target.AddressOf (ec, AddressOp.Store);
5727 if (method == null) {
5728 ec.ig.Emit (OpCodes.Initobj, type);
5729 } else {
5730 if (Arguments != null)
5731 Arguments.Emit (ec);
5733 ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
5736 value_target.AddressOf (ec, mode);
5737 return value_target;
5740 protected override void CloneTo (CloneContext clonectx, Expression t)
5742 New target = (New) t;
5744 target.RequestedType = RequestedType.Clone (clonectx);
5745 if (Arguments != null){
5746 target.Arguments = Arguments.Clone (clonectx);
5750 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
5752 if (method != null) {
5753 method.MutateHoistedGenericType (storey);
5754 if (Arguments != null) {
5755 Arguments.MutateHoistedGenericType (storey);
5759 type = storey.MutateType (type);
5763 /// <summary>
5764 /// 14.5.10.2: Represents an array creation expression.
5765 /// </summary>
5767 /// <remarks>
5768 /// There are two possible scenarios here: one is an array creation
5769 /// expression that specifies the dimensions and optionally the
5770 /// initialization data and the other which does not need dimensions
5771 /// specified but where initialization data is mandatory.
5772 /// </remarks>
5773 public class ArrayCreation : Expression {
5774 FullNamedExpression requested_base_type;
5775 ArrayList initializers;
5778 // The list of Argument types.
5779 // This is used to construct the `newarray' or constructor signature
5781 protected ArrayList arguments;
5783 protected Type array_element_type;
5784 bool expect_initializers = false;
5785 int num_arguments = 0;
5786 protected int dimensions;
5787 protected readonly string rank;
5789 protected ArrayList array_data;
5791 IDictionary bounds;
5793 // The number of constants in array initializers
5794 int const_initializers_count;
5795 bool only_constant_initializers;
5797 public ArrayCreation (FullNamedExpression requested_base_type, ArrayList exprs, string rank, ArrayList initializers, Location l)
5799 this.requested_base_type = requested_base_type;
5800 this.initializers = initializers;
5801 this.rank = rank;
5802 loc = l;
5804 arguments = new ArrayList (exprs.Count);
5806 foreach (Expression e in exprs) {
5807 arguments.Add (e);
5808 num_arguments++;
5812 public ArrayCreation (FullNamedExpression requested_base_type, string rank, ArrayList initializers, Location l)
5814 this.requested_base_type = requested_base_type;
5815 this.initializers = initializers;
5816 this.rank = rank;
5817 loc = l;
5819 //this.rank = rank.Substring (0, rank.LastIndexOf ('['));
5821 //string tmp = rank.Substring (rank.LastIndexOf ('['));
5823 //dimensions = tmp.Length - 1;
5824 expect_initializers = true;
5827 protected override void Error_NegativeArrayIndex (ResolveContext ec, Location loc)
5829 ec.Report.Error (248, loc, "Cannot create an array with a negative size");
5832 bool CheckIndices (ResolveContext ec, ArrayList probe, int idx, bool specified_dims, int child_bounds)
5834 if (specified_dims) {
5835 Expression a = (Expression) arguments [idx];
5836 a = a.Resolve (ec);
5837 if (a == null)
5838 return false;
5840 Constant c = a as Constant;
5841 if (c != null) {
5842 c = c.ImplicitConversionRequired (ec, TypeManager.int32_type, a.Location);
5845 if (c == null) {
5846 ec.Report.Error (150, a.Location, "A constant value is expected");
5847 return false;
5850 int value = (int) c.GetValue ();
5852 if (value != probe.Count) {
5853 ec.Report.Error (847, loc, "An array initializer of length `{0}' was expected", value);
5854 return false;
5857 bounds [idx] = value;
5860 only_constant_initializers = true;
5861 for (int i = 0; i < probe.Count; ++i) {
5862 object o = probe [i];
5863 if (o is ArrayList) {
5864 ArrayList sub_probe = o as ArrayList;
5865 if (idx + 1 >= dimensions){
5866 ec.Report.Error (623, loc, "Array initializers can only be used in a variable or field initializer. Try using a new expression instead");
5867 return false;
5870 bool ret = CheckIndices (ec, sub_probe, idx + 1, specified_dims, child_bounds - 1);
5871 if (!ret)
5872 return false;
5873 } else if (child_bounds > 1) {
5874 ec.Report.Error (846, ((Expression) o).Location, "A nested array initializer was expected");
5875 } else {
5876 Expression element = ResolveArrayElement (ec, (Expression) o);
5877 if (element == null)
5878 continue;
5880 // Initializers with the default values can be ignored
5881 Constant c = element as Constant;
5882 if (c != null) {
5883 if (c.IsDefaultInitializer (array_element_type)) {
5884 element = null;
5886 else {
5887 ++const_initializers_count;
5889 } else {
5890 only_constant_initializers = false;
5893 array_data.Add (element);
5897 return true;
5900 public override Expression CreateExpressionTree (ResolveContext ec)
5902 Arguments args;
5904 if (array_data == null) {
5905 args = new Arguments (arguments.Count + 1);
5906 args.Add (new Argument (new TypeOf (new TypeExpression (array_element_type, loc), loc)));
5907 foreach (Expression a in arguments)
5908 args.Add (new Argument (a.CreateExpressionTree (ec)));
5910 return CreateExpressionFactoryCall (ec, "NewArrayBounds", args);
5913 if (dimensions > 1) {
5914 ec.Report.Error (838, loc, "An expression tree cannot contain a multidimensional array initializer");
5915 return null;
5918 args = new Arguments (array_data == null ? 1 : array_data.Count + 1);
5919 args.Add (new Argument (new TypeOf (new TypeExpression (array_element_type, loc), loc)));
5920 if (array_data != null) {
5921 for (int i = 0; i < array_data.Count; ++i) {
5922 Expression e = (Expression) array_data [i];
5923 if (e == null)
5924 e = Convert.ImplicitConversion (ec, (Expression) initializers [i], array_element_type, loc);
5926 args.Add (new Argument (e.CreateExpressionTree (ec)));
5930 return CreateExpressionFactoryCall (ec, "NewArrayInit", args);
5933 public void UpdateIndices ()
5935 int i = 0;
5936 for (ArrayList probe = initializers; probe != null;) {
5937 if (probe.Count > 0 && probe [0] is ArrayList) {
5938 Expression e = new IntConstant (probe.Count, Location.Null);
5939 arguments.Add (e);
5941 bounds [i++] = probe.Count;
5943 probe = (ArrayList) probe [0];
5945 } else {
5946 Expression e = new IntConstant (probe.Count, Location.Null);
5947 arguments.Add (e);
5949 bounds [i++] = probe.Count;
5950 return;
5956 Expression first_emit;
5957 LocalTemporary first_emit_temp;
5959 protected virtual Expression ResolveArrayElement (ResolveContext ec, Expression element)
5961 element = element.Resolve (ec);
5962 if (element == null)
5963 return null;
5965 if (element is CompoundAssign.TargetExpression) {
5966 if (first_emit != null)
5967 throw new InternalErrorException ("Can only handle one mutator at a time");
5968 first_emit = element;
5969 element = first_emit_temp = new LocalTemporary (element.Type);
5972 return Convert.ImplicitConversionRequired (
5973 ec, element, array_element_type, loc);
5976 protected bool ResolveInitializers (ResolveContext ec)
5978 if (initializers == null) {
5979 return !expect_initializers;
5983 // We use this to store all the date values in the order in which we
5984 // will need to store them in the byte blob later
5986 array_data = new ArrayList ();
5987 bounds = new System.Collections.Specialized.HybridDictionary ();
5989 if (arguments != null)
5990 return CheckIndices (ec, initializers, 0, true, dimensions);
5992 arguments = new ArrayList ();
5994 if (!CheckIndices (ec, initializers, 0, false, dimensions))
5995 return false;
5997 UpdateIndices ();
5999 return true;
6003 // Resolved the type of the array
6005 bool ResolveArrayType (ResolveContext ec)
6007 if (requested_base_type == null) {
6008 ec.Report.Error (622, loc, "Can only use array initializer expressions to assign to array types. Try using a new expression instead");
6009 return false;
6012 if (requested_base_type is VarExpr) {
6013 ec.Report.Error (820, loc, "An implicitly typed local variable declarator cannot use an array initializer");
6014 return false;
6017 StringBuilder array_qualifier = new StringBuilder (rank);
6020 // `In the first form allocates an array instace of the type that results
6021 // from deleting each of the individual expression from the expression list'
6023 if (num_arguments > 0) {
6024 array_qualifier.Append ("[");
6025 for (int i = num_arguments-1; i > 0; i--)
6026 array_qualifier.Append (",");
6027 array_qualifier.Append ("]");
6031 // Lookup the type
6033 TypeExpr array_type_expr;
6034 array_type_expr = new ComposedCast (requested_base_type, array_qualifier.ToString (), loc);
6035 array_type_expr = array_type_expr.ResolveAsTypeTerminal (ec, false);
6036 if (array_type_expr == null)
6037 return false;
6039 type = array_type_expr.Type;
6040 array_element_type = TypeManager.GetElementType (type);
6041 dimensions = type.GetArrayRank ();
6043 return true;
6046 public override Expression DoResolve (ResolveContext ec)
6048 if (type != null)
6049 return this;
6051 if (!ResolveArrayType (ec))
6052 return null;
6055 // First step is to validate the initializers and fill
6056 // in any missing bits
6058 if (!ResolveInitializers (ec))
6059 return null;
6061 for (int i = 0; i < arguments.Count; ++i) {
6062 Expression e = ((Expression) arguments[i]).Resolve (ec);
6063 if (e == null)
6064 continue;
6066 arguments [i] = ConvertExpressionToArrayIndex (ec, e);
6069 eclass = ExprClass.Value;
6070 return this;
6073 MethodInfo GetArrayMethod (int arguments)
6075 ModuleBuilder mb = RootContext.ToplevelTypes.Builder;
6077 Type[] arg_types = new Type[arguments];
6078 for (int i = 0; i < arguments; i++)
6079 arg_types[i] = TypeManager.int32_type;
6081 MethodInfo mi = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
6082 arg_types);
6084 if (mi == null) {
6085 RootContext.ToplevelTypes.Compiler.Report.Error (-6, "New invocation: Can not find a constructor for " +
6086 "this argument list");
6087 return null;
6090 return mi;
6093 byte [] MakeByteBlob ()
6095 int factor;
6096 byte [] data;
6097 byte [] element;
6098 int count = array_data.Count;
6100 if (TypeManager.IsEnumType (array_element_type))
6101 array_element_type = TypeManager.GetEnumUnderlyingType (array_element_type);
6103 factor = GetTypeSize (array_element_type);
6104 if (factor == 0)
6105 throw new Exception ("unrecognized type in MakeByteBlob: " + array_element_type);
6107 data = new byte [(count * factor + 3) & ~3];
6108 int idx = 0;
6110 for (int i = 0; i < count; ++i) {
6111 object v = array_data [i];
6113 if (v is EnumConstant)
6114 v = ((EnumConstant) v).Child;
6116 if (v is Constant && !(v is StringConstant))
6117 v = ((Constant) v).GetValue ();
6118 else {
6119 idx += factor;
6120 continue;
6123 if (array_element_type == TypeManager.int64_type){
6124 if (!(v is Expression)){
6125 long val = (long) v;
6127 for (int j = 0; j < factor; ++j) {
6128 data [idx + j] = (byte) (val & 0xFF);
6129 val = (val >> 8);
6132 } else if (array_element_type == TypeManager.uint64_type){
6133 if (!(v is Expression)){
6134 ulong val = (ulong) v;
6136 for (int j = 0; j < factor; ++j) {
6137 data [idx + j] = (byte) (val & 0xFF);
6138 val = (val >> 8);
6141 } else if (array_element_type == TypeManager.float_type) {
6142 if (!(v is Expression)){
6143 element = BitConverter.GetBytes ((float) v);
6145 for (int j = 0; j < factor; ++j)
6146 data [idx + j] = element [j];
6147 if (!BitConverter.IsLittleEndian)
6148 System.Array.Reverse (data, idx, 4);
6150 } else if (array_element_type == TypeManager.double_type) {
6151 if (!(v is Expression)){
6152 element = BitConverter.GetBytes ((double) v);
6154 for (int j = 0; j < factor; ++j)
6155 data [idx + j] = element [j];
6157 // FIXME: Handle the ARM float format.
6158 if (!BitConverter.IsLittleEndian)
6159 System.Array.Reverse (data, idx, 8);
6161 } else if (array_element_type == TypeManager.char_type){
6162 if (!(v is Expression)){
6163 int val = (int) ((char) v);
6165 data [idx] = (byte) (val & 0xff);
6166 data [idx+1] = (byte) (val >> 8);
6168 } else if (array_element_type == TypeManager.short_type){
6169 if (!(v is Expression)){
6170 int val = (int) ((short) v);
6172 data [idx] = (byte) (val & 0xff);
6173 data [idx+1] = (byte) (val >> 8);
6175 } else if (array_element_type == TypeManager.ushort_type){
6176 if (!(v is Expression)){
6177 int val = (int) ((ushort) v);
6179 data [idx] = (byte) (val & 0xff);
6180 data [idx+1] = (byte) (val >> 8);
6182 } else if (array_element_type == TypeManager.int32_type) {
6183 if (!(v is Expression)){
6184 int val = (int) v;
6186 data [idx] = (byte) (val & 0xff);
6187 data [idx+1] = (byte) ((val >> 8) & 0xff);
6188 data [idx+2] = (byte) ((val >> 16) & 0xff);
6189 data [idx+3] = (byte) (val >> 24);
6191 } else if (array_element_type == TypeManager.uint32_type) {
6192 if (!(v is Expression)){
6193 uint val = (uint) v;
6195 data [idx] = (byte) (val & 0xff);
6196 data [idx+1] = (byte) ((val >> 8) & 0xff);
6197 data [idx+2] = (byte) ((val >> 16) & 0xff);
6198 data [idx+3] = (byte) (val >> 24);
6200 } else if (array_element_type == TypeManager.sbyte_type) {
6201 if (!(v is Expression)){
6202 sbyte val = (sbyte) v;
6203 data [idx] = (byte) val;
6205 } else if (array_element_type == TypeManager.byte_type) {
6206 if (!(v is Expression)){
6207 byte val = (byte) v;
6208 data [idx] = (byte) val;
6210 } else if (array_element_type == TypeManager.bool_type) {
6211 if (!(v is Expression)){
6212 bool val = (bool) v;
6213 data [idx] = (byte) (val ? 1 : 0);
6215 } else if (array_element_type == TypeManager.decimal_type){
6216 if (!(v is Expression)){
6217 int [] bits = Decimal.GetBits ((decimal) v);
6218 int p = idx;
6220 // FIXME: For some reason, this doesn't work on the MS runtime.
6221 int [] nbits = new int [4];
6222 nbits [0] = bits [3];
6223 nbits [1] = bits [2];
6224 nbits [2] = bits [0];
6225 nbits [3] = bits [1];
6227 for (int j = 0; j < 4; j++){
6228 data [p++] = (byte) (nbits [j] & 0xff);
6229 data [p++] = (byte) ((nbits [j] >> 8) & 0xff);
6230 data [p++] = (byte) ((nbits [j] >> 16) & 0xff);
6231 data [p++] = (byte) (nbits [j] >> 24);
6234 } else
6235 throw new Exception ("Unrecognized type in MakeByteBlob: " + array_element_type);
6237 idx += factor;
6240 return data;
6243 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
6245 array_element_type = storey.MutateType (array_element_type);
6246 type = storey.MutateType (type);
6247 if (arguments != null) {
6248 foreach (Expression e in arguments)
6249 e.MutateHoistedGenericType (storey);
6252 if (array_data != null) {
6253 foreach (Expression e in array_data) {
6254 // Don't mutate values optimized away
6255 if (e == null)
6256 continue;
6258 e.MutateHoistedGenericType (storey);
6264 // Emits the initializers for the array
6266 void EmitStaticInitializers (EmitContext ec)
6268 // FIXME: This should go to Resolve !
6269 if (TypeManager.void_initializearray_array_fieldhandle == null) {
6270 TypeManager.void_initializearray_array_fieldhandle = TypeManager.GetPredefinedMethod (
6271 TypeManager.runtime_helpers_type, "InitializeArray", loc,
6272 TypeManager.array_type, TypeManager.runtime_field_handle_type);
6273 if (TypeManager.void_initializearray_array_fieldhandle == null)
6274 return;
6278 // First, the static data
6280 FieldBuilder fb;
6281 ILGenerator ig = ec.ig;
6283 byte [] data = MakeByteBlob ();
6285 fb = RootContext.MakeStaticData (data);
6287 ig.Emit (OpCodes.Dup);
6288 ig.Emit (OpCodes.Ldtoken, fb);
6289 ig.Emit (OpCodes.Call,
6290 TypeManager.void_initializearray_array_fieldhandle);
6294 // Emits pieces of the array that can not be computed at compile
6295 // time (variables and string locations).
6297 // This always expect the top value on the stack to be the array
6299 void EmitDynamicInitializers (EmitContext ec, bool emitConstants)
6301 ILGenerator ig = ec.ig;
6302 int dims = bounds.Count;
6303 int [] current_pos = new int [dims];
6305 MethodInfo set = null;
6307 if (dims != 1){
6308 Type [] args = new Type [dims + 1];
6310 for (int j = 0; j < dims; j++)
6311 args [j] = TypeManager.int32_type;
6312 args [dims] = array_element_type;
6314 set = RootContext.ToplevelTypes.Builder.GetArrayMethod (
6315 type, "Set",
6316 CallingConventions.HasThis | CallingConventions.Standard,
6317 TypeManager.void_type, args);
6320 for (int i = 0; i < array_data.Count; i++){
6322 Expression e = (Expression)array_data [i];
6324 // Constant can be initialized via StaticInitializer
6325 if (e != null && !(!emitConstants && e is Constant)) {
6326 Type etype = e.Type;
6328 ig.Emit (OpCodes.Dup);
6330 for (int idx = 0; idx < dims; idx++)
6331 IntConstant.EmitInt (ig, current_pos [idx]);
6334 // If we are dealing with a struct, get the
6335 // address of it, so we can store it.
6337 if ((dims == 1) && TypeManager.IsStruct (etype) &&
6338 (!TypeManager.IsBuiltinOrEnum (etype) ||
6339 etype == TypeManager.decimal_type)) {
6341 ig.Emit (OpCodes.Ldelema, etype);
6344 e.Emit (ec);
6346 if (dims == 1) {
6347 bool is_stobj, has_type_arg;
6348 OpCode op = ArrayAccess.GetStoreOpcode (etype, out is_stobj, out has_type_arg);
6349 if (is_stobj)
6350 ig.Emit (OpCodes.Stobj, etype);
6351 else if (has_type_arg)
6352 ig.Emit (op, etype);
6353 else
6354 ig.Emit (op);
6355 } else
6356 ig.Emit (OpCodes.Call, set);
6361 // Advance counter
6363 for (int j = dims - 1; j >= 0; j--){
6364 current_pos [j]++;
6365 if (current_pos [j] < (int) bounds [j])
6366 break;
6367 current_pos [j] = 0;
6372 public override void Emit (EmitContext ec)
6374 ILGenerator ig = ec.ig;
6376 if (first_emit != null) {
6377 first_emit.Emit (ec);
6378 first_emit_temp.Store (ec);
6381 foreach (Expression e in arguments)
6382 e.Emit (ec);
6384 if (arguments.Count == 1)
6385 ig.Emit (OpCodes.Newarr, array_element_type);
6386 else {
6387 ig.Emit (OpCodes.Newobj, GetArrayMethod (arguments.Count));
6390 if (initializers == null)
6391 return;
6393 // Emit static initializer for arrays which have contain more than 4 items and
6394 // the static initializer will initialize at least 25% of array values.
6395 // NOTE: const_initializers_count does not contain default constant values.
6396 if (const_initializers_count >= 4 && const_initializers_count * 4 > (array_data.Count) &&
6397 TypeManager.IsPrimitiveType (array_element_type)) {
6398 EmitStaticInitializers (ec);
6400 if (!only_constant_initializers)
6401 EmitDynamicInitializers (ec, false);
6402 } else {
6403 EmitDynamicInitializers (ec, true);
6406 if (first_emit_temp != null)
6407 first_emit_temp.Release (ec);
6410 public override bool GetAttributableValue (ResolveContext ec, Type value_type, out object value)
6412 if (arguments.Count != 1) {
6413 // ec.Report.Error (-211, Location, "attribute can not encode multi-dimensional arrays");
6414 return base.GetAttributableValue (ec, null, out value);
6417 if (array_data == null) {
6418 Expression arg = (Expression) arguments[0];
6419 object arg_value;
6420 if (arg.GetAttributableValue (ec, arg.Type, out arg_value) && arg_value is int && (int)arg_value == 0) {
6421 value = Array.CreateInstance (array_element_type, 0);
6422 return true;
6425 // ec.Report.Error (-212, Location, "array should be initialized when passing it to an attribute");
6426 return base.GetAttributableValue (ec, null, out value);
6429 Array ret = Array.CreateInstance (array_element_type, array_data.Count);
6430 object element_value;
6431 for (int i = 0; i < ret.Length; ++i)
6433 Expression e = (Expression)array_data [i];
6435 // Is null when an initializer is optimized (value == predefined value)
6436 if (e == null)
6437 continue;
6439 if (!e.GetAttributableValue (ec, array_element_type, out element_value)) {
6440 value = null;
6441 return false;
6443 ret.SetValue (element_value, i);
6445 value = ret;
6446 return true;
6449 protected override void CloneTo (CloneContext clonectx, Expression t)
6451 ArrayCreation target = (ArrayCreation) t;
6453 if (requested_base_type != null)
6454 target.requested_base_type = (FullNamedExpression)requested_base_type.Clone (clonectx);
6456 if (arguments != null){
6457 target.arguments = new ArrayList (arguments.Count);
6458 foreach (Expression e in arguments)
6459 target.arguments.Add (e.Clone (clonectx));
6462 if (initializers != null){
6463 target.initializers = new ArrayList (initializers.Count);
6464 foreach (object initializer in initializers)
6465 if (initializer is ArrayList) {
6466 ArrayList this_al = (ArrayList)initializer;
6467 ArrayList al = new ArrayList (this_al.Count);
6468 target.initializers.Add (al);
6469 foreach (Expression e in this_al)
6470 al.Add (e.Clone (clonectx));
6471 } else {
6472 target.initializers.Add (((Expression)initializer).Clone (clonectx));
6479 // Represents an implicitly typed array epxression
6481 public class ImplicitlyTypedArrayCreation : ArrayCreation
6483 public ImplicitlyTypedArrayCreation (string rank, ArrayList initializers, Location loc)
6484 : base (null, rank, initializers, loc)
6486 if (rank.Length > 2) {
6487 while (rank [++dimensions] == ',');
6488 } else {
6489 dimensions = 1;
6493 public override Expression DoResolve (ResolveContext ec)
6495 if (type != null)
6496 return this;
6498 if (!ResolveInitializers (ec))
6499 return null;
6501 if (array_element_type == null || array_element_type == TypeManager.null_type ||
6502 array_element_type == TypeManager.void_type || array_element_type == InternalType.AnonymousMethod ||
6503 array_element_type == InternalType.MethodGroup ||
6504 arguments.Count != dimensions) {
6505 Error_NoBestType (ec);
6506 return null;
6510 // At this point we found common base type for all initializer elements
6511 // but we have to be sure that all static initializer elements are of
6512 // same type
6514 UnifyInitializerElement (ec);
6516 type = TypeManager.GetConstructedType (array_element_type, rank);
6517 eclass = ExprClass.Value;
6518 return this;
6521 void Error_NoBestType (ResolveContext ec)
6523 ec.Report.Error (826, loc,
6524 "The type of an implicitly typed array cannot be inferred from the initializer. Try specifying array type explicitly");
6528 // Converts static initializer only
6530 void UnifyInitializerElement (ResolveContext ec)
6532 for (int i = 0; i < array_data.Count; ++i) {
6533 Expression e = (Expression)array_data[i];
6534 if (e != null)
6535 array_data [i] = Convert.ImplicitConversion (ec, e, array_element_type, Location.Null);
6539 protected override Expression ResolveArrayElement (ResolveContext ec, Expression element)
6541 element = element.Resolve (ec);
6542 if (element == null)
6543 return null;
6545 if (array_element_type == null) {
6546 if (element.Type != TypeManager.null_type)
6547 array_element_type = element.Type;
6549 return element;
6552 if (Convert.ImplicitConversionExists (ec, element, array_element_type)) {
6553 return element;
6556 if (Convert.ImplicitConversionExists (ec, new TypeExpression (array_element_type, loc), element.Type)) {
6557 array_element_type = element.Type;
6558 return element;
6561 Error_NoBestType (ec);
6562 return null;
6566 public sealed class CompilerGeneratedThis : This
6568 public static This Instance = new CompilerGeneratedThis ();
6570 private CompilerGeneratedThis ()
6571 : base (Location.Null)
6575 public CompilerGeneratedThis (Type type, Location loc)
6576 : base (loc)
6578 this.type = type;
6581 public override Expression DoResolve (ResolveContext ec)
6583 eclass = ExprClass.Variable;
6584 if (type == null)
6585 type = ec.CurrentType;
6587 is_struct = type.IsValueType;
6588 return this;
6591 public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
6593 return null;
6597 /// <summary>
6598 /// Represents the `this' construct
6599 /// </summary>
6601 public class This : VariableReference
6603 sealed class ThisVariable : ILocalVariable
6605 public static readonly ILocalVariable Instance = new ThisVariable ();
6607 public void Emit (EmitContext ec)
6609 ec.ig.Emit (OpCodes.Ldarg_0);
6612 public void EmitAssign (EmitContext ec)
6614 throw new InvalidOperationException ();
6617 public void EmitAddressOf (EmitContext ec)
6619 ec.ig.Emit (OpCodes.Ldarg_0);
6623 Block block;
6624 VariableInfo variable_info;
6625 protected bool is_struct;
6627 public This (Block block, Location loc)
6629 this.loc = loc;
6630 this.block = block;
6633 public This (Location loc)
6635 this.loc = loc;
6638 public override VariableInfo VariableInfo {
6639 get { return variable_info; }
6642 public override bool IsFixed {
6643 get { return false; }
6646 public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
6648 if (ae == null)
6649 return null;
6651 AnonymousMethodStorey storey = ae.Storey;
6652 while (storey != null) {
6653 AnonymousMethodStorey temp = storey.Parent as AnonymousMethodStorey;
6654 if (temp == null)
6655 return storey.HoistedThis;
6657 storey = temp;
6660 return null;
6663 public override bool IsRef {
6664 get { return is_struct; }
6667 protected override ILocalVariable Variable {
6668 get { return ThisVariable.Instance; }
6671 public static bool IsThisAvailable (ResolveContext ec)
6673 if (ec.IsStatic || ec.HasAny (ResolveContext.Options.FieldInitializerScope | ResolveContext.Options.BaseInitializer | ResolveContext.Options.ConstantScope))
6674 return false;
6676 if (ec.CurrentAnonymousMethod == null)
6677 return true;
6679 if (ec.CurrentType.IsValueType && ec.CurrentIterator == null)
6680 return false;
6682 return true;
6685 public bool ResolveBase (ResolveContext ec)
6687 if (eclass != ExprClass.Invalid)
6688 return true;
6690 eclass = ExprClass.Variable;
6691 type = ec.CurrentType;
6693 if (!IsThisAvailable (ec)) {
6694 if (ec.IsStatic && !ec.HasSet (ResolveContext.Options.ConstantScope)) {
6695 ec.Report.Error (26, loc, "Keyword `this' is not valid in a static property, static method, or static field initializer");
6696 } else if (ec.CurrentAnonymousMethod != null) {
6697 ec.Report.Error (1673, loc,
6698 "Anonymous methods inside structs cannot access instance members of `this'. " +
6699 "Consider copying `this' to a local variable outside the anonymous method and using the local instead");
6700 } else {
6701 ec.Report.Error (27, loc, "Keyword `this' is not available in the current context");
6705 is_struct = type.IsValueType;
6707 if (block != null) {
6708 if (block.Toplevel.ThisVariable != null)
6709 variable_info = block.Toplevel.ThisVariable.VariableInfo;
6711 AnonymousExpression am = ec.CurrentAnonymousMethod;
6712 if (am != null && ec.IsVariableCapturingRequired) {
6713 am.SetHasThisAccess ();
6717 return true;
6721 // Called from Invocation to check if the invocation is correct
6723 public override void CheckMarshalByRefAccess (ResolveContext ec)
6725 if ((variable_info != null) && !(TypeManager.IsStruct (type) && ec.OmitStructFlowAnalysis) &&
6726 !variable_info.IsAssigned (ec)) {
6727 ec.Report.Error (188, loc,
6728 "The `this' object cannot be used before all of its fields are assigned to");
6729 variable_info.SetAssigned (ec);
6733 public override Expression CreateExpressionTree (ResolveContext ec)
6735 Arguments args = new Arguments (1);
6736 args.Add (new Argument (this));
6738 // Use typeless constant for ldarg.0 to save some
6739 // space and avoid problems with anonymous stories
6740 return CreateExpressionFactoryCall (ec, "Constant", args);
6743 public override Expression DoResolve (ResolveContext ec)
6745 ResolveBase (ec);
6746 return this;
6749 override public Expression DoResolveLValue (ResolveContext ec, Expression right_side)
6751 if (!ResolveBase (ec))
6752 return null;
6754 if (variable_info != null)
6755 variable_info.SetAssigned (ec);
6757 if (ec.CurrentType.IsClass){
6758 if (right_side == EmptyExpression.UnaryAddress)
6759 ec.Report.Error (459, loc, "Cannot take the address of `this' because it is read-only");
6760 else if (right_side == EmptyExpression.OutAccess)
6761 ec.Report.Error (1605, loc, "Cannot pass `this' as a ref or out argument because it is read-only");
6762 else
6763 ec.Report.Error (1604, loc, "Cannot assign to `this' because it is read-only");
6766 return this;
6769 public override int GetHashCode()
6771 return block.GetHashCode ();
6774 public override string Name {
6775 get { return "this"; }
6778 public override bool Equals (object obj)
6780 This t = obj as This;
6781 if (t == null)
6782 return false;
6784 return block == t.block;
6787 protected override void CloneTo (CloneContext clonectx, Expression t)
6789 This target = (This) t;
6791 target.block = clonectx.LookupBlock (block);
6794 public override void SetHasAddressTaken ()
6796 // Nothing
6800 /// <summary>
6801 /// Represents the `__arglist' construct
6802 /// </summary>
6803 public class ArglistAccess : Expression
6805 public ArglistAccess (Location loc)
6807 this.loc = loc;
6810 public override Expression CreateExpressionTree (ResolveContext ec)
6812 throw new NotSupportedException ("ET");
6815 public override Expression DoResolve (ResolveContext ec)
6817 eclass = ExprClass.Variable;
6818 type = TypeManager.runtime_argument_handle_type;
6820 if (ec.HasSet (ResolveContext.Options.FieldInitializerScope) || !ec.CurrentBlock.Toplevel.Parameters.HasArglist) {
6821 ec.Report.Error (190, loc,
6822 "The __arglist construct is valid only within a variable argument method");
6825 return this;
6828 public override void Emit (EmitContext ec)
6830 ec.ig.Emit (OpCodes.Arglist);
6833 protected override void CloneTo (CloneContext clonectx, Expression target)
6835 // nothing.
6839 /// <summary>
6840 /// Represents the `__arglist (....)' construct
6841 /// </summary>
6842 class Arglist : Expression
6844 Arguments Arguments;
6846 public Arglist (Location loc)
6847 : this (null, loc)
6851 public Arglist (Arguments args, Location l)
6853 Arguments = args;
6854 loc = l;
6857 public Type[] ArgumentTypes {
6858 get {
6859 if (Arguments == null)
6860 return Type.EmptyTypes;
6862 Type[] retval = new Type [Arguments.Count];
6863 for (int i = 0; i < retval.Length; i++)
6864 retval [i] = Arguments [i].Expr.Type;
6866 return retval;
6870 public override Expression CreateExpressionTree (ResolveContext ec)
6872 ec.Report.Error (1952, loc, "An expression tree cannot contain a method with variable arguments");
6873 return null;
6876 public override Expression DoResolve (ResolveContext ec)
6878 eclass = ExprClass.Variable;
6879 type = InternalType.Arglist;
6880 if (Arguments != null) {
6881 bool dynamic; // Can be ignored as there is always only 1 overload
6882 Arguments.Resolve (ec, out dynamic);
6885 return this;
6888 public override void Emit (EmitContext ec)
6890 if (Arguments != null)
6891 Arguments.Emit (ec);
6894 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
6896 if (Arguments != null)
6897 Arguments.MutateHoistedGenericType (storey);
6900 protected override void CloneTo (CloneContext clonectx, Expression t)
6902 Arglist target = (Arglist) t;
6904 if (Arguments != null)
6905 target.Arguments = Arguments.Clone (clonectx);
6909 /// <summary>
6910 /// Implements the typeof operator
6911 /// </summary>
6912 public class TypeOf : Expression {
6913 Expression QueriedType;
6914 protected Type typearg;
6916 public TypeOf (Expression queried_type, Location l)
6918 QueriedType = queried_type;
6919 loc = l;
6922 public override Expression CreateExpressionTree (ResolveContext ec)
6924 Arguments args = new Arguments (2);
6925 args.Add (new Argument (this));
6926 args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
6927 return CreateExpressionFactoryCall (ec, "Constant", args);
6930 public override Expression DoResolve (ResolveContext ec)
6932 if (eclass != ExprClass.Invalid)
6933 return this;
6935 TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec, false);
6936 if (texpr == null)
6937 return null;
6939 typearg = texpr.Type;
6941 if (typearg == TypeManager.void_type) {
6942 ec.Report.Error (673, loc, "System.Void cannot be used from C#. Use typeof (void) to get the void type object");
6943 } else if (typearg.IsPointer && !ec.IsUnsafe){
6944 UnsafeError (ec, loc);
6945 } else if (texpr is DynamicTypeExpr) {
6946 ec.Report.Error (1962, QueriedType.Location,
6947 "The typeof operator cannot be used on the dynamic type");
6950 type = TypeManager.type_type;
6952 return DoResolveBase ();
6955 protected Expression DoResolveBase ()
6957 if (TypeManager.system_type_get_type_from_handle == null) {
6958 TypeManager.system_type_get_type_from_handle = TypeManager.GetPredefinedMethod (
6959 TypeManager.type_type, "GetTypeFromHandle", loc, TypeManager.runtime_handle_type);
6962 // Even though what is returned is a type object, it's treated as a value by the compiler.
6963 // In particular, 'typeof (Foo).X' is something totally different from 'Foo.X'.
6964 eclass = ExprClass.Value;
6965 return this;
6968 public override void Emit (EmitContext ec)
6970 ec.ig.Emit (OpCodes.Ldtoken, TypeManager.TypeToReflectionType (typearg));
6971 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
6974 public override bool GetAttributableValue (ResolveContext ec, Type value_type, out object value)
6976 if (TypeManager.ContainsGenericParameters (typearg) &&
6977 !TypeManager.IsGenericTypeDefinition (typearg)) {
6978 ec.Report.SymbolRelatedToPreviousError (typearg);
6979 ec.Report.Error (416, loc, "`{0}': an attribute argument cannot use type parameters",
6980 TypeManager.CSharpName (typearg));
6981 value = null;
6982 return false;
6985 if (value_type == TypeManager.object_type) {
6986 value = (object)typearg;
6987 return true;
6989 value = typearg;
6990 return true;
6993 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
6995 typearg = storey.MutateType (typearg);
6998 public Type TypeArgument {
6999 get {
7000 return typearg;
7004 protected override void CloneTo (CloneContext clonectx, Expression t)
7006 TypeOf target = (TypeOf) t;
7007 if (QueriedType != null)
7008 target.QueriedType = QueriedType.Clone (clonectx);
7012 /// <summary>
7013 /// Implements the `typeof (void)' operator
7014 /// </summary>
7015 public class TypeOfVoid : TypeOf {
7016 public TypeOfVoid (Location l) : base (null, l)
7018 loc = l;
7021 public override Expression DoResolve (ResolveContext ec)
7023 type = TypeManager.type_type;
7024 typearg = TypeManager.void_type;
7026 return DoResolveBase ();
7030 class TypeOfMethod : TypeOfMember
7032 public TypeOfMethod (MethodBase method, Location loc)
7033 : base (method, loc)
7037 public override Expression DoResolve (ResolveContext ec)
7039 if (member is MethodInfo) {
7040 type = TypeManager.methodinfo_type;
7041 if (type == null)
7042 type = TypeManager.methodinfo_type = TypeManager.CoreLookupType (ec.Compiler, "System.Reflection", "MethodInfo", Kind.Class, true);
7043 } else {
7044 type = TypeManager.ctorinfo_type;
7045 if (type == null)
7046 type = TypeManager.ctorinfo_type = TypeManager.CoreLookupType (ec.Compiler, "System.Reflection", "ConstructorInfo", Kind.Class, true);
7049 return base.DoResolve (ec);
7052 public override void Emit (EmitContext ec)
7054 if (member is ConstructorInfo)
7055 ec.ig.Emit (OpCodes.Ldtoken, (ConstructorInfo) member);
7056 else
7057 ec.ig.Emit (OpCodes.Ldtoken, (MethodInfo) member);
7059 base.Emit (ec);
7060 ec.ig.Emit (OpCodes.Castclass, type);
7063 protected override string GetMethodName {
7064 get { return "GetMethodFromHandle"; }
7067 protected override string RuntimeHandleName {
7068 get { return "RuntimeMethodHandle"; }
7071 protected override MethodInfo TypeFromHandle {
7072 get {
7073 return TypeManager.methodbase_get_type_from_handle;
7075 set {
7076 TypeManager.methodbase_get_type_from_handle = value;
7080 protected override MethodInfo TypeFromHandleGeneric {
7081 get {
7082 return TypeManager.methodbase_get_type_from_handle_generic;
7084 set {
7085 TypeManager.methodbase_get_type_from_handle_generic = value;
7089 protected override string TypeName {
7090 get { return "MethodBase"; }
7094 abstract class TypeOfMember : Expression
7096 protected readonly MemberInfo member;
7098 protected TypeOfMember (MemberInfo member, Location loc)
7100 this.member = member;
7101 this.loc = loc;
7104 public override Expression CreateExpressionTree (ResolveContext ec)
7106 Arguments args = new Arguments (2);
7107 args.Add (new Argument (this));
7108 args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
7109 return CreateExpressionFactoryCall (ec, "Constant", args);
7112 public override Expression DoResolve (ResolveContext ec)
7114 bool is_generic = TypeManager.IsGenericType (member.DeclaringType);
7115 MethodInfo mi = is_generic ? TypeFromHandleGeneric : TypeFromHandle;
7117 if (mi == null) {
7118 Type t = TypeManager.CoreLookupType (ec.Compiler, "System.Reflection", TypeName, Kind.Class, true);
7119 Type handle_type = TypeManager.CoreLookupType (ec.Compiler, "System", RuntimeHandleName, Kind.Class, true);
7121 if (t == null || handle_type == null)
7122 return null;
7124 mi = TypeManager.GetPredefinedMethod (t, GetMethodName, loc,
7125 is_generic ?
7126 new Type[] { handle_type, TypeManager.runtime_handle_type } :
7127 new Type[] { handle_type } );
7129 if (is_generic)
7130 TypeFromHandleGeneric = mi;
7131 else
7132 TypeFromHandle = mi;
7135 eclass = ExprClass.Value;
7136 return this;
7139 public override void Emit (EmitContext ec)
7141 bool is_generic = TypeManager.IsGenericType (member.DeclaringType);
7142 MethodInfo mi;
7143 if (is_generic) {
7144 mi = TypeFromHandleGeneric;
7145 ec.ig.Emit (OpCodes.Ldtoken, member.DeclaringType);
7146 } else {
7147 mi = TypeFromHandle;
7150 ec.ig.Emit (OpCodes.Call, mi);
7153 protected abstract string GetMethodName { get; }
7154 protected abstract string RuntimeHandleName { get; }
7155 protected abstract MethodInfo TypeFromHandle { get; set; }
7156 protected abstract MethodInfo TypeFromHandleGeneric { get; set; }
7157 protected abstract string TypeName { get; }
7160 class TypeOfField : TypeOfMember
7162 public TypeOfField (FieldInfo field, Location loc)
7163 : base (field, loc)
7167 public override Expression DoResolve (ResolveContext ec)
7169 if (TypeManager.fieldinfo_type == null)
7170 TypeManager.fieldinfo_type = TypeManager.CoreLookupType (ec.Compiler, "System.Reflection", TypeName, Kind.Class, true);
7172 type = TypeManager.fieldinfo_type;
7173 return base.DoResolve (ec);
7176 public override void Emit (EmitContext ec)
7178 ec.ig.Emit (OpCodes.Ldtoken, (FieldInfo) member);
7179 base.Emit (ec);
7182 protected override string GetMethodName {
7183 get { return "GetFieldFromHandle"; }
7186 protected override string RuntimeHandleName {
7187 get { return "RuntimeFieldHandle"; }
7190 protected override MethodInfo TypeFromHandle {
7191 get {
7192 return TypeManager.fieldinfo_get_field_from_handle;
7194 set {
7195 TypeManager.fieldinfo_get_field_from_handle = value;
7199 protected override MethodInfo TypeFromHandleGeneric {
7200 get {
7201 return TypeManager.fieldinfo_get_field_from_handle_generic;
7203 set {
7204 TypeManager.fieldinfo_get_field_from_handle_generic = value;
7208 protected override string TypeName {
7209 get { return "FieldInfo"; }
7213 /// <summary>
7214 /// Implements the sizeof expression
7215 /// </summary>
7216 public class SizeOf : Expression {
7217 readonly Expression QueriedType;
7218 Type type_queried;
7220 public SizeOf (Expression queried_type, Location l)
7222 this.QueriedType = queried_type;
7223 loc = l;
7226 public override Expression CreateExpressionTree (ResolveContext ec)
7228 Error_PointerInsideExpressionTree (ec);
7229 return null;
7232 public override Expression DoResolve (ResolveContext ec)
7234 TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec, false);
7235 if (texpr == null)
7236 return null;
7238 type_queried = texpr.Type;
7239 if (TypeManager.IsEnumType (type_queried))
7240 type_queried = TypeManager.GetEnumUnderlyingType (type_queried);
7242 int size_of = GetTypeSize (type_queried);
7243 if (size_of > 0) {
7244 return new IntConstant (size_of, loc);
7247 if (!TypeManager.VerifyUnManaged (type_queried, loc)){
7248 return null;
7251 if (!ec.IsUnsafe) {
7252 ec.Report.Error (233, loc,
7253 "`{0}' does not have a predefined size, therefore sizeof can only be used in an unsafe context (consider using System.Runtime.InteropServices.Marshal.SizeOf)",
7254 TypeManager.CSharpName (type_queried));
7257 type = TypeManager.int32_type;
7258 eclass = ExprClass.Value;
7259 return this;
7262 public override void Emit (EmitContext ec)
7264 int size = GetTypeSize (type_queried);
7266 if (size == 0)
7267 ec.ig.Emit (OpCodes.Sizeof, type_queried);
7268 else
7269 IntConstant.EmitInt (ec.ig, size);
7272 protected override void CloneTo (CloneContext clonectx, Expression t)
7277 /// <summary>
7278 /// Implements the qualified-alias-member (::) expression.
7279 /// </summary>
7280 public class QualifiedAliasMember : MemberAccess
7282 readonly string alias;
7283 public static readonly string GlobalAlias = "global";
7285 public QualifiedAliasMember (string alias, string identifier, TypeArguments targs, Location l)
7286 : base (null, identifier, targs, l)
7288 this.alias = alias;
7291 public QualifiedAliasMember (string alias, string identifier, Location l)
7292 : base (null, identifier, l)
7294 this.alias = alias;
7297 public override FullNamedExpression ResolveAsTypeStep (IMemberContext ec, bool silent)
7299 if (alias == GlobalAlias) {
7300 expr = GlobalRootNamespace.Instance;
7301 return base.ResolveAsTypeStep (ec, silent);
7304 int errors = ec.Compiler.Report.Errors;
7305 expr = ec.LookupNamespaceAlias (alias);
7306 if (expr == null) {
7307 if (errors == ec.Compiler.Report.Errors)
7308 ec.Compiler.Report.Error (432, loc, "Alias `{0}' not found", alias);
7309 return null;
7312 FullNamedExpression fne = base.ResolveAsTypeStep (ec, silent);
7313 if (fne == null)
7314 return null;
7316 if (expr.eclass == ExprClass.Type) {
7317 if (!silent) {
7318 ec.Compiler.Report.Error (431, loc,
7319 "Alias `{0}' cannot be used with '::' since it denotes a type. Consider replacing '::' with '.'", alias);
7321 return null;
7324 return fne;
7327 public override Expression DoResolve (ResolveContext ec)
7329 return ResolveAsTypeStep (ec, false);
7332 protected override void Error_IdentifierNotFound (IMemberContext rc, FullNamedExpression expr_type, string identifier)
7334 rc.Compiler.Report.Error (687, loc,
7335 "A namespace alias qualifier `{0}' did not resolve to a namespace or a type",
7336 GetSignatureForError ());
7339 public override string GetSignatureForError ()
7341 string name = Name;
7342 if (targs != null) {
7343 name = TypeManager.RemoveGenericArity (Name) + "<" +
7344 targs.GetSignatureForError () + ">";
7347 return alias + "::" + name;
7350 protected override void CloneTo (CloneContext clonectx, Expression t)
7352 // Nothing
7356 /// <summary>
7357 /// Implements the member access expression
7358 /// </summary>
7359 public class MemberAccess : ATypeNameExpression {
7360 protected Expression expr;
7362 public MemberAccess (Expression expr, string id)
7363 : base (id, expr.Location)
7365 this.expr = expr;
7368 public MemberAccess (Expression expr, string identifier, Location loc)
7369 : base (identifier, loc)
7371 this.expr = expr;
7374 public MemberAccess (Expression expr, string identifier, TypeArguments args, Location loc)
7375 : base (identifier, args, loc)
7377 this.expr = expr;
7380 Expression DoResolve (ResolveContext ec, Expression right_side)
7382 if (type != null)
7383 throw new Exception ();
7386 // Resolve the expression with flow analysis turned off, we'll do the definite
7387 // assignment checks later. This is because we don't know yet what the expression
7388 // will resolve to - it may resolve to a FieldExpr and in this case we must do the
7389 // definite assignment check on the actual field and not on the whole struct.
7392 SimpleName original = expr as SimpleName;
7393 Expression expr_resolved = expr.Resolve (ec,
7394 ResolveFlags.VariableOrValue | ResolveFlags.Type |
7395 ResolveFlags.Intermediate | ResolveFlags.DisableStructFlowAnalysis);
7397 if (expr_resolved == null)
7398 return null;
7400 string LookupIdentifier = MemberName.MakeName (Name, targs);
7402 Namespace ns = expr_resolved as Namespace;
7403 if (ns != null) {
7404 FullNamedExpression retval = ns.Lookup (ec.Compiler, LookupIdentifier, loc);
7406 if (retval == null)
7407 ns.Error_NamespaceDoesNotExist (loc, LookupIdentifier, ec.Report);
7408 else if (targs != null)
7409 retval = new GenericTypeExpr (retval.Type, targs, loc).ResolveAsTypeStep (ec, false);
7411 return retval;
7414 Type expr_type = expr_resolved.Type;
7415 if (TypeManager.IsDynamicType (expr_type)) {
7416 Arguments args = new Arguments (2);
7417 args.Add (new Argument (expr_resolved.Resolve (ec)));
7418 if (right_side != null)
7419 args.Add (new Argument (right_side));
7421 return new DynamicMemberBinder (right_side != null, Name, args, loc).Resolve (ec);
7424 if (expr_type.IsPointer || expr_type == TypeManager.void_type ||
7425 expr_type == TypeManager.null_type || expr_type == InternalType.AnonymousMethod) {
7426 Unary.Error_OperatorCannotBeApplied (ec, loc, ".", expr_type);
7427 return null;
7430 Constant c = expr_resolved as Constant;
7431 if (c != null && c.GetValue () == null) {
7432 ec.Report.Warning (1720, 1, loc, "Expression will always cause a `{0}'",
7433 "System.NullReferenceException");
7436 if (targs != null) {
7437 if (!targs.Resolve (ec))
7438 return null;
7441 Expression member_lookup;
7442 member_lookup = MemberLookup (ec.Compiler,
7443 ec.CurrentType, expr_type, expr_type, Name, loc);
7445 if (member_lookup == null && targs != null) {
7446 member_lookup = MemberLookup (ec.Compiler,
7447 ec.CurrentType, expr_type, expr_type, LookupIdentifier, loc);
7450 if (member_lookup == null) {
7451 ExprClass expr_eclass = expr_resolved.eclass;
7454 // Extension methods are not allowed on all expression types
7456 if (expr_eclass == ExprClass.Value || expr_eclass == ExprClass.Variable ||
7457 expr_eclass == ExprClass.IndexerAccess || expr_eclass == ExprClass.PropertyAccess ||
7458 expr_eclass == ExprClass.EventAccess) {
7459 ExtensionMethodGroupExpr ex_method_lookup = ec.LookupExtensionMethod (expr_type, Name, loc);
7460 if (ex_method_lookup != null) {
7461 ex_method_lookup.ExtensionExpression = expr_resolved;
7463 if (targs != null) {
7464 ex_method_lookup.SetTypeArguments (ec, targs);
7467 return ex_method_lookup.DoResolve (ec);
7471 expr = expr_resolved;
7472 member_lookup = Error_MemberLookupFailed (ec,
7473 ec.CurrentType, expr_type, expr_type, Name, null,
7474 AllMemberTypes, AllBindingFlags);
7475 if (member_lookup == null)
7476 return null;
7479 TypeExpr texpr = member_lookup as TypeExpr;
7480 if (texpr != null) {
7481 if (!(expr_resolved is TypeExpr) &&
7482 (original == null || !original.IdenticalNameAndTypeName (ec, expr_resolved, loc))) {
7483 ec.Report.Error (572, loc, "`{0}': cannot reference a type through an expression; try `{1}' instead",
7484 Name, member_lookup.GetSignatureForError ());
7485 return null;
7488 if (!texpr.CheckAccessLevel (ec.MemberContext)) {
7489 ec.Report.SymbolRelatedToPreviousError (member_lookup.Type);
7490 ErrorIsInaccesible (loc, TypeManager.CSharpName (member_lookup.Type), ec.Report);
7491 return null;
7494 GenericTypeExpr ct = expr_resolved as GenericTypeExpr;
7495 if (ct != null) {
7497 // When looking up a nested type in a generic instance
7498 // via reflection, we always get a generic type definition
7499 // and not a generic instance - so we have to do this here.
7501 // See gtest-172-lib.cs and gtest-172.cs for an example.
7504 TypeArguments nested_targs;
7505 if (HasTypeArguments) {
7506 nested_targs = ct.TypeArguments.Clone ();
7507 nested_targs.Add (targs);
7508 } else {
7509 nested_targs = ct.TypeArguments;
7512 ct = new GenericTypeExpr (member_lookup.Type, nested_targs, loc);
7514 return ct.ResolveAsTypeStep (ec, false);
7517 return member_lookup;
7520 MemberExpr me = (MemberExpr) member_lookup;
7521 me = me.ResolveMemberAccess (ec, expr_resolved, loc, original);
7522 if (me == null)
7523 return null;
7525 if (targs != null) {
7526 me.SetTypeArguments (ec, targs);
7529 if (original != null && !TypeManager.IsValueType (expr_type)) {
7530 if (me.IsInstance) {
7531 LocalVariableReference var = expr_resolved as LocalVariableReference;
7532 if (var != null && !var.VerifyAssigned (ec))
7533 return null;
7537 // The following DoResolve/DoResolveLValue will do the definite assignment
7538 // check.
7540 if (right_side != null)
7541 return me.DoResolveLValue (ec, right_side);
7542 else
7543 return me.DoResolve (ec);
7546 public override Expression DoResolve (ResolveContext ec)
7548 return DoResolve (ec, null);
7551 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
7553 return DoResolve (ec, right_side);
7556 public override FullNamedExpression ResolveAsTypeStep (IMemberContext ec, bool silent)
7558 return ResolveNamespaceOrType (ec, silent);
7561 public FullNamedExpression ResolveNamespaceOrType (IMemberContext rc, bool silent)
7563 FullNamedExpression expr_resolved = expr.ResolveAsTypeStep (rc, silent);
7565 if (expr_resolved == null)
7566 return null;
7568 string LookupIdentifier = MemberName.MakeName (Name, targs);
7570 Namespace ns = expr_resolved as Namespace;
7571 if (ns != null) {
7572 FullNamedExpression retval = ns.Lookup (rc.Compiler, LookupIdentifier, loc);
7574 if (retval == null && !silent)
7575 ns.Error_NamespaceDoesNotExist (loc, LookupIdentifier, rc.Compiler.Report);
7576 else if (targs != null)
7577 retval = new GenericTypeExpr (retval.Type, targs, loc).ResolveAsTypeStep (rc, silent);
7579 return retval;
7582 TypeExpr tnew_expr = expr_resolved.ResolveAsTypeTerminal (rc, false);
7583 if (tnew_expr == null)
7584 return null;
7586 Type expr_type = tnew_expr.Type;
7587 if (TypeManager.IsGenericParameter (expr_type)) {
7588 rc.Compiler.Report.Error (704, loc, "A nested type cannot be specified through a type parameter `{0}'",
7589 tnew_expr.GetSignatureForError ());
7590 return null;
7593 Expression member_lookup = MemberLookup (rc.Compiler,
7594 rc.CurrentType, expr_type, expr_type, LookupIdentifier,
7595 MemberTypes.NestedType, BindingFlags.Public | BindingFlags.NonPublic, loc);
7596 if (member_lookup == null) {
7597 if (silent)
7598 return null;
7600 Error_IdentifierNotFound (rc, expr_resolved, LookupIdentifier);
7601 return null;
7604 TypeExpr texpr = member_lookup.ResolveAsTypeTerminal (rc, false);
7605 if (texpr == null)
7606 return null;
7608 TypeArguments the_args = targs;
7609 Type declaring_type = texpr.Type.DeclaringType;
7610 if (TypeManager.HasGenericArguments (declaring_type) && !TypeManager.IsGenericTypeDefinition (expr_type)) {
7611 while (!TypeManager.IsEqual (TypeManager.DropGenericTypeArguments (expr_type), declaring_type)) {
7612 expr_type = expr_type.BaseType;
7615 TypeArguments new_args = new TypeArguments ();
7616 foreach (Type decl in TypeManager.GetTypeArguments (expr_type))
7617 new_args.Add (new TypeExpression (TypeManager.TypeToCoreType (decl), loc));
7619 if (targs != null)
7620 new_args.Add (targs);
7622 the_args = new_args;
7625 if (the_args != null) {
7626 GenericTypeExpr ctype = new GenericTypeExpr (texpr.Type, the_args, loc);
7627 return ctype.ResolveAsTypeStep (rc, false);
7630 return texpr;
7633 protected virtual void Error_IdentifierNotFound (IMemberContext rc, FullNamedExpression expr_type, string identifier)
7635 Expression member_lookup = MemberLookup (rc.Compiler,
7636 rc.CurrentType, expr_type.Type, expr_type.Type, SimpleName.RemoveGenericArity (identifier),
7637 MemberTypes.NestedType, BindingFlags.Public | BindingFlags.NonPublic, loc);
7639 if (member_lookup != null) {
7640 expr_type = member_lookup.ResolveAsTypeTerminal (rc, false);
7641 if (expr_type == null)
7642 return;
7644 Namespace.Error_TypeArgumentsCannotBeUsed (expr_type, loc);
7645 return;
7648 member_lookup = MemberLookup (rc.Compiler,
7649 rc.CurrentType, expr_type.Type, expr_type.Type, identifier,
7650 MemberTypes.All, BindingFlags.Public | BindingFlags.NonPublic, loc);
7652 if (member_lookup == null) {
7653 rc.Compiler.Report.Error (426, loc, "The nested type `{0}' does not exist in the type `{1}'",
7654 Name, expr_type.GetSignatureForError ());
7655 } else {
7656 // TODO: Report.SymbolRelatedToPreviousError
7657 member_lookup.Error_UnexpectedKind (rc.Compiler.Report, null, "type", loc);
7661 protected override void Error_TypeDoesNotContainDefinition (ResolveContext ec, Type type, string name)
7663 if (RootContext.Version > LanguageVersion.ISO_2 &&
7664 ((expr.eclass & (ExprClass.Value | ExprClass.Variable)) != 0)) {
7665 ec.Report.Error (1061, loc, "Type `{0}' does not contain a definition for `{1}' and no " +
7666 "extension method `{1}' of type `{0}' could be found " +
7667 "(are you missing a using directive or an assembly reference?)",
7668 TypeManager.CSharpName (type), name);
7669 return;
7672 base.Error_TypeDoesNotContainDefinition (ec, type, name);
7675 public override string GetSignatureForError ()
7677 return expr.GetSignatureForError () + "." + base.GetSignatureForError ();
7680 public Expression Left {
7681 get {
7682 return expr;
7686 protected override void CloneTo (CloneContext clonectx, Expression t)
7688 MemberAccess target = (MemberAccess) t;
7690 target.expr = expr.Clone (clonectx);
7694 /// <summary>
7695 /// Implements checked expressions
7696 /// </summary>
7697 public class CheckedExpr : Expression {
7699 public Expression Expr;
7701 public CheckedExpr (Expression e, Location l)
7703 Expr = e;
7704 loc = l;
7707 public override Expression CreateExpressionTree (ResolveContext ec)
7709 using (ec.With (ResolveContext.Options.AllCheckStateFlags, true))
7710 return Expr.CreateExpressionTree (ec);
7713 public override Expression DoResolve (ResolveContext ec)
7715 using (ec.With (ResolveContext.Options.AllCheckStateFlags, true))
7716 Expr = Expr.Resolve (ec);
7718 if (Expr == null)
7719 return null;
7721 if (Expr is Constant || Expr is MethodGroupExpr || Expr is AnonymousMethodExpression || Expr is DefaultValueExpression)
7722 return Expr;
7724 eclass = Expr.eclass;
7725 type = Expr.Type;
7726 return this;
7729 public override void Emit (EmitContext ec)
7731 using (ec.With (EmitContext.Options.AllCheckStateFlags, true))
7732 Expr.Emit (ec);
7735 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
7737 using (ec.With (EmitContext.Options.AllCheckStateFlags, true))
7738 Expr.EmitBranchable (ec, target, on_true);
7741 #if NET_4_0
7742 public override SLE.Expression MakeExpression (BuilderContext ctx)
7744 using (ctx.With (BuilderContext.Options.AllCheckStateFlags, true)) {
7745 return Expr.MakeExpression (ctx);
7748 #endif
7750 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
7752 Expr.MutateHoistedGenericType (storey);
7755 protected override void CloneTo (CloneContext clonectx, Expression t)
7757 CheckedExpr target = (CheckedExpr) t;
7759 target.Expr = Expr.Clone (clonectx);
7763 /// <summary>
7764 /// Implements the unchecked expression
7765 /// </summary>
7766 public class UnCheckedExpr : Expression {
7768 public Expression Expr;
7770 public UnCheckedExpr (Expression e, Location l)
7772 Expr = e;
7773 loc = l;
7776 public override Expression CreateExpressionTree (ResolveContext ec)
7778 using (ec.With (ResolveContext.Options.AllCheckStateFlags, false))
7779 return Expr.CreateExpressionTree (ec);
7782 public override Expression DoResolve (ResolveContext ec)
7784 using (ec.With (ResolveContext.Options.AllCheckStateFlags, false))
7785 Expr = Expr.Resolve (ec);
7787 if (Expr == null)
7788 return null;
7790 if (Expr is Constant || Expr is MethodGroupExpr || Expr is AnonymousMethodExpression || Expr is DefaultValueExpression)
7791 return Expr;
7793 eclass = Expr.eclass;
7794 type = Expr.Type;
7795 return this;
7798 public override void Emit (EmitContext ec)
7800 using (ec.With (EmitContext.Options.AllCheckStateFlags, false))
7801 Expr.Emit (ec);
7804 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
7806 using (ec.With (EmitContext.Options.AllCheckStateFlags, false))
7807 Expr.EmitBranchable (ec, target, on_true);
7810 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
7812 Expr.MutateHoistedGenericType (storey);
7815 protected override void CloneTo (CloneContext clonectx, Expression t)
7817 UnCheckedExpr target = (UnCheckedExpr) t;
7819 target.Expr = Expr.Clone (clonectx);
7823 /// <summary>
7824 /// An Element Access expression.
7826 /// During semantic analysis these are transformed into
7827 /// IndexerAccess, ArrayAccess or a PointerArithmetic.
7828 /// </summary>
7829 public class ElementAccess : Expression {
7830 public Arguments Arguments;
7831 public Expression Expr;
7833 public ElementAccess (Expression e, Arguments args)
7835 Expr = e;
7836 loc = e.Location;
7837 this.Arguments = args;
7840 public override Expression CreateExpressionTree (ResolveContext ec)
7842 Arguments args = Arguments.CreateForExpressionTree (ec, Arguments,
7843 Expr.CreateExpressionTree (ec));
7845 return CreateExpressionFactoryCall (ec, "ArrayIndex", args);
7848 Expression MakePointerAccess (ResolveContext ec, Type t)
7850 if (Arguments.Count != 1){
7851 ec.Report.Error (196, loc, "A pointer must be indexed by only one value");
7852 return null;
7855 if (Arguments [0] is NamedArgument)
7856 Error_NamedArgument ((NamedArgument) Arguments[0], ec.Report);
7858 Expression p = new PointerArithmetic (Binary.Operator.Addition, Expr, Arguments [0].Expr.Resolve (ec), t, loc);
7859 return new Indirection (p, loc).Resolve (ec);
7862 public override Expression DoResolve (ResolveContext ec)
7864 Expr = Expr.Resolve (ec);
7865 if (Expr == null)
7866 return null;
7869 // We perform some simple tests, and then to "split" the emit and store
7870 // code we create an instance of a different class, and return that.
7872 // I am experimenting with this pattern.
7874 Type t = Expr.Type;
7876 if (t == TypeManager.array_type){
7877 ec.Report.Error (21, loc, "Cannot apply indexing with [] to an expression of type `System.Array'");
7878 return null;
7881 if (t.IsArray)
7882 return (new ArrayAccess (this, loc)).Resolve (ec);
7883 if (t.IsPointer)
7884 return MakePointerAccess (ec, t);
7886 FieldExpr fe = Expr as FieldExpr;
7887 if (fe != null) {
7888 IFixedBuffer ff = AttributeTester.GetFixedBuffer (fe.FieldInfo);
7889 if (ff != null) {
7890 return MakePointerAccess (ec, ff.ElementType);
7893 return (new IndexerAccess (this, loc)).Resolve (ec);
7896 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
7898 Expr = Expr.Resolve (ec);
7899 if (Expr == null)
7900 return null;
7902 type = Expr.Type;
7903 if (type.IsArray)
7904 return (new ArrayAccess (this, loc)).DoResolveLValue (ec, right_side);
7906 if (type.IsPointer)
7907 return MakePointerAccess (ec, type);
7909 if (Expr.eclass != ExprClass.Variable && TypeManager.IsStruct (type))
7910 Error_CannotModifyIntermediateExpressionValue (ec);
7912 return (new IndexerAccess (this, loc)).DoResolveLValue (ec, right_side);
7915 public override void Emit (EmitContext ec)
7917 throw new Exception ("Should never be reached");
7920 public static void Error_NamedArgument (NamedArgument na, Report Report)
7922 Report.Error (1742, na.Name.Location, "An element access expression cannot use named argument");
7925 public override string GetSignatureForError ()
7927 return Expr.GetSignatureForError ();
7930 protected override void CloneTo (CloneContext clonectx, Expression t)
7932 ElementAccess target = (ElementAccess) t;
7934 target.Expr = Expr.Clone (clonectx);
7935 if (Arguments != null)
7936 target.Arguments = Arguments.Clone (clonectx);
7940 /// <summary>
7941 /// Implements array access
7942 /// </summary>
7943 public class ArrayAccess : Expression, IAssignMethod, IMemoryLocation {
7945 // Points to our "data" repository
7947 ElementAccess ea;
7949 LocalTemporary temp;
7951 bool prepared;
7953 public ArrayAccess (ElementAccess ea_data, Location l)
7955 ea = ea_data;
7956 loc = l;
7959 public override Expression CreateExpressionTree (ResolveContext ec)
7961 return ea.CreateExpressionTree (ec);
7964 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
7966 return DoResolve (ec);
7969 public override Expression DoResolve (ResolveContext ec)
7971 #if false
7972 ExprClass eclass = ea.Expr.eclass;
7974 // As long as the type is valid
7975 if (!(eclass == ExprClass.Variable || eclass == ExprClass.PropertyAccess ||
7976 eclass == ExprClass.Value)) {
7977 ea.Expr.Error_UnexpectedKind ("variable or value");
7978 return null;
7980 #endif
7982 if (eclass != ExprClass.Invalid)
7983 return this;
7985 // dynamic is used per argument in ConvertExpressionToArrayIndex case
7986 bool dynamic;
7987 ea.Arguments.Resolve (ec, out dynamic);
7989 Type t = ea.Expr.Type;
7990 int rank = ea.Arguments.Count;
7991 if (t.GetArrayRank () != rank) {
7992 ec.Report.Error (22, ea.Location, "Wrong number of indexes `{0}' inside [], expected `{1}'",
7993 ea.Arguments.Count.ToString (), t.GetArrayRank ().ToString ());
7994 return null;
7997 type = TypeManager.GetElementType (t);
7998 if (type.IsPointer && !ec.IsUnsafe) {
7999 UnsafeError (ec, ea.Location);
8002 foreach (Argument a in ea.Arguments) {
8003 if (a is NamedArgument)
8004 ElementAccess.Error_NamedArgument ((NamedArgument) a, ec.Report);
8006 a.Expr = ConvertExpressionToArrayIndex (ec, a.Expr);
8009 eclass = ExprClass.Variable;
8011 return this;
8014 /// <summary>
8015 /// Emits the right opcode to load an object of Type `t'
8016 /// from an array of T
8017 /// </summary>
8018 void EmitLoadOpcode (ILGenerator ig, Type type, int rank)
8020 if (rank > 1) {
8021 MethodInfo get = FetchGetMethod ();
8022 ig.Emit (OpCodes.Call, get);
8023 return;
8026 if (type == TypeManager.byte_type || type == TypeManager.bool_type)
8027 ig.Emit (OpCodes.Ldelem_U1);
8028 else if (type == TypeManager.sbyte_type)
8029 ig.Emit (OpCodes.Ldelem_I1);
8030 else if (type == TypeManager.short_type)
8031 ig.Emit (OpCodes.Ldelem_I2);
8032 else if (type == TypeManager.ushort_type || type == TypeManager.char_type)
8033 ig.Emit (OpCodes.Ldelem_U2);
8034 else if (type == TypeManager.int32_type)
8035 ig.Emit (OpCodes.Ldelem_I4);
8036 else if (type == TypeManager.uint32_type)
8037 ig.Emit (OpCodes.Ldelem_U4);
8038 else if (type == TypeManager.uint64_type)
8039 ig.Emit (OpCodes.Ldelem_I8);
8040 else if (type == TypeManager.int64_type)
8041 ig.Emit (OpCodes.Ldelem_I8);
8042 else if (type == TypeManager.float_type)
8043 ig.Emit (OpCodes.Ldelem_R4);
8044 else if (type == TypeManager.double_type)
8045 ig.Emit (OpCodes.Ldelem_R8);
8046 else if (type == TypeManager.intptr_type)
8047 ig.Emit (OpCodes.Ldelem_I);
8048 else if (TypeManager.IsEnumType (type)){
8049 EmitLoadOpcode (ig, TypeManager.GetEnumUnderlyingType (type), rank);
8050 } else if (TypeManager.IsStruct (type)){
8051 ig.Emit (OpCodes.Ldelema, type);
8052 ig.Emit (OpCodes.Ldobj, type);
8053 #if GMCS_SOURCE
8054 } else if (type.IsGenericParameter) {
8055 ig.Emit (OpCodes.Ldelem, type);
8056 #endif
8057 } else if (type.IsPointer)
8058 ig.Emit (OpCodes.Ldelem_I);
8059 else
8060 ig.Emit (OpCodes.Ldelem_Ref);
8063 protected override void Error_NegativeArrayIndex (ResolveContext ec, Location loc)
8065 ec.Report.Warning (251, 2, loc, "Indexing an array with a negative index (array indices always start at zero)");
8068 /// <summary>
8069 /// Returns the right opcode to store an object of Type `t'
8070 /// from an array of T.
8071 /// </summary>
8072 static public OpCode GetStoreOpcode (Type t, out bool is_stobj, out bool has_type_arg)
8074 has_type_arg = false; is_stobj = false;
8075 t = TypeManager.TypeToCoreType (t);
8076 if (TypeManager.IsEnumType (t))
8077 t = TypeManager.GetEnumUnderlyingType (t);
8078 if (t == TypeManager.byte_type || t == TypeManager.sbyte_type ||
8079 t == TypeManager.bool_type)
8080 return OpCodes.Stelem_I1;
8081 else if (t == TypeManager.short_type || t == TypeManager.ushort_type ||
8082 t == TypeManager.char_type)
8083 return OpCodes.Stelem_I2;
8084 else if (t == TypeManager.int32_type || t == TypeManager.uint32_type)
8085 return OpCodes.Stelem_I4;
8086 else if (t == TypeManager.int64_type || t == TypeManager.uint64_type)
8087 return OpCodes.Stelem_I8;
8088 else if (t == TypeManager.float_type)
8089 return OpCodes.Stelem_R4;
8090 else if (t == TypeManager.double_type)
8091 return OpCodes.Stelem_R8;
8092 else if (t == TypeManager.intptr_type) {
8093 has_type_arg = true;
8094 is_stobj = true;
8095 return OpCodes.Stobj;
8096 } else if (TypeManager.IsStruct (t)) {
8097 has_type_arg = true;
8098 is_stobj = true;
8099 return OpCodes.Stobj;
8100 #if GMCS_SOURCE
8101 } else if (t.IsGenericParameter) {
8102 has_type_arg = true;
8103 return OpCodes.Stelem;
8104 #endif
8106 } else if (t.IsPointer)
8107 return OpCodes.Stelem_I;
8108 else
8109 return OpCodes.Stelem_Ref;
8112 MethodInfo FetchGetMethod ()
8114 ModuleBuilder mb = RootContext.ToplevelTypes.Builder;
8115 int arg_count = ea.Arguments.Count;
8116 Type [] args = new Type [arg_count];
8117 MethodInfo get;
8119 for (int i = 0; i < arg_count; i++){
8120 //args [i++] = a.Type;
8121 args [i] = TypeManager.int32_type;
8124 get = mb.GetArrayMethod (
8125 ea.Expr.Type, "Get",
8126 CallingConventions.HasThis |
8127 CallingConventions.Standard,
8128 type, args);
8129 return get;
8133 MethodInfo FetchAddressMethod ()
8135 ModuleBuilder mb = RootContext.ToplevelTypes.Builder;
8136 int arg_count = ea.Arguments.Count;
8137 Type [] args = new Type [arg_count];
8138 MethodInfo address;
8139 Type ret_type;
8141 ret_type = TypeManager.GetReferenceType (type);
8143 for (int i = 0; i < arg_count; i++){
8144 //args [i++] = a.Type;
8145 args [i] = TypeManager.int32_type;
8148 address = mb.GetArrayMethod (
8149 ea.Expr.Type, "Address",
8150 CallingConventions.HasThis |
8151 CallingConventions.Standard,
8152 ret_type, args);
8154 return address;
8158 // Load the array arguments into the stack.
8160 void LoadArrayAndArguments (EmitContext ec)
8162 ea.Expr.Emit (ec);
8164 for (int i = 0; i < ea.Arguments.Count; ++i) {
8165 ea.Arguments [i].Emit (ec);
8169 public void Emit (EmitContext ec, bool leave_copy)
8171 int rank = ea.Expr.Type.GetArrayRank ();
8172 ILGenerator ig = ec.ig;
8174 if (prepared) {
8175 LoadFromPtr (ig, this.type);
8176 } else {
8177 LoadArrayAndArguments (ec);
8178 EmitLoadOpcode (ig, type, rank);
8181 if (leave_copy) {
8182 ig.Emit (OpCodes.Dup);
8183 temp = new LocalTemporary (this.type);
8184 temp.Store (ec);
8188 public override void Emit (EmitContext ec)
8190 Emit (ec, false);
8193 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
8195 int rank = ea.Expr.Type.GetArrayRank ();
8196 ILGenerator ig = ec.ig;
8197 Type t = source.Type;
8198 prepared = prepare_for_load;
8200 if (prepared) {
8201 AddressOf (ec, AddressOp.LoadStore);
8202 ec.ig.Emit (OpCodes.Dup);
8203 } else {
8204 LoadArrayAndArguments (ec);
8207 if (rank == 1) {
8208 bool is_stobj, has_type_arg;
8209 OpCode op = GetStoreOpcode (t, out is_stobj, out has_type_arg);
8211 if (!prepared) {
8213 // The stobj opcode used by value types will need
8214 // an address on the stack, not really an array/array
8215 // pair
8217 if (is_stobj)
8218 ig.Emit (OpCodes.Ldelema, t);
8221 source.Emit (ec);
8222 if (leave_copy) {
8223 ec.ig.Emit (OpCodes.Dup);
8224 temp = new LocalTemporary (this.type);
8225 temp.Store (ec);
8228 if (prepared)
8229 StoreFromPtr (ig, t);
8230 else if (is_stobj)
8231 ig.Emit (OpCodes.Stobj, t);
8232 else if (has_type_arg)
8233 ig.Emit (op, t);
8234 else
8235 ig.Emit (op);
8236 } else {
8237 source.Emit (ec);
8238 if (leave_copy) {
8239 ec.ig.Emit (OpCodes.Dup);
8240 temp = new LocalTemporary (this.type);
8241 temp.Store (ec);
8244 if (prepared) {
8245 StoreFromPtr (ig, t);
8246 } else {
8247 int arg_count = ea.Arguments.Count;
8248 Type [] args = new Type [arg_count + 1];
8249 for (int i = 0; i < arg_count; i++) {
8250 //args [i++] = a.Type;
8251 args [i] = TypeManager.int32_type;
8253 args [arg_count] = type;
8255 MethodInfo set = RootContext.ToplevelTypes.Builder.GetArrayMethod (
8256 ea.Expr.Type, "Set",
8257 CallingConventions.HasThis |
8258 CallingConventions.Standard,
8259 TypeManager.void_type, args);
8261 ig.Emit (OpCodes.Call, set);
8265 if (temp != null) {
8266 temp.Emit (ec);
8267 temp.Release (ec);
8271 public void EmitNew (EmitContext ec, New source, bool leave_copy)
8273 if (!source.Emit (ec, this)) {
8274 if (leave_copy)
8275 throw new NotImplementedException ();
8277 return;
8280 throw new NotImplementedException ();
8283 public void AddressOf (EmitContext ec, AddressOp mode)
8285 int rank = ea.Expr.Type.GetArrayRank ();
8286 ILGenerator ig = ec.ig;
8288 LoadArrayAndArguments (ec);
8290 if (rank == 1){
8291 ig.Emit (OpCodes.Ldelema, type);
8292 } else {
8293 MethodInfo address = FetchAddressMethod ();
8294 ig.Emit (OpCodes.Call, address);
8298 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
8300 type = storey.MutateType (type);
8301 ea.Expr.Type = storey.MutateType (ea.Expr.Type);
8305 /// <summary>
8306 /// Expressions that represent an indexer call.
8307 /// </summary>
8308 public class IndexerAccess : Expression, IAssignMethod
8310 class IndexerMethodGroupExpr : MethodGroupExpr
8312 public IndexerMethodGroupExpr (Indexers indexers, Location loc)
8313 : base (null, loc)
8315 Methods = (MethodBase []) indexers.Methods.ToArray (typeof (MethodBase));
8318 public override string Name {
8319 get {
8320 return "this";
8324 protected override int GetApplicableParametersCount (MethodBase method, AParametersCollection parameters)
8327 // Here is the trick, decrease number of arguments by 1 when only
8328 // available property method is setter. This makes overload resolution
8329 // work correctly for indexers.
8332 if (method.Name [0] == 'g')
8333 return parameters.Count;
8335 return parameters.Count - 1;
8339 class Indexers
8341 // Contains either property getter or setter
8342 public ArrayList Methods;
8343 public ArrayList Properties;
8345 Indexers ()
8349 void Append (Type caller_type, MemberInfo [] mi)
8351 if (mi == null)
8352 return;
8354 foreach (PropertyInfo property in mi) {
8355 MethodInfo accessor = property.GetGetMethod (true);
8356 if (accessor == null)
8357 accessor = property.GetSetMethod (true);
8359 if (Methods == null) {
8360 Methods = new ArrayList ();
8361 Properties = new ArrayList ();
8364 Methods.Add (accessor);
8365 Properties.Add (property);
8369 static MemberInfo [] GetIndexersForTypeOrInterface (Type caller_type, Type lookup_type)
8371 string p_name = TypeManager.IndexerPropertyName (lookup_type);
8373 return TypeManager.MemberLookup (
8374 caller_type, caller_type, lookup_type, MemberTypes.Property,
8375 BindingFlags.Public | BindingFlags.Instance |
8376 BindingFlags.DeclaredOnly, p_name, null);
8379 public static Indexers GetIndexersForType (Type caller_type, Type lookup_type)
8381 Indexers ix = new Indexers ();
8383 if (TypeManager.IsGenericParameter (lookup_type)) {
8384 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (lookup_type);
8385 if (gc == null)
8386 return ix;
8388 if (gc.HasClassConstraint) {
8389 Type class_contraint = gc.ClassConstraint;
8390 while (class_contraint != TypeManager.object_type && class_contraint != null) {
8391 ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, class_contraint));
8392 class_contraint = class_contraint.BaseType;
8396 Type[] ifaces = gc.InterfaceConstraints;
8397 foreach (Type itype in ifaces)
8398 ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, itype));
8400 return ix;
8403 Type copy = lookup_type;
8404 while (copy != TypeManager.object_type && copy != null){
8405 ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, copy));
8406 copy = copy.BaseType;
8409 if (lookup_type.IsInterface) {
8410 Type [] ifaces = TypeManager.GetInterfaces (lookup_type);
8411 if (ifaces != null) {
8412 foreach (Type itype in ifaces)
8413 ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, itype));
8417 return ix;
8422 // Points to our "data" repository
8424 MethodInfo get, set;
8425 bool is_base_indexer;
8426 bool prepared;
8427 LocalTemporary temp;
8428 LocalTemporary prepared_value;
8429 Expression set_expr;
8431 protected Type indexer_type;
8432 protected Type current_type;
8433 protected Expression instance_expr;
8434 protected Arguments arguments;
8436 public IndexerAccess (ElementAccess ea, Location loc)
8437 : this (ea.Expr, false, loc)
8439 this.arguments = ea.Arguments;
8442 protected IndexerAccess (Expression instance_expr, bool is_base_indexer,
8443 Location loc)
8445 this.instance_expr = instance_expr;
8446 this.is_base_indexer = is_base_indexer;
8447 this.eclass = ExprClass.Value;
8448 this.loc = loc;
8451 static string GetAccessorName (bool isSet)
8453 return isSet ? "set" : "get";
8456 public override Expression CreateExpressionTree (ResolveContext ec)
8458 Arguments args = Arguments.CreateForExpressionTree (ec, arguments,
8459 instance_expr.CreateExpressionTree (ec),
8460 new TypeOfMethod (get, loc));
8462 return CreateExpressionFactoryCall (ec, "Call", args);
8465 protected virtual void CommonResolve (ResolveContext ec)
8467 indexer_type = instance_expr.Type;
8468 current_type = ec.CurrentType;
8471 public override Expression DoResolve (ResolveContext ec)
8473 return ResolveAccessor (ec, null);
8476 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
8478 if (right_side == EmptyExpression.OutAccess) {
8479 ec.Report.Error (206, loc,
8480 "A property or indexer may not be passed as an out or ref parameter");
8481 return null;
8484 // if the indexer returns a value type, and we try to set a field in it
8485 if (right_side == EmptyExpression.LValueMemberAccess || right_side == EmptyExpression.LValueMemberOutAccess) {
8486 Error_CannotModifyIntermediateExpressionValue (ec);
8489 return ResolveAccessor (ec, right_side);
8492 Expression ResolveAccessor (ResolveContext ec, Expression right_side)
8494 CommonResolve (ec);
8496 bool dynamic;
8497 arguments.Resolve (ec, out dynamic);
8498 if (dynamic || TypeManager.IsDynamicType (indexer_type)) {
8499 int additional = right_side == null ? 1 : 2;
8500 Arguments args = new Arguments (arguments.Count + additional);
8501 if (is_base_indexer) {
8502 ec.Report.Error (1972, loc, "The indexer base access cannot be dynamically dispatched. Consider casting the dynamic arguments or eliminating the base access");
8503 } else {
8504 args.Add (new Argument (instance_expr));
8506 args.AddRange (arguments);
8507 if (right_side != null)
8508 args.Add (new Argument (right_side));
8510 return new DynamicIndexBinder (right_side != null, args, loc).Resolve (ec);
8513 Indexers ilist = Indexers.GetIndexersForType (current_type, indexer_type);
8514 if (ilist.Methods == null) {
8515 ec.Report.Error (21, loc, "Cannot apply indexing with [] to an expression of type `{0}'",
8516 TypeManager.CSharpName (indexer_type));
8517 return null;
8520 MethodGroupExpr mg = new IndexerMethodGroupExpr (ilist, loc);
8521 mg = mg.OverloadResolve (ec, ref arguments, false, loc);
8522 if (mg == null)
8523 return null;
8525 MethodInfo mi = (MethodInfo) mg;
8526 PropertyInfo pi = null;
8527 for (int i = 0; i < ilist.Methods.Count; ++i) {
8528 if (ilist.Methods [i] == mi) {
8529 pi = (PropertyInfo) ilist.Properties [i];
8530 break;
8534 type = TypeManager.TypeToCoreType (pi.PropertyType);
8535 if (type.IsPointer && !ec.IsUnsafe)
8536 UnsafeError (ec, loc);
8538 MethodInfo accessor;
8539 if (right_side == null) {
8540 accessor = get = pi.GetGetMethod (true);
8541 } else {
8542 accessor = set = pi.GetSetMethod (true);
8543 if (accessor == null && pi.GetGetMethod (true) != null) {
8544 ec.Report.SymbolRelatedToPreviousError (pi);
8545 ec.Report.Error (200, loc, "The read only property or indexer `{0}' cannot be assigned to",
8546 TypeManager.GetFullNameSignature (pi));
8547 return null;
8550 set_expr = Convert.ImplicitConversion (ec, right_side, type, loc);
8553 if (accessor == null) {
8554 ec.Report.SymbolRelatedToPreviousError (pi);
8555 ec.Report.Error (154, loc, "The property or indexer `{0}' cannot be used in this context because it lacks a `{1}' accessor",
8556 TypeManager.GetFullNameSignature (pi), GetAccessorName (right_side != null));
8557 return null;
8561 // Only base will allow this invocation to happen.
8563 if (accessor.IsAbstract && this is BaseIndexerAccess) {
8564 Error_CannotCallAbstractBase (ec, TypeManager.GetFullNameSignature (pi));
8567 bool must_do_cs1540_check;
8568 if (!IsAccessorAccessible (ec.CurrentType, accessor, out must_do_cs1540_check)) {
8569 if (set == null)
8570 set = pi.GetSetMethod (true);
8571 else
8572 get = pi.GetGetMethod (true);
8574 if (set != null && get != null &&
8575 (set.Attributes & MethodAttributes.MemberAccessMask) != (get.Attributes & MethodAttributes.MemberAccessMask)) {
8576 ec.Report.SymbolRelatedToPreviousError (accessor);
8577 ec.Report.Error (271, loc, "The property or indexer `{0}' cannot be used in this context because a `{1}' accessor is inaccessible",
8578 TypeManager.GetFullNameSignature (pi), GetAccessorName (right_side != null));
8579 } else {
8580 ec.Report.SymbolRelatedToPreviousError (pi);
8581 ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (pi), ec.Report);
8585 instance_expr.CheckMarshalByRefAccess (ec);
8586 eclass = ExprClass.IndexerAccess;
8587 return this;
8590 public void Emit (EmitContext ec, bool leave_copy)
8592 if (prepared) {
8593 prepared_value.Emit (ec);
8594 } else {
8595 Invocation.EmitCall (ec, is_base_indexer, instance_expr, get,
8596 arguments, loc, false, false);
8599 if (leave_copy) {
8600 ec.ig.Emit (OpCodes.Dup);
8601 temp = new LocalTemporary (Type);
8602 temp.Store (ec);
8607 // source is ignored, because we already have a copy of it from the
8608 // LValue resolution and we have already constructed a pre-cached
8609 // version of the arguments (ea.set_arguments);
8611 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
8613 prepared = prepare_for_load;
8614 Expression value = set_expr;
8616 if (prepared) {
8617 Invocation.EmitCall (ec, is_base_indexer, instance_expr, get,
8618 arguments, loc, true, false);
8620 prepared_value = new LocalTemporary (type);
8621 prepared_value.Store (ec);
8622 source.Emit (ec);
8623 prepared_value.Release (ec);
8625 if (leave_copy) {
8626 ec.ig.Emit (OpCodes.Dup);
8627 temp = new LocalTemporary (Type);
8628 temp.Store (ec);
8630 } else if (leave_copy) {
8631 temp = new LocalTemporary (Type);
8632 source.Emit (ec);
8633 temp.Store (ec);
8634 value = temp;
8637 if (!prepared)
8638 arguments.Add (new Argument (value));
8640 Invocation.EmitCall (ec, is_base_indexer, instance_expr, set, arguments, loc, false, prepared);
8642 if (temp != null) {
8643 temp.Emit (ec);
8644 temp.Release (ec);
8648 public override void Emit (EmitContext ec)
8650 Emit (ec, false);
8653 public override string GetSignatureForError ()
8655 return TypeManager.CSharpSignature (get != null ? get : set, false);
8658 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
8660 if (get != null)
8661 get = storey.MutateGenericMethod (get);
8662 if (set != null)
8663 set = storey.MutateGenericMethod (set);
8665 instance_expr.MutateHoistedGenericType (storey);
8666 if (arguments != null)
8667 arguments.MutateHoistedGenericType (storey);
8669 type = storey.MutateType (type);
8672 protected override void CloneTo (CloneContext clonectx, Expression t)
8674 IndexerAccess target = (IndexerAccess) t;
8676 if (arguments != null)
8677 target.arguments = arguments.Clone (clonectx);
8679 if (instance_expr != null)
8680 target.instance_expr = instance_expr.Clone (clonectx);
8684 /// <summary>
8685 /// The base operator for method names
8686 /// </summary>
8687 public class BaseAccess : Expression {
8688 public readonly string Identifier;
8689 TypeArguments args;
8691 public BaseAccess (string member, Location l)
8693 this.Identifier = member;
8694 loc = l;
8697 public BaseAccess (string member, TypeArguments args, Location l)
8698 : this (member, l)
8700 this.args = args;
8703 public override Expression CreateExpressionTree (ResolveContext ec)
8705 throw new NotSupportedException ("ET");
8708 public override Expression DoResolve (ResolveContext ec)
8710 Expression c = CommonResolve (ec);
8712 if (c == null)
8713 return null;
8716 // MethodGroups use this opportunity to flag an error on lacking ()
8718 if (!(c is MethodGroupExpr))
8719 return c.Resolve (ec);
8720 return c;
8723 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
8725 Expression c = CommonResolve (ec);
8727 if (c == null)
8728 return null;
8731 // MethodGroups use this opportunity to flag an error on lacking ()
8733 if (! (c is MethodGroupExpr))
8734 return c.DoResolveLValue (ec, right_side);
8736 return c;
8739 Expression CommonResolve (ResolveContext ec)
8741 Expression member_lookup;
8742 Type current_type = ec.CurrentType;
8743 Type base_type = current_type.BaseType;
8745 if (!This.IsThisAvailable (ec)) {
8746 if (ec.IsStatic) {
8747 ec.Report.Error (1511, loc, "Keyword `base' is not available in a static method");
8748 } else {
8749 ec.Report.Error (1512, loc, "Keyword `base' is not available in the current context");
8751 return null;
8754 member_lookup = MemberLookup (ec.Compiler, ec.CurrentType, null, base_type, Identifier,
8755 AllMemberTypes, AllBindingFlags, loc);
8756 if (member_lookup == null) {
8757 Error_MemberLookupFailed (ec, ec.CurrentType, base_type, base_type, Identifier,
8758 null, AllMemberTypes, AllBindingFlags);
8759 return null;
8762 Expression left;
8764 if (ec.IsStatic)
8765 left = new TypeExpression (base_type, loc);
8766 else
8767 left = ec.GetThis (loc);
8769 MemberExpr me = member_lookup as MemberExpr;
8770 if (me == null){
8771 if (member_lookup is TypeExpression){
8772 ec.Report.Error (582, loc, "{0}: Can not reference a type through an expression, try `{1}' instead",
8773 Identifier, member_lookup.GetSignatureForError ());
8774 } else {
8775 ec.Report.Error (582, loc, "{0}: Can not reference a {1} through an expression",
8776 Identifier, member_lookup.ExprClassName);
8779 return null;
8782 me = me.ResolveMemberAccess (ec, left, loc, null);
8783 if (me == null)
8784 return null;
8786 me.IsBase = true;
8787 if (args != null) {
8788 args.Resolve (ec);
8789 me.SetTypeArguments (ec, args);
8792 return me;
8795 public override void Emit (EmitContext ec)
8797 throw new Exception ("Should never be called");
8800 protected override void CloneTo (CloneContext clonectx, Expression t)
8802 BaseAccess target = (BaseAccess) t;
8804 if (args != null)
8805 target.args = args.Clone ();
8809 /// <summary>
8810 /// The base indexer operator
8811 /// </summary>
8812 public class BaseIndexerAccess : IndexerAccess {
8813 public BaseIndexerAccess (Arguments args, Location loc)
8814 : base (null, true, loc)
8816 this.arguments = args;
8819 protected override void CommonResolve (ResolveContext ec)
8821 instance_expr = ec.GetThis (loc);
8823 current_type = ec.CurrentType.BaseType;
8824 indexer_type = current_type;
8827 public override Expression CreateExpressionTree (ResolveContext ec)
8829 MemberExpr.Error_BaseAccessInExpressionTree (ec, loc);
8830 return base.CreateExpressionTree (ec);
8834 /// <summary>
8835 /// This class exists solely to pass the Type around and to be a dummy
8836 /// that can be passed to the conversion functions (this is used by
8837 /// foreach implementation to typecast the object return value from
8838 /// get_Current into the proper type. All code has been generated and
8839 /// we only care about the side effect conversions to be performed
8841 /// This is also now used as a placeholder where a no-action expression
8842 /// is needed (the `New' class).
8843 /// </summary>
8844 public class EmptyExpression : Expression {
8845 public static readonly Expression Null = new EmptyExpression ();
8847 public static readonly EmptyExpression OutAccess = new EmptyExpression ();
8848 public static readonly EmptyExpression LValueMemberAccess = new EmptyExpression ();
8849 public static readonly EmptyExpression LValueMemberOutAccess = new EmptyExpression ();
8850 public static readonly EmptyExpression UnaryAddress = new EmptyExpression ();
8852 static EmptyExpression temp = new EmptyExpression ();
8853 public static EmptyExpression Grab ()
8855 EmptyExpression retval = temp == null ? new EmptyExpression () : temp;
8856 temp = null;
8857 return retval;
8860 public static void Release (EmptyExpression e)
8862 temp = e;
8865 EmptyExpression ()
8867 // FIXME: Don't set to object
8868 type = TypeManager.object_type;
8869 eclass = ExprClass.Value;
8870 loc = Location.Null;
8873 public EmptyExpression (Type t)
8875 type = t;
8876 eclass = ExprClass.Value;
8877 loc = Location.Null;
8880 public override Expression CreateExpressionTree (ResolveContext ec)
8882 throw new NotSupportedException ("ET");
8885 public override Expression DoResolve (ResolveContext ec)
8887 return this;
8890 public override void Emit (EmitContext ec)
8892 // nothing, as we only exist to not do anything.
8895 public override void EmitSideEffect (EmitContext ec)
8900 // This is just because we might want to reuse this bad boy
8901 // instead of creating gazillions of EmptyExpressions.
8902 // (CanImplicitConversion uses it)
8904 public void SetType (Type t)
8906 type = t;
8911 // Empty statement expression
8913 public sealed class EmptyExpressionStatement : ExpressionStatement
8915 public static readonly EmptyExpressionStatement Instance = new EmptyExpressionStatement ();
8917 private EmptyExpressionStatement ()
8919 eclass = ExprClass.Value;
8920 loc = Location.Null;
8923 public override Expression CreateExpressionTree (ResolveContext ec)
8925 return null;
8928 public override void EmitStatement (EmitContext ec)
8930 // Do nothing
8933 public override Expression DoResolve (ResolveContext ec)
8935 type = TypeManager.object_type;
8936 return this;
8939 public override void Emit (EmitContext ec)
8941 // Do nothing
8945 public class UserCast : Expression {
8946 MethodInfo method;
8947 Expression source;
8949 public UserCast (MethodInfo method, Expression source, Location l)
8951 this.method = method;
8952 this.source = source;
8953 type = TypeManager.TypeToCoreType (method.ReturnType);
8954 loc = l;
8957 public Expression Source {
8958 get {
8959 return source;
8963 public override Expression CreateExpressionTree (ResolveContext ec)
8965 Arguments args = new Arguments (3);
8966 args.Add (new Argument (source.CreateExpressionTree (ec)));
8967 args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
8968 args.Add (new Argument (new TypeOfMethod (method, loc)));
8969 return CreateExpressionFactoryCall (ec, "Convert", args);
8972 public override Expression DoResolve (ResolveContext ec)
8974 ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (method);
8975 if (oa != null)
8976 AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc, ec.Report);
8978 eclass = ExprClass.Value;
8979 return this;
8982 public override void Emit (EmitContext ec)
8984 source.Emit (ec);
8985 ec.ig.Emit (OpCodes.Call, method);
8988 public override string GetSignatureForError ()
8990 return TypeManager.CSharpSignature (method);
8993 #if NET_4_0
8994 public override SLE.Expression MakeExpression (BuilderContext ctx)
8996 return SLE.Expression.Convert (source.MakeExpression (ctx), type, method);
8998 #endif
9000 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
9002 source.MutateHoistedGenericType (storey);
9003 method = storey.MutateGenericMethod (method);
9007 // <summary>
9008 // This class is used to "construct" the type during a typecast
9009 // operation. Since the Type.GetType class in .NET can parse
9010 // the type specification, we just use this to construct the type
9011 // one bit at a time.
9012 // </summary>
9013 public class ComposedCast : TypeExpr {
9014 FullNamedExpression left;
9015 string dim;
9017 public ComposedCast (FullNamedExpression left, string dim)
9018 : this (left, dim, left.Location)
9022 public ComposedCast (FullNamedExpression left, string dim, Location l)
9024 this.left = left;
9025 this.dim = dim;
9026 loc = l;
9029 protected override TypeExpr DoResolveAsTypeStep (IMemberContext ec)
9031 TypeExpr lexpr = left.ResolveAsTypeTerminal (ec, false);
9032 if (lexpr == null)
9033 return null;
9035 Type ltype = lexpr.Type;
9036 if ((dim.Length > 0) && (dim [0] == '?')) {
9037 TypeExpr nullable = new Nullable.NullableType (lexpr, loc);
9038 if (dim.Length > 1)
9039 nullable = new ComposedCast (nullable, dim.Substring (1), loc);
9040 return nullable.ResolveAsTypeTerminal (ec, false);
9043 if (dim == "*" && !TypeManager.VerifyUnManaged (ltype, loc))
9044 return null;
9046 if (dim.Length != 0 && dim [0] == '[') {
9047 if (TypeManager.IsSpecialType (ltype)) {
9048 ec.Compiler.Report.Error (611, loc, "Array elements cannot be of type `{0}'", TypeManager.CSharpName (ltype));
9049 return null;
9052 if ((ltype.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute) {
9053 ec.Compiler.Report.SymbolRelatedToPreviousError (ltype);
9054 ec.Compiler.Report.Error (719, loc, "Array elements cannot be of static type `{0}'",
9055 TypeManager.CSharpName (ltype));
9059 if (dim != "")
9060 type = TypeManager.GetConstructedType (ltype, dim);
9061 else
9062 type = ltype;
9064 if (type == null)
9065 throw new InternalErrorException ("Couldn't create computed type " + ltype + dim);
9067 if (type.IsPointer && !ec.IsUnsafe){
9068 UnsafeError (ec.Compiler.Report, loc);
9071 eclass = ExprClass.Type;
9072 return this;
9075 public override string GetSignatureForError ()
9077 return left.GetSignatureForError () + dim;
9080 public override TypeExpr ResolveAsTypeTerminal (IMemberContext ec, bool silent)
9082 return ResolveAsBaseTerminal (ec, silent);
9086 public class FixedBufferPtr : Expression {
9087 Expression array;
9089 public FixedBufferPtr (Expression array, Type array_type, Location l)
9091 this.array = array;
9092 this.loc = l;
9094 type = TypeManager.GetPointerType (array_type);
9095 eclass = ExprClass.Value;
9098 public override Expression CreateExpressionTree (ResolveContext ec)
9100 Error_PointerInsideExpressionTree (ec);
9101 return null;
9104 public override void Emit(EmitContext ec)
9106 array.Emit (ec);
9109 public override Expression DoResolve (ResolveContext ec)
9112 // We are born fully resolved
9114 return this;
9120 // This class is used to represent the address of an array, used
9121 // only by the Fixed statement, this generates "&a [0]" construct
9122 // for fixed (char *pa = a)
9124 public class ArrayPtr : FixedBufferPtr {
9125 Type array_type;
9127 public ArrayPtr (Expression array, Type array_type, Location l):
9128 base (array, array_type, l)
9130 this.array_type = array_type;
9133 public override void Emit (EmitContext ec)
9135 base.Emit (ec);
9137 ILGenerator ig = ec.ig;
9138 IntLiteral.EmitInt (ig, 0);
9139 ig.Emit (OpCodes.Ldelema, array_type);
9144 // Encapsulates a conversion rules required for array indexes
9146 public class ArrayIndexCast : TypeCast
9148 public ArrayIndexCast (Expression expr)
9149 : base (expr, expr.Type)
9151 if (type == TypeManager.int32_type)
9152 throw new ArgumentException ("unnecessary conversion");
9155 public override Expression CreateExpressionTree (ResolveContext ec)
9157 Arguments args = new Arguments (2);
9158 args.Add (new Argument (child.CreateExpressionTree (ec)));
9159 args.Add (new Argument (new TypeOf (new TypeExpression (TypeManager.int32_type, loc), loc)));
9160 return CreateExpressionFactoryCall (ec, "ConvertChecked", args);
9163 public override void Emit (EmitContext ec)
9165 child.Emit (ec);
9167 if (type == TypeManager.uint32_type)
9168 ec.ig.Emit (OpCodes.Conv_U);
9169 else if (type == TypeManager.int64_type)
9170 ec.ig.Emit (OpCodes.Conv_Ovf_I);
9171 else if (type == TypeManager.uint64_type)
9172 ec.ig.Emit (OpCodes.Conv_Ovf_I_Un);
9173 else
9174 throw new InternalErrorException ("Cannot emit cast to unknown array element type", type);
9177 public override bool GetAttributableValue (ResolveContext ec, Type value_type, out object value)
9179 return child.GetAttributableValue (ec, value_type, out value);
9184 // Implements the `stackalloc' keyword
9186 public class StackAlloc : Expression {
9187 Type otype;
9188 Expression t;
9189 Expression count;
9191 public StackAlloc (Expression type, Expression count, Location l)
9193 t = type;
9194 this.count = count;
9195 loc = l;
9198 public override Expression CreateExpressionTree (ResolveContext ec)
9200 throw new NotSupportedException ("ET");
9203 public override Expression DoResolve (ResolveContext ec)
9205 count = count.Resolve (ec);
9206 if (count == null)
9207 return null;
9209 if (count.Type != TypeManager.uint32_type){
9210 count = Convert.ImplicitConversionRequired (ec, count, TypeManager.int32_type, loc);
9211 if (count == null)
9212 return null;
9215 Constant c = count as Constant;
9216 if (c != null && c.IsNegative) {
9217 ec.Report.Error (247, loc, "Cannot use a negative size with stackalloc");
9218 return null;
9221 if (ec.HasAny (ResolveContext.Options.CatchScope | ResolveContext.Options.FinallyScope)) {
9222 ec.Report.Error (255, loc, "Cannot use stackalloc in finally or catch");
9225 TypeExpr texpr = t.ResolveAsTypeTerminal (ec, false);
9226 if (texpr == null)
9227 return null;
9229 otype = texpr.Type;
9231 if (!TypeManager.VerifyUnManaged (otype, loc))
9232 return null;
9234 type = TypeManager.GetPointerType (otype);
9235 eclass = ExprClass.Value;
9237 return this;
9240 public override void Emit (EmitContext ec)
9242 int size = GetTypeSize (otype);
9243 ILGenerator ig = ec.ig;
9245 count.Emit (ec);
9247 if (size == 0)
9248 ig.Emit (OpCodes.Sizeof, otype);
9249 else
9250 IntConstant.EmitInt (ig, size);
9252 ig.Emit (OpCodes.Mul_Ovf_Un);
9253 ig.Emit (OpCodes.Localloc);
9256 protected override void CloneTo (CloneContext clonectx, Expression t)
9258 StackAlloc target = (StackAlloc) t;
9259 target.count = count.Clone (clonectx);
9260 target.t = t.Clone (clonectx);
9265 // An object initializer expression
9267 public class ElementInitializer : Assign
9269 public readonly string Name;
9271 public ElementInitializer (string name, Expression initializer, Location loc)
9272 : base (null, initializer, loc)
9274 this.Name = name;
9277 protected override void CloneTo (CloneContext clonectx, Expression t)
9279 ElementInitializer target = (ElementInitializer) t;
9280 target.source = source.Clone (clonectx);
9283 public override Expression CreateExpressionTree (ResolveContext ec)
9285 Arguments args = new Arguments (2);
9286 FieldExpr fe = target as FieldExpr;
9287 if (fe != null)
9288 args.Add (new Argument (fe.CreateTypeOfExpression ()));
9289 else
9290 args.Add (new Argument (((PropertyExpr)target).CreateSetterTypeOfExpression ()));
9292 args.Add (new Argument (source.CreateExpressionTree (ec)));
9293 return CreateExpressionFactoryCall (ec,
9294 source is CollectionOrObjectInitializers ? "ListBind" : "Bind",
9295 args);
9298 public override Expression DoResolve (ResolveContext ec)
9300 if (source == null)
9301 return EmptyExpressionStatement.Instance;
9303 MemberExpr me = MemberLookupFinal (ec, ec.CurrentInitializerVariable.Type, ec.CurrentInitializerVariable.Type,
9304 Name, MemberTypes.Field | MemberTypes.Property, BindingFlags.Public | BindingFlags.Instance, loc) as MemberExpr;
9306 if (me == null)
9307 return null;
9309 target = me;
9310 me.InstanceExpression = ec.CurrentInitializerVariable;
9312 if (source is CollectionOrObjectInitializers) {
9313 Expression previous = ec.CurrentInitializerVariable;
9314 ec.CurrentInitializerVariable = target;
9315 source = source.Resolve (ec);
9316 ec.CurrentInitializerVariable = previous;
9317 if (source == null)
9318 return null;
9320 eclass = source.eclass;
9321 type = source.Type;
9322 return this;
9325 Expression expr = base.DoResolve (ec);
9326 if (expr == null)
9327 return null;
9330 // Ignore field initializers with default value
9332 Constant c = source as Constant;
9333 if (c != null && c.IsDefaultInitializer (type) && target.eclass == ExprClass.Variable)
9334 return EmptyExpressionStatement.Instance.DoResolve (ec);
9336 return expr;
9339 protected override Expression Error_MemberLookupFailed (ResolveContext ec, Type type, MemberInfo[] members)
9341 MemberInfo member = members [0];
9342 if (member.MemberType != MemberTypes.Property && member.MemberType != MemberTypes.Field)
9343 ec.Report.Error (1913, loc, "Member `{0}' cannot be initialized. An object " +
9344 "initializer may only be used for fields, or properties", TypeManager.GetFullNameSignature (member));
9345 else
9346 ec.Report.Error (1914, loc, " Static field or property `{0}' cannot be assigned in an object initializer",
9347 TypeManager.GetFullNameSignature (member));
9349 return null;
9352 public override void EmitStatement (EmitContext ec)
9354 if (source is CollectionOrObjectInitializers)
9355 source.Emit (ec);
9356 else
9357 base.EmitStatement (ec);
9362 // A collection initializer expression
9364 class CollectionElementInitializer : Invocation
9366 public class ElementInitializerArgument : Argument
9368 public ElementInitializerArgument (Expression e)
9369 : base (e)
9374 sealed class AddMemberAccess : MemberAccess
9376 public AddMemberAccess (Expression expr, Location loc)
9377 : base (expr, "Add", loc)
9381 protected override void Error_TypeDoesNotContainDefinition (ResolveContext ec, Type type, string name)
9383 if (TypeManager.HasElementType (type))
9384 return;
9386 base.Error_TypeDoesNotContainDefinition (ec, type, name);
9390 public CollectionElementInitializer (Expression argument)
9391 : base (null, new Arguments (1))
9393 base.arguments.Add (new ElementInitializerArgument (argument));
9394 this.loc = argument.Location;
9397 public CollectionElementInitializer (ArrayList arguments, Location loc)
9398 : base (null, new Arguments (arguments.Count))
9400 foreach (Expression e in arguments)
9401 base.arguments.Add (new ElementInitializerArgument (e));
9403 this.loc = loc;
9406 public override Expression CreateExpressionTree (ResolveContext ec)
9408 Arguments args = new Arguments (2);
9409 args.Add (new Argument (mg.CreateExpressionTree (ec)));
9411 ArrayList expr_initializers = new ArrayList (arguments.Count);
9412 foreach (Argument a in arguments)
9413 expr_initializers.Add (a.CreateExpressionTree (ec));
9415 args.Add (new Argument (new ArrayCreation (
9416 CreateExpressionTypeExpression (ec, loc), "[]", expr_initializers, loc)));
9417 return CreateExpressionFactoryCall (ec, "ElementInit", args);
9420 protected override void CloneTo (CloneContext clonectx, Expression t)
9422 CollectionElementInitializer target = (CollectionElementInitializer) t;
9423 if (arguments != null)
9424 target.arguments = arguments.Clone (clonectx);
9427 public override Expression DoResolve (ResolveContext ec)
9429 if (eclass != ExprClass.Invalid)
9430 return this;
9432 base.expr = new AddMemberAccess (ec.CurrentInitializerVariable, loc);
9434 return base.DoResolve (ec);
9439 // A block of object or collection initializers
9441 public class CollectionOrObjectInitializers : ExpressionStatement
9443 ArrayList initializers;
9444 bool is_collection_initialization;
9446 public static readonly CollectionOrObjectInitializers Empty =
9447 new CollectionOrObjectInitializers (new ArrayList (0), Location.Null);
9449 public CollectionOrObjectInitializers (ArrayList initializers, Location loc)
9451 this.initializers = initializers;
9452 this.loc = loc;
9455 public bool IsEmpty {
9456 get {
9457 return initializers.Count == 0;
9461 public bool IsCollectionInitializer {
9462 get {
9463 return is_collection_initialization;
9467 protected override void CloneTo (CloneContext clonectx, Expression target)
9469 CollectionOrObjectInitializers t = (CollectionOrObjectInitializers) target;
9471 t.initializers = new ArrayList (initializers.Count);
9472 foreach (Expression e in initializers)
9473 t.initializers.Add (e.Clone (clonectx));
9476 public override Expression CreateExpressionTree (ResolveContext ec)
9478 ArrayList expr_initializers = new ArrayList (initializers.Count);
9479 foreach (Expression e in initializers) {
9480 Expression expr = e.CreateExpressionTree (ec);
9481 if (expr != null)
9482 expr_initializers.Add (expr);
9485 return new ImplicitlyTypedArrayCreation ("[]", expr_initializers, loc);
9488 public override Expression DoResolve (ResolveContext ec)
9490 if (eclass != ExprClass.Invalid)
9491 return this;
9493 ArrayList element_names = null;
9494 for (int i = 0; i < initializers.Count; ++i) {
9495 Expression initializer = (Expression) initializers [i];
9496 ElementInitializer element_initializer = initializer as ElementInitializer;
9498 if (i == 0) {
9499 if (element_initializer != null) {
9500 element_names = new ArrayList (initializers.Count);
9501 element_names.Add (element_initializer.Name);
9502 } else if (initializer is CompletingExpression){
9503 initializer.Resolve (ec);
9504 throw new InternalErrorException ("This line should never be reached");
9505 } else {
9506 if (!TypeManager.ImplementsInterface (ec.CurrentInitializerVariable.Type, TypeManager.ienumerable_type)) {
9507 ec.Report.Error (1922, loc, "A field or property `{0}' cannot be initialized with a collection " +
9508 "object initializer because type `{1}' does not implement `{2}' interface",
9509 ec.CurrentInitializerVariable.GetSignatureForError (),
9510 TypeManager.CSharpName (ec.CurrentInitializerVariable.Type),
9511 TypeManager.CSharpName (TypeManager.ienumerable_type));
9512 return null;
9514 is_collection_initialization = true;
9516 } else {
9517 if (is_collection_initialization != (element_initializer == null)) {
9518 ec.Report.Error (747, initializer.Location, "Inconsistent `{0}' member declaration",
9519 is_collection_initialization ? "collection initializer" : "object initializer");
9520 continue;
9523 if (!is_collection_initialization) {
9524 if (element_names.Contains (element_initializer.Name)) {
9525 ec.Report.Error (1912, element_initializer.Location,
9526 "An object initializer includes more than one member `{0}' initialization",
9527 element_initializer.Name);
9528 } else {
9529 element_names.Add (element_initializer.Name);
9534 Expression e = initializer.Resolve (ec);
9535 if (e == EmptyExpressionStatement.Instance)
9536 initializers.RemoveAt (i--);
9537 else
9538 initializers [i] = e;
9541 type = ec.CurrentInitializerVariable.Type;
9542 if (is_collection_initialization) {
9543 if (TypeManager.HasElementType (type)) {
9544 ec.Report.Error (1925, loc, "Cannot initialize object of type `{0}' with a collection initializer",
9545 TypeManager.CSharpName (type));
9549 eclass = ExprClass.Variable;
9550 return this;
9553 public override void Emit (EmitContext ec)
9555 EmitStatement (ec);
9558 public override void EmitStatement (EmitContext ec)
9560 foreach (ExpressionStatement e in initializers)
9561 e.EmitStatement (ec);
9564 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
9566 foreach (Expression e in initializers)
9567 e.MutateHoistedGenericType (storey);
9572 // New expression with element/object initializers
9574 public class NewInitialize : New
9577 // This class serves as a proxy for variable initializer target instances.
9578 // A real variable is assigned later when we resolve left side of an
9579 // assignment
9581 sealed class InitializerTargetExpression : Expression, IMemoryLocation
9583 NewInitialize new_instance;
9585 public InitializerTargetExpression (NewInitialize newInstance)
9587 this.type = newInstance.type;
9588 this.loc = newInstance.loc;
9589 this.eclass = newInstance.eclass;
9590 this.new_instance = newInstance;
9593 public override Expression CreateExpressionTree (ResolveContext ec)
9595 // Should not be reached
9596 throw new NotSupportedException ("ET");
9599 public override Expression DoResolve (ResolveContext ec)
9601 return this;
9604 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
9606 return this;
9609 public override void Emit (EmitContext ec)
9611 Expression e = (Expression) new_instance.instance;
9612 e.Emit (ec);
9615 #region IMemoryLocation Members
9617 public void AddressOf (EmitContext ec, AddressOp mode)
9619 new_instance.instance.AddressOf (ec, mode);
9622 #endregion
9625 CollectionOrObjectInitializers initializers;
9626 IMemoryLocation instance;
9628 public NewInitialize (Expression requested_type, Arguments arguments, CollectionOrObjectInitializers initializers, Location l)
9629 : base (requested_type, arguments, l)
9631 this.initializers = initializers;
9634 protected override IMemoryLocation EmitAddressOf (EmitContext ec, AddressOp Mode)
9636 instance = base.EmitAddressOf (ec, Mode);
9638 if (!initializers.IsEmpty)
9639 initializers.Emit (ec);
9641 return instance;
9644 protected override void CloneTo (CloneContext clonectx, Expression t)
9646 base.CloneTo (clonectx, t);
9648 NewInitialize target = (NewInitialize) t;
9649 target.initializers = (CollectionOrObjectInitializers) initializers.Clone (clonectx);
9652 public override Expression CreateExpressionTree (ResolveContext ec)
9654 Arguments args = new Arguments (2);
9655 args.Add (new Argument (base.CreateExpressionTree (ec)));
9656 if (!initializers.IsEmpty)
9657 args.Add (new Argument (initializers.CreateExpressionTree (ec)));
9659 return CreateExpressionFactoryCall (ec,
9660 initializers.IsCollectionInitializer ? "ListInit" : "MemberInit",
9661 args);
9664 public override Expression DoResolve (ResolveContext ec)
9666 if (eclass != ExprClass.Invalid)
9667 return this;
9669 Expression e = base.DoResolve (ec);
9670 if (type == null)
9671 return null;
9673 Expression previous = ec.CurrentInitializerVariable;
9674 ec.CurrentInitializerVariable = new InitializerTargetExpression (this);
9675 initializers.Resolve (ec);
9676 ec.CurrentInitializerVariable = previous;
9677 return e;
9680 public override bool Emit (EmitContext ec, IMemoryLocation target)
9682 bool left_on_stack = base.Emit (ec, target);
9684 if (initializers.IsEmpty)
9685 return left_on_stack;
9687 LocalTemporary temp = target as LocalTemporary;
9688 if (temp == null) {
9689 if (!left_on_stack) {
9690 VariableReference vr = target as VariableReference;
9692 // FIXME: This still does not work correctly for pre-set variables
9693 if (vr != null && vr.IsRef)
9694 target.AddressOf (ec, AddressOp.Load);
9696 ((Expression) target).Emit (ec);
9697 left_on_stack = true;
9700 temp = new LocalTemporary (type);
9703 instance = temp;
9704 if (left_on_stack)
9705 temp.Store (ec);
9707 initializers.Emit (ec);
9709 if (left_on_stack) {
9710 temp.Emit (ec);
9711 temp.Release (ec);
9714 return left_on_stack;
9717 public override bool HasInitializer {
9718 get {
9719 return !initializers.IsEmpty;
9723 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
9725 base.MutateHoistedGenericType (storey);
9726 initializers.MutateHoistedGenericType (storey);
9730 public class NewAnonymousType : New
9732 static readonly ArrayList EmptyParameters = new ArrayList (0);
9734 ArrayList parameters;
9735 readonly TypeContainer parent;
9736 AnonymousTypeClass anonymous_type;
9738 public NewAnonymousType (ArrayList parameters, TypeContainer parent, Location loc)
9739 : base (null, null, loc)
9741 this.parameters = parameters;
9742 this.parent = parent;
9745 protected override void CloneTo (CloneContext clonectx, Expression target)
9747 if (parameters == null)
9748 return;
9750 NewAnonymousType t = (NewAnonymousType) target;
9751 t.parameters = new ArrayList (parameters.Count);
9752 foreach (AnonymousTypeParameter atp in parameters)
9753 t.parameters.Add (atp.Clone (clonectx));
9756 AnonymousTypeClass CreateAnonymousType (ResolveContext ec, ArrayList parameters)
9758 AnonymousTypeClass type = parent.Module.GetAnonymousType (parameters);
9759 if (type != null)
9760 return type;
9762 type = AnonymousTypeClass.Create (ec.Compiler, parent, parameters, loc);
9763 if (type == null)
9764 return null;
9766 type.DefineType ();
9767 type.Define ();
9768 type.EmitType ();
9769 if (ec.Report.Errors == 0)
9770 type.CloseType ();
9772 parent.Module.AddAnonymousType (type);
9773 return type;
9776 public override Expression CreateExpressionTree (ResolveContext ec)
9778 #if GMCS_SOURCE
9779 if (parameters == null)
9780 return base.CreateExpressionTree (ec);
9782 ArrayList init = new ArrayList (parameters.Count);
9783 foreach (Property p in anonymous_type.Properties)
9784 init.Add (new TypeOfMethod (TypeBuilder.GetMethod (type, p.GetBuilder), loc));
9786 ArrayList ctor_args = new ArrayList (Arguments.Count);
9787 foreach (Argument a in Arguments)
9788 ctor_args.Add (a.CreateExpressionTree (ec));
9790 Arguments args = new Arguments (3);
9791 args.Add (new Argument (method.CreateExpressionTree (ec)));
9792 args.Add (new Argument (new ArrayCreation (TypeManager.expression_type_expr, "[]", ctor_args, loc)));
9793 args.Add (new Argument (new ImplicitlyTypedArrayCreation ("[]", init, loc)));
9795 return CreateExpressionFactoryCall (ec, "New", args);
9796 #else
9797 throw new NotSupportedException ();
9798 #endif
9801 public override Expression DoResolve (ResolveContext ec)
9803 if (ec.HasSet (ResolveContext.Options.ConstantScope)) {
9804 ec.Report.Error (836, loc, "Anonymous types cannot be used in this expression");
9805 return null;
9808 if (parameters == null) {
9809 anonymous_type = CreateAnonymousType (ec, EmptyParameters);
9810 RequestedType = new TypeExpression (anonymous_type.TypeBuilder, loc);
9811 return base.DoResolve (ec);
9814 bool error = false;
9815 Arguments = new Arguments (parameters.Count);
9816 TypeExpression [] t_args = new TypeExpression [parameters.Count];
9817 for (int i = 0; i < parameters.Count; ++i) {
9818 Expression e = ((AnonymousTypeParameter) parameters [i]).Resolve (ec);
9819 if (e == null) {
9820 error = true;
9821 continue;
9824 Arguments.Add (new Argument (e));
9825 t_args [i] = new TypeExpression (e.Type, e.Location);
9828 if (error)
9829 return null;
9831 anonymous_type = CreateAnonymousType (ec, parameters);
9832 if (anonymous_type == null)
9833 return null;
9835 RequestedType = new GenericTypeExpr (anonymous_type.TypeBuilder, new TypeArguments (t_args), loc);
9836 return base.DoResolve (ec);
9840 public class AnonymousTypeParameter : Expression
9842 public readonly string Name;
9843 Expression initializer;
9845 public AnonymousTypeParameter (Expression initializer, string name, Location loc)
9847 this.Name = name;
9848 this.loc = loc;
9849 this.initializer = initializer;
9852 public AnonymousTypeParameter (Parameter parameter)
9854 this.Name = parameter.Name;
9855 this.loc = parameter.Location;
9856 this.initializer = new SimpleName (Name, loc);
9859 protected override void CloneTo (CloneContext clonectx, Expression target)
9861 AnonymousTypeParameter t = (AnonymousTypeParameter) target;
9862 t.initializer = initializer.Clone (clonectx);
9865 public override Expression CreateExpressionTree (ResolveContext ec)
9867 throw new NotSupportedException ("ET");
9870 public override bool Equals (object o)
9872 AnonymousTypeParameter other = o as AnonymousTypeParameter;
9873 return other != null && Name == other.Name;
9876 public override int GetHashCode ()
9878 return Name.GetHashCode ();
9881 public override Expression DoResolve (ResolveContext ec)
9883 Expression e = initializer.Resolve (ec);
9884 if (e == null)
9885 return null;
9887 if (e.eclass == ExprClass.MethodGroup) {
9888 Error_InvalidInitializer (ec, e.ExprClassName);
9889 return null;
9892 type = e.Type;
9893 if (type == TypeManager.void_type || type == TypeManager.null_type ||
9894 type == InternalType.AnonymousMethod || type.IsPointer) {
9895 Error_InvalidInitializer (ec, e.GetSignatureForError ());
9896 return null;
9899 return e;
9902 protected virtual void Error_InvalidInitializer (ResolveContext ec, string initializer)
9904 ec.Report.Error (828, loc, "An anonymous type property `{0}' cannot be initialized with `{1}'",
9905 Name, initializer);
9908 public override void Emit (EmitContext ec)
9910 throw new InternalErrorException ("Should not be reached");