2 // expression.cs: Expression representation for the IL tree.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // Copyright 2001, 2002, 2003 Ximian, Inc.
9 // Copyright 2003-2008 Novell, Inc.
13 namespace Mono.CSharp {
15 using System.Collections.Generic;
16 using System.Reflection;
17 using System.Reflection.Emit;
20 using SLE = System.Linq.Expressions;
23 // This is an user operator expression, automatically created during
26 public class UserOperatorCall : Expression {
27 public delegate Expression ExpressionTreeExpression (ResolveContext ec, MethodGroupExpr mg);
29 protected readonly Arguments arguments;
30 protected readonly MethodGroupExpr mg;
31 readonly ExpressionTreeExpression expr_tree;
33 public UserOperatorCall (MethodGroupExpr mg, Arguments args, ExpressionTreeExpression expr_tree, Location loc)
36 this.arguments = args;
37 this.expr_tree = expr_tree;
39 type = mg.BestCandidate.ReturnType;
40 eclass = ExprClass.Value;
44 public override Expression CreateExpressionTree (ResolveContext ec)
46 if (expr_tree != null)
47 return expr_tree (ec, mg);
49 Arguments args = Arguments.CreateForExpressionTree (ec, arguments,
50 new NullLiteral (loc),
51 mg.CreateExpressionTree (ec));
53 return CreateExpressionFactoryCall (ec, "Call", args);
56 protected override void CloneTo (CloneContext context, Expression target)
61 protected override Expression DoResolve (ResolveContext ec)
64 // We are born fully resolved
69 public override void Emit (EmitContext ec)
71 mg.EmitCall (ec, arguments);
74 public override SLE.Expression MakeExpression (BuilderContext ctx)
76 var method = mg.BestCandidate.GetMetaInfo () as MethodInfo;
77 return SLE.Expression.Call (method, Arguments.MakeExpression (arguments, ctx));
80 public MethodGroupExpr Method {
85 public class ParenthesizedExpression : ShimExpression
87 public ParenthesizedExpression (Expression expr)
93 protected override Expression DoResolve (ResolveContext ec)
95 return expr.Resolve (ec);
98 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
100 return expr.DoResolveLValue (ec, right_side);
105 // Unary implements unary expressions.
107 public class Unary : Expression
109 public enum Operator : byte {
110 UnaryPlus, UnaryNegation, LogicalNot, OnesComplement,
114 static TypeSpec[][] predefined_operators;
116 public readonly Operator Oper;
117 public Expression Expr;
118 Expression enum_conversion;
120 public Unary (Operator op, Expression expr, Location loc)
128 // This routine will attempt to simplify the unary expression when the
129 // argument is a constant.
131 Constant TryReduceConstant (ResolveContext ec, Constant e)
133 if (e is EmptyConstantCast)
134 return TryReduceConstant (ec, ((EmptyConstantCast) e).child);
136 if (e is SideEffectConstant) {
137 Constant r = TryReduceConstant (ec, ((SideEffectConstant) e).value);
138 return r == null ? null : new SideEffectConstant (r, e, r.Location);
141 TypeSpec expr_type = e.Type;
144 case Operator.UnaryPlus:
145 // Unary numeric promotions
146 if (expr_type == TypeManager.byte_type)
147 return new IntConstant (((ByteConstant)e).Value, e.Location);
148 if (expr_type == TypeManager.sbyte_type)
149 return new IntConstant (((SByteConstant)e).Value, e.Location);
150 if (expr_type == TypeManager.short_type)
151 return new IntConstant (((ShortConstant)e).Value, e.Location);
152 if (expr_type == TypeManager.ushort_type)
153 return new IntConstant (((UShortConstant)e).Value, e.Location);
154 if (expr_type == TypeManager.char_type)
155 return new IntConstant (((CharConstant)e).Value, e.Location);
157 // Predefined operators
158 if (expr_type == TypeManager.int32_type || expr_type == TypeManager.uint32_type ||
159 expr_type == TypeManager.int64_type || expr_type == TypeManager.uint64_type ||
160 expr_type == TypeManager.float_type || expr_type == TypeManager.double_type ||
161 expr_type == TypeManager.decimal_type) {
167 case Operator.UnaryNegation:
168 // Unary numeric promotions
169 if (expr_type == TypeManager.byte_type)
170 return new IntConstant (-((ByteConstant)e).Value, e.Location);
171 if (expr_type == TypeManager.sbyte_type)
172 return new IntConstant (-((SByteConstant)e).Value, e.Location);
173 if (expr_type == TypeManager.short_type)
174 return new IntConstant (-((ShortConstant)e).Value, e.Location);
175 if (expr_type == TypeManager.ushort_type)
176 return new IntConstant (-((UShortConstant)e).Value, e.Location);
177 if (expr_type == TypeManager.char_type)
178 return new IntConstant (-((CharConstant)e).Value, e.Location);
180 // Predefined operators
181 if (expr_type == TypeManager.int32_type) {
182 int value = ((IntConstant)e).Value;
183 if (value == int.MinValue) {
184 if (ec.ConstantCheckState) {
185 ConstantFold.Error_CompileTimeOverflow (ec, loc);
190 return new IntConstant (-value, e.Location);
192 if (expr_type == TypeManager.int64_type) {
193 long value = ((LongConstant)e).Value;
194 if (value == long.MinValue) {
195 if (ec.ConstantCheckState) {
196 ConstantFold.Error_CompileTimeOverflow (ec, loc);
201 return new LongConstant (-value, e.Location);
204 if (expr_type == TypeManager.uint32_type) {
205 UIntLiteral uil = e as UIntLiteral;
207 if (uil.Value == int.MaxValue + (uint) 1)
208 return new IntLiteral (int.MinValue, e.Location);
209 return new LongLiteral (-uil.Value, e.Location);
211 return new LongConstant (-((UIntConstant)e).Value, e.Location);
214 if (expr_type == TypeManager.uint64_type) {
215 ULongLiteral ull = e as ULongLiteral;
216 if (ull != null && ull.Value == 9223372036854775808)
217 return new LongLiteral (long.MinValue, e.Location);
221 if (expr_type == TypeManager.float_type) {
222 FloatLiteral fl = e as FloatLiteral;
223 // For better error reporting
225 return new FloatLiteral (-fl.Value, e.Location);
227 return new FloatConstant (-((FloatConstant)e).Value, e.Location);
229 if (expr_type == TypeManager.double_type) {
230 DoubleLiteral dl = e as DoubleLiteral;
231 // For better error reporting
233 return new DoubleLiteral (-dl.Value, e.Location);
235 return new DoubleConstant (-((DoubleConstant)e).Value, e.Location);
237 if (expr_type == TypeManager.decimal_type)
238 return new DecimalConstant (-((DecimalConstant)e).Value, e.Location);
242 case Operator.LogicalNot:
243 if (expr_type != TypeManager.bool_type)
246 bool b = (bool)e.GetValue ();
247 return new BoolConstant (!b, e.Location);
249 case Operator.OnesComplement:
250 // Unary numeric promotions
251 if (expr_type == TypeManager.byte_type)
252 return new IntConstant (~((ByteConstant)e).Value, e.Location);
253 if (expr_type == TypeManager.sbyte_type)
254 return new IntConstant (~((SByteConstant)e).Value, e.Location);
255 if (expr_type == TypeManager.short_type)
256 return new IntConstant (~((ShortConstant)e).Value, e.Location);
257 if (expr_type == TypeManager.ushort_type)
258 return new IntConstant (~((UShortConstant)e).Value, e.Location);
259 if (expr_type == TypeManager.char_type)
260 return new IntConstant (~((CharConstant)e).Value, e.Location);
262 // Predefined operators
263 if (expr_type == TypeManager.int32_type)
264 return new IntConstant (~((IntConstant)e).Value, e.Location);
265 if (expr_type == TypeManager.uint32_type)
266 return new UIntConstant (~((UIntConstant)e).Value, e.Location);
267 if (expr_type == TypeManager.int64_type)
268 return new LongConstant (~((LongConstant)e).Value, e.Location);
269 if (expr_type == TypeManager.uint64_type){
270 return new ULongConstant (~((ULongConstant)e).Value, e.Location);
272 if (e is EnumConstant) {
273 e = TryReduceConstant (ec, ((EnumConstant)e).Child);
275 e = new EnumConstant (e, expr_type);
280 throw new Exception ("Can not constant fold: " + Oper.ToString());
283 protected Expression ResolveOperator (ResolveContext ec, Expression expr)
285 eclass = ExprClass.Value;
287 if (predefined_operators == null)
288 CreatePredefinedOperatorsTable ();
290 TypeSpec expr_type = expr.Type;
291 Expression best_expr;
294 // Primitive types first
296 if (TypeManager.IsPrimitiveType (expr_type)) {
297 best_expr = ResolvePrimitivePredefinedType (expr);
298 if (best_expr == null)
301 type = best_expr.Type;
307 // E operator ~(E x);
309 if (Oper == Operator.OnesComplement && TypeManager.IsEnumType (expr_type))
310 return ResolveEnumOperator (ec, expr);
312 return ResolveUserType (ec, expr);
315 protected virtual Expression ResolveEnumOperator (ResolveContext ec, Expression expr)
317 TypeSpec underlying_type = EnumSpec.GetUnderlyingType (expr.Type);
318 Expression best_expr = ResolvePrimitivePredefinedType (EmptyCast.Create (expr, underlying_type));
319 if (best_expr == null)
323 enum_conversion = Convert.ExplicitNumericConversion (new EmptyExpression (best_expr.Type), underlying_type);
325 return EmptyCast.Create (this, type);
328 public override Expression CreateExpressionTree (ResolveContext ec)
330 return CreateExpressionTree (ec, null);
333 Expression CreateExpressionTree (ResolveContext ec, MethodGroupExpr user_op)
337 case Operator.AddressOf:
338 Error_PointerInsideExpressionTree (ec);
340 case Operator.UnaryNegation:
341 if (ec.HasSet (ResolveContext.Options.CheckedScope) && user_op == null && !IsFloat (type))
342 method_name = "NegateChecked";
344 method_name = "Negate";
346 case Operator.OnesComplement:
347 case Operator.LogicalNot:
350 case Operator.UnaryPlus:
351 method_name = "UnaryPlus";
354 throw new InternalErrorException ("Unknown unary operator " + Oper.ToString ());
357 Arguments args = new Arguments (2);
358 args.Add (new Argument (Expr.CreateExpressionTree (ec)));
360 args.Add (new Argument (user_op.CreateExpressionTree (ec)));
361 return CreateExpressionFactoryCall (ec, method_name, args);
364 static void CreatePredefinedOperatorsTable ()
366 predefined_operators = new TypeSpec [(int) Operator.TOP] [];
369 // 7.6.1 Unary plus operator
371 predefined_operators [(int) Operator.UnaryPlus] = new TypeSpec [] {
372 TypeManager.int32_type, TypeManager.uint32_type,
373 TypeManager.int64_type, TypeManager.uint64_type,
374 TypeManager.float_type, TypeManager.double_type,
375 TypeManager.decimal_type
379 // 7.6.2 Unary minus operator
381 predefined_operators [(int) Operator.UnaryNegation] = new TypeSpec [] {
382 TypeManager.int32_type,
383 TypeManager.int64_type,
384 TypeManager.float_type, TypeManager.double_type,
385 TypeManager.decimal_type
389 // 7.6.3 Logical negation operator
391 predefined_operators [(int) Operator.LogicalNot] = new TypeSpec [] {
392 TypeManager.bool_type
396 // 7.6.4 Bitwise complement operator
398 predefined_operators [(int) Operator.OnesComplement] = new TypeSpec [] {
399 TypeManager.int32_type, TypeManager.uint32_type,
400 TypeManager.int64_type, TypeManager.uint64_type
405 // Unary numeric promotions
407 static Expression DoNumericPromotion (Operator op, Expression expr)
409 TypeSpec expr_type = expr.Type;
410 if ((op == Operator.UnaryPlus || op == Operator.UnaryNegation || op == Operator.OnesComplement) &&
411 expr_type == TypeManager.byte_type || expr_type == TypeManager.sbyte_type ||
412 expr_type == TypeManager.short_type || expr_type == TypeManager.ushort_type ||
413 expr_type == TypeManager.char_type)
414 return Convert.ImplicitNumericConversion (expr, TypeManager.int32_type);
416 if (op == Operator.UnaryNegation && expr_type == TypeManager.uint32_type)
417 return Convert.ImplicitNumericConversion (expr, TypeManager.int64_type);
422 protected override Expression DoResolve (ResolveContext ec)
424 if (Oper == Operator.AddressOf) {
425 return ResolveAddressOf (ec);
428 Expr = Expr.Resolve (ec);
432 if (Expr.Type == InternalType.Dynamic) {
433 Arguments args = new Arguments (1);
434 args.Add (new Argument (Expr));
435 return new DynamicUnaryConversion (GetOperatorExpressionTypeName (), args, loc).Resolve (ec);
438 if (TypeManager.IsNullableType (Expr.Type))
439 return new Nullable.LiftedUnaryOperator (Oper, Expr, loc).Resolve (ec);
442 // Attempt to use a constant folding operation.
444 Constant cexpr = Expr as Constant;
446 cexpr = TryReduceConstant (ec, cexpr);
448 return cexpr.Resolve (ec);
451 Expression expr = ResolveOperator (ec, Expr);
453 Error_OperatorCannotBeApplied (ec, loc, OperName (Oper), Expr.Type);
456 // Reduce unary operator on predefined types
458 if (expr == this && Oper == Operator.UnaryPlus)
464 public override Expression DoResolveLValue (ResolveContext ec, Expression right)
469 public override void Emit (EmitContext ec)
471 EmitOperator (ec, type);
474 protected void EmitOperator (EmitContext ec, TypeSpec type)
477 case Operator.UnaryPlus:
481 case Operator.UnaryNegation:
482 if (ec.HasSet (EmitContext.Options.CheckedScope) && !IsFloat (type)) {
483 ec.Emit (OpCodes.Ldc_I4_0);
484 if (type == TypeManager.int64_type)
485 ec.Emit (OpCodes.Conv_U8);
487 ec.Emit (OpCodes.Sub_Ovf);
490 ec.Emit (OpCodes.Neg);
495 case Operator.LogicalNot:
497 ec.Emit (OpCodes.Ldc_I4_0);
498 ec.Emit (OpCodes.Ceq);
501 case Operator.OnesComplement:
503 ec.Emit (OpCodes.Not);
506 case Operator.AddressOf:
507 ((IMemoryLocation)Expr).AddressOf (ec, AddressOp.LoadStore);
511 throw new Exception ("This should not happen: Operator = "
516 // Same trick as in Binary expression
518 if (enum_conversion != null)
519 enum_conversion.Emit (ec);
522 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
524 if (Oper == Operator.LogicalNot)
525 Expr.EmitBranchable (ec, target, !on_true);
527 base.EmitBranchable (ec, target, on_true);
530 public override void EmitSideEffect (EmitContext ec)
532 Expr.EmitSideEffect (ec);
535 public static void Error_OperatorCannotBeApplied (ResolveContext ec, Location loc, string oper, TypeSpec t)
537 ec.Report.Error (23, loc, "The `{0}' operator cannot be applied to operand of type `{1}'",
538 oper, TypeManager.CSharpName (t));
542 // Converts operator to System.Linq.Expressions.ExpressionType enum name
544 string GetOperatorExpressionTypeName ()
547 case Operator.OnesComplement:
548 return "OnesComplement";
549 case Operator.LogicalNot:
551 case Operator.UnaryNegation:
553 case Operator.UnaryPlus:
556 throw new NotImplementedException ("Unknown express type operator " + Oper.ToString ());
560 static bool IsFloat (TypeSpec t)
562 return t == TypeManager.float_type || t == TypeManager.double_type;
566 // Returns a stringified representation of the Operator
568 public static string OperName (Operator oper)
571 case Operator.UnaryPlus:
573 case Operator.UnaryNegation:
575 case Operator.LogicalNot:
577 case Operator.OnesComplement:
579 case Operator.AddressOf:
583 throw new NotImplementedException (oper.ToString ());
586 public override SLE.Expression MakeExpression (BuilderContext ctx)
588 var expr = Expr.MakeExpression (ctx);
589 bool is_checked = ctx.HasSet (BuilderContext.Options.CheckedScope);
592 case Operator.UnaryNegation:
593 return is_checked ? SLE.Expression.NegateChecked (expr) : SLE.Expression.Negate (expr);
594 case Operator.LogicalNot:
595 return SLE.Expression.Not (expr);
597 case Operator.OnesComplement:
598 return SLE.Expression.OnesComplement (expr);
601 throw new NotImplementedException (Oper.ToString ());
605 public static void Reset ()
607 predefined_operators = null;
610 Expression ResolveAddressOf (ResolveContext ec)
613 UnsafeError (ec, loc);
615 Expr = Expr.DoResolveLValue (ec, EmptyExpression.UnaryAddress);
616 if (Expr == null || Expr.eclass != ExprClass.Variable) {
617 ec.Report.Error (211, loc, "Cannot take the address of the given expression");
621 if (!TypeManager.VerifyUnmanaged (ec.Compiler, Expr.Type, loc)) {
625 IVariableReference vr = Expr as IVariableReference;
628 VariableInfo vi = vr.VariableInfo;
630 if (vi.LocalInfo != null)
631 vi.LocalInfo.Used = true;
634 // A variable is considered definitely assigned if you take its address.
639 is_fixed = vr.IsFixed;
640 vr.SetHasAddressTaken ();
643 AnonymousMethodExpression.Error_AddressOfCapturedVar (ec, vr, loc);
646 IFixedExpression fe = Expr as IFixedExpression;
647 is_fixed = fe != null && fe.IsFixed;
650 if (!is_fixed && !ec.HasSet (ResolveContext.Options.FixedInitializerScope)) {
651 ec.Report.Error (212, loc, "You can only take the address of unfixed expression inside of a fixed statement initializer");
654 type = PointerContainer.MakeType (Expr.Type);
655 eclass = ExprClass.Value;
659 Expression ResolvePrimitivePredefinedType (Expression expr)
661 expr = DoNumericPromotion (Oper, expr);
662 TypeSpec expr_type = expr.Type;
663 TypeSpec[] predefined = predefined_operators [(int) Oper];
664 foreach (TypeSpec t in predefined) {
672 // Perform user-operator overload resolution
674 protected virtual Expression ResolveUserOperator (ResolveContext ec, Expression expr)
676 CSharp.Operator.OpType op_type;
678 case Operator.LogicalNot:
679 op_type = CSharp.Operator.OpType.LogicalNot; break;
680 case Operator.OnesComplement:
681 op_type = CSharp.Operator.OpType.OnesComplement; break;
682 case Operator.UnaryNegation:
683 op_type = CSharp.Operator.OpType.UnaryNegation; break;
684 case Operator.UnaryPlus:
685 op_type = CSharp.Operator.OpType.UnaryPlus; break;
687 throw new InternalErrorException (Oper.ToString ());
690 string op_name = CSharp.Operator.GetMetadataName (op_type);
691 MethodGroupExpr user_op = MethodLookup (ec.Compiler, ec.CurrentType, expr.Type, MemberKind.Operator, op_name, 0, expr.Location);
695 Arguments args = new Arguments (1);
696 args.Add (new Argument (expr));
697 user_op = user_op.OverloadResolve (ec, ref args, false, expr.Location);
702 Expr = args [0].Expr;
703 return new UserOperatorCall (user_op, args, CreateExpressionTree, expr.Location);
707 // Unary user type overload resolution
709 Expression ResolveUserType (ResolveContext ec, Expression expr)
711 Expression best_expr = ResolveUserOperator (ec, expr);
712 if (best_expr != null)
715 TypeSpec[] predefined = predefined_operators [(int) Oper];
716 foreach (TypeSpec t in predefined) {
717 Expression oper_expr = Convert.UserDefinedConversion (ec, expr, t, expr.Location, false, false);
718 if (oper_expr == null)
722 // decimal type is predefined but has user-operators
724 if (oper_expr.Type == TypeManager.decimal_type)
725 oper_expr = ResolveUserType (ec, oper_expr);
727 oper_expr = ResolvePrimitivePredefinedType (oper_expr);
729 if (oper_expr == null)
732 if (best_expr == null) {
733 best_expr = oper_expr;
737 int result = MethodGroupExpr.BetterTypeConversion (ec, best_expr.Type, t);
739 ec.Report.Error (35, loc, "Operator `{0}' is ambiguous on an operand of type `{1}'",
740 OperName (Oper), TypeManager.CSharpName (expr.Type));
745 best_expr = oper_expr;
748 if (best_expr == null)
752 // HACK: Decimal user-operator is included in standard operators
754 if (best_expr.Type == TypeManager.decimal_type)
758 type = best_expr.Type;
762 protected override void CloneTo (CloneContext clonectx, Expression t)
764 Unary target = (Unary) t;
766 target.Expr = Expr.Clone (clonectx);
771 // Unary operators are turned into Indirection expressions
772 // after semantic analysis (this is so we can take the address
773 // of an indirection).
775 public class Indirection : Expression, IMemoryLocation, IAssignMethod, IFixedExpression {
777 LocalTemporary temporary;
780 public Indirection (Expression expr, Location l)
786 public override Expression CreateExpressionTree (ResolveContext ec)
788 Error_PointerInsideExpressionTree (ec);
792 protected override void CloneTo (CloneContext clonectx, Expression t)
794 Indirection target = (Indirection) t;
795 target.expr = expr.Clone (clonectx);
798 public override void Emit (EmitContext ec)
803 ec.EmitLoadFromPtr (Type);
806 public void Emit (EmitContext ec, bool leave_copy)
810 ec.Emit (OpCodes.Dup);
811 temporary = new LocalTemporary (expr.Type);
812 temporary.Store (ec);
816 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
818 prepared = prepare_for_load;
822 if (prepare_for_load)
823 ec.Emit (OpCodes.Dup);
827 ec.Emit (OpCodes.Dup);
828 temporary = new LocalTemporary (expr.Type);
829 temporary.Store (ec);
832 ec.EmitStoreFromPtr (type);
834 if (temporary != null) {
836 temporary.Release (ec);
840 public void AddressOf (EmitContext ec, AddressOp Mode)
845 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
847 return DoResolve (ec);
850 protected override Expression DoResolve (ResolveContext ec)
852 expr = expr.Resolve (ec);
857 UnsafeError (ec, loc);
859 if (!expr.Type.IsPointer) {
860 ec.Report.Error (193, loc, "The * or -> operator must be applied to a pointer");
864 if (expr.Type == TypeManager.void_ptr_type) {
865 ec.Report.Error (242, loc, "The operation in question is undefined on void pointers");
869 type = TypeManager.GetElementType (expr.Type);
870 eclass = ExprClass.Variable;
874 public bool IsFixed {
878 public override string ToString ()
880 return "*(" + expr + ")";
885 /// Unary Mutator expressions (pre and post ++ and --)
889 /// UnaryMutator implements ++ and -- expressions. It derives from
890 /// ExpressionStatement becuase the pre/post increment/decrement
891 /// operators can be used in a statement context.
893 /// FIXME: Idea, we could split this up in two classes, one simpler
894 /// for the common case, and one with the extra fields for more complex
895 /// classes (indexers require temporary access; overloaded require method)
898 public class UnaryMutator : ExpressionStatement
900 class DynamicPostMutator : Expression, IAssignMethod
905 public DynamicPostMutator (Expression expr)
908 this.type = expr.Type;
909 this.loc = expr.Location;
912 public override Expression CreateExpressionTree (ResolveContext ec)
914 throw new NotImplementedException ("ET");
917 protected override Expression DoResolve (ResolveContext rc)
919 eclass = expr.eclass;
923 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
925 expr.DoResolveLValue (ec, right_side);
926 return DoResolve (ec);
929 public override void Emit (EmitContext ec)
934 public void Emit (EmitContext ec, bool leave_copy)
936 throw new NotImplementedException ();
940 // Emits target assignment using unmodified source value
942 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
945 // Allocate temporary variable to keep original value before it's modified
947 temp = new LocalTemporary (type);
951 ((IAssignMethod) expr).EmitAssign (ec, source, false, prepare_for_load);
962 public enum Mode : byte {
969 PreDecrement = IsDecrement,
970 PostIncrement = IsPost,
971 PostDecrement = IsPost | IsDecrement
975 bool is_expr, recurse;
979 // Holds the real operation
980 Expression operation;
982 public UnaryMutator (Mode m, Expression e, Location loc)
989 public override Expression CreateExpressionTree (ResolveContext ec)
991 return new SimpleAssign (this, this).CreateExpressionTree (ec);
994 protected override Expression DoResolve (ResolveContext ec)
996 expr = expr.Resolve (ec);
1001 if (expr.Type == InternalType.Dynamic) {
1003 // Handle postfix unary operators using local
1004 // temporary variable
1006 if ((mode & Mode.IsPost) != 0)
1007 expr = new DynamicPostMutator (expr);
1009 Arguments args = new Arguments (1);
1010 args.Add (new Argument (expr));
1011 return new SimpleAssign (expr, new DynamicUnaryConversion (GetOperatorExpressionTypeName (), args, loc)).Resolve (ec);
1014 if (TypeManager.IsNullableType (expr.Type))
1015 return new Nullable.LiftedUnaryMutator (mode, expr, loc).Resolve (ec);
1017 eclass = ExprClass.Value;
1019 return ResolveOperator (ec);
1022 void EmitCode (EmitContext ec, bool is_expr)
1025 this.is_expr = is_expr;
1026 ((IAssignMethod) expr).EmitAssign (ec, this, is_expr && (mode == Mode.PreIncrement || mode == Mode.PreDecrement), true);
1029 public override void Emit (EmitContext ec)
1032 // We use recurse to allow ourselfs to be the source
1033 // of an assignment. This little hack prevents us from
1034 // having to allocate another expression
1037 ((IAssignMethod) expr).Emit (ec, is_expr && (mode == Mode.PostIncrement || mode == Mode.PostDecrement));
1039 operation.Emit (ec);
1045 EmitCode (ec, true);
1048 public override void EmitStatement (EmitContext ec)
1050 EmitCode (ec, false);
1054 // Converts operator to System.Linq.Expressions.ExpressionType enum name
1056 string GetOperatorExpressionTypeName ()
1058 return IsDecrement ? "Decrement" : "Increment";
1062 get { return (mode & Mode.IsDecrement) != 0; }
1066 // Returns whether an object of type `t' can be incremented
1067 // or decremented with add/sub (ie, basically whether we can
1068 // use pre-post incr-decr operations on it, but it is not a
1069 // System.Decimal, which we require operator overloading to catch)
1071 static bool IsPredefinedOperator (TypeSpec t)
1073 return (TypeManager.IsPrimitiveType (t) && t != TypeManager.bool_type) ||
1074 TypeManager.IsEnumType (t) ||
1075 t.IsPointer && t != TypeManager.void_ptr_type;
1079 public override SLE.Expression MakeExpression (BuilderContext ctx)
1081 var target = ((RuntimeValueExpression) expr).MetaObject.Expression;
1082 var source = SLE.Expression.Convert (operation.MakeExpression (ctx), target.Type);
1083 return SLE.Expression.Assign (target, source);
1087 protected override void CloneTo (CloneContext clonectx, Expression t)
1089 UnaryMutator target = (UnaryMutator) t;
1091 target.expr = expr.Clone (clonectx);
1094 Expression ResolveOperator (ResolveContext ec)
1096 if (expr is RuntimeValueExpression) {
1099 // Use itself at the top of the stack
1100 operation = new EmptyExpression (type);
1104 // The operand of the prefix/postfix increment decrement operators
1105 // should be an expression that is classified as a variable,
1106 // a property access or an indexer access
1108 if (expr.eclass == ExprClass.Variable || expr.eclass == ExprClass.IndexerAccess || expr.eclass == ExprClass.PropertyAccess) {
1109 expr = expr.ResolveLValue (ec, expr);
1111 ec.Report.Error (1059, loc, "The operand of an increment or decrement operator must be a variable, property or indexer");
1115 // 1. Check predefined types
1117 if (IsPredefinedOperator (type)) {
1118 // TODO: Move to IntConstant once I get rid of int32_type
1119 var one = new IntConstant (1, loc);
1121 // TODO: Cache this based on type when using EmptyExpression in
1123 Binary.Operator op = IsDecrement ? Binary.Operator.Subtraction : Binary.Operator.Addition;
1124 operation = new Binary (op, operation, one, loc);
1125 operation = operation.Resolve (ec);
1126 if (operation != null && operation.Type != type)
1127 operation = Convert.ExplicitNumericConversion (operation, type);
1133 // Step 2: Perform Operator Overload location
1138 op_name = Operator.GetMetadataName (Operator.OpType.Decrement);
1140 op_name = Operator.GetMetadataName (Operator.OpType.Increment);
1142 var mg = MethodLookup (ec.Compiler, ec.CurrentType, type, MemberKind.Operator, op_name, 0, loc);
1145 Arguments args = new Arguments (1);
1146 args.Add (new Argument (expr));
1147 mg = mg.OverloadResolve (ec, ref args, false, loc);
1151 args[0].Expr = operation;
1152 operation = new UserOperatorCall (mg, args, null, loc);
1153 operation = Convert.ImplicitConversionRequired (ec, operation, type, loc);
1157 string name = IsDecrement ?
1158 Operator.GetName (Operator.OpType.Decrement) :
1159 Operator.GetName (Operator.OpType.Increment);
1161 Unary.Error_OperatorCannotBeApplied (ec, loc, name, type);
1167 /// Base class for the `Is' and `As' classes.
1171 /// FIXME: Split this in two, and we get to save the `Operator' Oper
1174 public abstract class Probe : Expression {
1175 public Expression ProbeType;
1176 protected Expression expr;
1177 protected TypeExpr probe_type_expr;
1179 public Probe (Expression expr, Expression probe_type, Location l)
1181 ProbeType = probe_type;
1186 public Expression Expr {
1192 protected override Expression DoResolve (ResolveContext ec)
1194 probe_type_expr = ProbeType.ResolveAsTypeTerminal (ec, false);
1195 if (probe_type_expr == null)
1198 expr = expr.Resolve (ec);
1202 if (probe_type_expr.Type.IsStatic) {
1203 ec.Report.Error (-244, loc, "The `{0}' operator cannot be applied to an operand of a static type",
1207 if (expr.Type.IsPointer || probe_type_expr.Type.IsPointer) {
1208 ec.Report.Error (244, loc, "The `{0}' operator cannot be applied to an operand of pointer type",
1213 if (expr.Type == InternalType.AnonymousMethod) {
1214 ec.Report.Error (837, loc, "The `{0}' operator cannot be applied to a lambda expression or anonymous method",
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);
1235 /// Implementation of the `is' operator.
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 if (expr_unwrap != null) {
1257 expr_unwrap.EmitCheck (ec);
1262 ec.Emit (OpCodes.Isinst, probe_type_expr.Type);
1263 ec.Emit (OpCodes.Ldnull);
1264 ec.Emit (OpCodes.Cgt_Un);
1267 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
1269 if (expr_unwrap != null) {
1270 expr_unwrap.EmitCheck (ec);
1273 ec.Emit (OpCodes.Isinst, probe_type_expr.Type);
1275 ec.Emit (on_true ? OpCodes.Brtrue : OpCodes.Brfalse, target);
1278 Expression CreateConstantResult (ResolveContext ec, bool result)
1281 ec.Report.Warning (183, 1, loc, "The given expression is always of the provided (`{0}') type",
1282 TypeManager.CSharpName (probe_type_expr.Type));
1284 ec.Report.Warning (184, 1, loc, "The given expression is never of the provided (`{0}') type",
1285 TypeManager.CSharpName (probe_type_expr.Type));
1287 return ReducedExpression.Create (new BoolConstant (result, loc).Resolve (ec), this);
1290 protected override Expression DoResolve (ResolveContext ec)
1292 if (base.DoResolve (ec) == null)
1295 TypeSpec d = expr.Type;
1296 bool d_is_nullable = false;
1299 // If E is a method group or the null literal, or if the type of E is a reference
1300 // type or a nullable type and the value of E is null, the result is false
1302 if (expr.IsNull || expr.eclass == ExprClass.MethodGroup)
1303 return CreateConstantResult (ec, false);
1305 if (TypeManager.IsNullableType (d)) {
1306 var ut = Nullable.NullableInfo.GetUnderlyingType (d);
1307 if (!ut.IsGenericParameter) {
1309 d_is_nullable = true;
1313 type = TypeManager.bool_type;
1314 eclass = ExprClass.Value;
1315 TypeSpec t = probe_type_expr.Type;
1316 bool t_is_nullable = false;
1317 if (TypeManager.IsNullableType (t)) {
1318 var ut = Nullable.NullableInfo.GetUnderlyingType (t);
1319 if (!ut.IsGenericParameter) {
1321 t_is_nullable = true;
1325 if (TypeManager.IsStruct (t)) {
1328 // D and T are the same value types but D can be null
1330 if (d_is_nullable && !t_is_nullable) {
1331 expr_unwrap = Nullable.Unwrap.Create (expr, false);
1336 // The result is true if D and T are the same value types
1338 return CreateConstantResult (ec, true);
1341 var tp = d as TypeParameterSpec;
1343 return ResolveGenericParameter (ec, t, tp);
1346 // An unboxing conversion exists
1348 if (Convert.ExplicitReferenceConversionExists (d, t))
1351 if (TypeManager.IsGenericParameter (t))
1352 return ResolveGenericParameter (ec, d, (TypeParameterSpec) t);
1354 if (TypeManager.IsStruct (d)) {
1356 if (Convert.ImplicitBoxingConversionExists (expr, t, out temp))
1357 return CreateConstantResult (ec, true);
1359 if (TypeManager.IsGenericParameter (d))
1360 return ResolveGenericParameter (ec, t, (TypeParameterSpec) d);
1362 if (TypeManager.ContainsGenericParameters (d))
1365 if (Convert.ImplicitReferenceConversionExists (expr, t) ||
1366 Convert.ExplicitReferenceConversionExists (d, t)) {
1372 return CreateConstantResult (ec, false);
1375 Expression ResolveGenericParameter (ResolveContext ec, TypeSpec d, TypeParameterSpec t)
1377 if (t.IsReferenceType) {
1378 if (TypeManager.IsStruct (d))
1379 return CreateConstantResult (ec, false);
1382 if (TypeManager.IsGenericParameter (expr.Type)) {
1383 if (t.IsValueType && expr.Type == t)
1384 return CreateConstantResult (ec, true);
1386 expr = new BoxedCast (expr, d);
1392 protected override string OperatorName {
1393 get { return "is"; }
1398 /// Implementation of the `as' operator.
1400 public class As : Probe {
1402 Expression resolved_type;
1404 public As (Expression expr, Expression probe_type, Location l)
1405 : base (expr, probe_type, l)
1409 public override Expression CreateExpressionTree (ResolveContext ec)
1411 Arguments args = Arguments.CreateForExpressionTree (ec, null,
1412 expr.CreateExpressionTree (ec),
1413 new TypeOf (probe_type_expr, loc));
1415 return CreateExpressionFactoryCall (ec, "TypeAs", args);
1418 public override void Emit (EmitContext ec)
1423 ec.Emit (OpCodes.Isinst, type);
1425 if (TypeManager.IsGenericParameter (type) || TypeManager.IsNullableType (type))
1426 ec.Emit (OpCodes.Unbox_Any, type);
1429 protected override Expression DoResolve (ResolveContext ec)
1431 if (resolved_type == null) {
1432 resolved_type = base.DoResolve (ec);
1434 if (resolved_type == null)
1438 type = probe_type_expr.Type;
1439 eclass = ExprClass.Value;
1440 TypeSpec etype = expr.Type;
1442 if (!TypeManager.IsReferenceType (type) && !TypeManager.IsNullableType (type)) {
1443 if (TypeManager.IsGenericParameter (type)) {
1444 ec.Report.Error (413, loc,
1445 "The `as' operator cannot be used with a non-reference type parameter `{0}'. Consider adding `class' or a reference type constraint",
1446 probe_type_expr.GetSignatureForError ());
1448 ec.Report.Error (77, loc,
1449 "The `as' operator cannot be used with a non-nullable value type `{0}'",
1450 TypeManager.CSharpName (type));
1455 if (expr.IsNull && TypeManager.IsNullableType (type)) {
1456 return Nullable.LiftedNull.CreateFromExpression (ec, this);
1459 Expression e = Convert.ImplicitConversion (ec, expr, type, loc);
1466 if (Convert.ExplicitReferenceConversionExists (etype, type)){
1467 if (TypeManager.IsGenericParameter (etype))
1468 expr = new BoxedCast (expr, etype);
1474 if (TypeManager.ContainsGenericParameters (etype) ||
1475 TypeManager.ContainsGenericParameters (type)) {
1476 expr = new BoxedCast (expr, etype);
1481 ec.Report.Error (39, loc, "Cannot convert type `{0}' to `{1}' via a built-in conversion",
1482 TypeManager.CSharpName (etype), TypeManager.CSharpName (type));
1487 protected override string OperatorName {
1488 get { return "as"; }
1493 /// This represents a typecast in the source language.
1495 /// FIXME: Cast expressions have an unusual set of parsing
1496 /// rules, we need to figure those out.
1498 public class Cast : ShimExpression {
1499 Expression target_type;
1501 public Cast (Expression cast_type, Expression expr)
1502 : this (cast_type, expr, cast_type.Location)
1506 public Cast (Expression cast_type, Expression expr, Location loc)
1509 this.target_type = cast_type;
1513 public Expression TargetType {
1514 get { return target_type; }
1517 protected override Expression DoResolve (ResolveContext ec)
1519 expr = expr.Resolve (ec);
1523 TypeExpr target = target_type.ResolveAsTypeTerminal (ec, false);
1529 if (type.IsStatic) {
1530 ec.Report.Error (716, loc, "Cannot convert to static type `{0}'", TypeManager.CSharpName (type));
1534 eclass = ExprClass.Value;
1536 Constant c = expr as Constant;
1538 c = c.TryReduce (ec, type, loc);
1543 if (type.IsPointer && !ec.IsUnsafe) {
1544 UnsafeError (ec, loc);
1545 } else if (expr.Type == InternalType.Dynamic) {
1546 Arguments arg = new Arguments (1);
1547 arg.Add (new Argument (expr));
1548 return new DynamicConversion (type, CSharpBinderFlags.ConvertExplicit, arg, loc).Resolve (ec);
1551 expr = Convert.ExplicitConversion (ec, expr, type, loc);
1555 protected override void CloneTo (CloneContext clonectx, Expression t)
1557 Cast target = (Cast) t;
1559 target.target_type = target_type.Clone (clonectx);
1560 target.expr = expr.Clone (clonectx);
1564 public class ImplicitCast : ShimExpression
1568 public ImplicitCast (Expression expr, TypeSpec target, bool arrayAccess)
1571 this.loc = expr.Location;
1573 this.arrayAccess = arrayAccess;
1576 protected override Expression DoResolve (ResolveContext ec)
1578 expr = expr.Resolve (ec);
1583 expr = ConvertExpressionToArrayIndex (ec, expr);
1585 expr = Convert.ImplicitConversionRequired (ec, expr, type, loc);
1592 // C# 2.0 Default value expression
1594 public class DefaultValueExpression : Expression
1598 public DefaultValueExpression (Expression expr, Location loc)
1604 public override Expression CreateExpressionTree (ResolveContext ec)
1606 Arguments args = new Arguments (2);
1607 args.Add (new Argument (this));
1608 args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
1609 return CreateExpressionFactoryCall (ec, "Constant", args);
1612 protected override Expression DoResolve (ResolveContext ec)
1614 TypeExpr texpr = expr.ResolveAsTypeTerminal (ec, false);
1620 if (type.IsStatic) {
1621 ec.Report.Error (-244, loc, "The `default value' operator cannot be applied to an operand of a static type");
1625 return new NullLiteral (Location).ConvertImplicitly (ec, type);
1627 if (TypeManager.IsReferenceType (type))
1628 return new NullConstant (type, loc);
1630 Constant c = New.Constantify (type);
1632 return c.Resolve (ec);
1634 eclass = ExprClass.Variable;
1638 public override void Emit (EmitContext ec)
1640 LocalTemporary temp_storage = new LocalTemporary(type);
1642 temp_storage.AddressOf(ec, AddressOp.LoadStore);
1643 ec.Emit(OpCodes.Initobj, type);
1644 temp_storage.Emit(ec);
1647 protected override void CloneTo (CloneContext clonectx, Expression t)
1649 DefaultValueExpression target = (DefaultValueExpression) t;
1651 target.expr = expr.Clone (clonectx);
1656 /// Binary operators
1658 public class Binary : Expression, IDynamicBinder
1660 protected class PredefinedOperator {
1661 protected readonly TypeSpec left;
1662 protected readonly TypeSpec right;
1663 public readonly Operator OperatorsMask;
1664 public TypeSpec ReturnType;
1666 public PredefinedOperator (TypeSpec ltype, TypeSpec rtype, Operator op_mask)
1667 : this (ltype, rtype, op_mask, ltype)
1671 public PredefinedOperator (TypeSpec type, Operator op_mask, TypeSpec return_type)
1672 : this (type, type, op_mask, return_type)
1676 public PredefinedOperator (TypeSpec type, Operator op_mask)
1677 : this (type, type, op_mask, type)
1681 public PredefinedOperator (TypeSpec ltype, TypeSpec rtype, Operator op_mask, TypeSpec return_type)
1683 if ((op_mask & Operator.ValuesOnlyMask) != 0)
1684 throw new InternalErrorException ("Only masked values can be used");
1688 this.OperatorsMask = op_mask;
1689 this.ReturnType = return_type;
1692 public virtual Expression ConvertResult (ResolveContext ec, Binary b)
1694 b.type = ReturnType;
1696 b.left = Convert.ImplicitConversion (ec, b.left, left, b.left.Location);
1697 b.right = Convert.ImplicitConversion (ec, b.right, right, b.right.Location);
1700 // A user operators does not support multiple user conversions, but decimal type
1701 // is considered to be predefined type therefore we apply predefined operators rules
1702 // and then look for decimal user-operator implementation
1704 if (left == TypeManager.decimal_type)
1705 return b.ResolveUserOperator (ec, b.left.Type, b.right.Type);
1707 var c = b.right as Constant;
1709 if (c.IsDefaultValue && (b.oper == Operator.Addition || b.oper == Operator.BitwiseOr || b.oper == Operator.Subtraction))
1710 return ReducedExpression.Create (b.left, b).Resolve (ec);
1711 if ((b.oper == Operator.Multiply || b.oper == Operator.Division) && c.IsOneInteger)
1712 return ReducedExpression.Create (b.left, b).Resolve (ec);
1716 c = b.left as Constant;
1718 if (c.IsDefaultValue && (b.oper == Operator.Addition || b.oper == Operator.BitwiseOr))
1719 return ReducedExpression.Create (b.right, b).Resolve (ec);
1720 if (b.oper == Operator.Multiply && c.IsOneInteger)
1721 return ReducedExpression.Create (b.right, b).Resolve (ec);
1728 public bool IsPrimitiveApplicable (TypeSpec ltype, TypeSpec rtype)
1731 // We are dealing with primitive types only
1733 return left == ltype && ltype == rtype;
1736 public virtual bool IsApplicable (ResolveContext ec, Expression lexpr, Expression rexpr)
1738 if (TypeManager.IsEqual (left, lexpr.Type) &&
1739 TypeManager.IsEqual (right, rexpr.Type))
1742 return Convert.ImplicitConversionExists (ec, lexpr, left) &&
1743 Convert.ImplicitConversionExists (ec, rexpr, right);
1746 public PredefinedOperator ResolveBetterOperator (ResolveContext ec, PredefinedOperator best_operator)
1749 if (left != null && best_operator.left != null) {
1750 result = MethodGroupExpr.BetterTypeConversion (ec, best_operator.left, left);
1754 // When second arguments are same as the first one, the result is same
1756 if (right != null && (left != right || best_operator.left != best_operator.right)) {
1757 result |= MethodGroupExpr.BetterTypeConversion (ec, best_operator.right, right);
1760 if (result == 0 || result > 2)
1763 return result == 1 ? best_operator : this;
1767 class PredefinedStringOperator : PredefinedOperator {
1768 public PredefinedStringOperator (TypeSpec type, Operator op_mask)
1769 : base (type, op_mask, type)
1771 ReturnType = TypeManager.string_type;
1774 public PredefinedStringOperator (TypeSpec ltype, TypeSpec rtype, Operator op_mask)
1775 : base (ltype, rtype, op_mask)
1777 ReturnType = TypeManager.string_type;
1780 public override Expression ConvertResult (ResolveContext ec, Binary b)
1783 // Use original expression for nullable arguments
1785 Nullable.Unwrap unwrap = b.left as Nullable.Unwrap;
1787 b.left = unwrap.Original;
1789 unwrap = b.right as Nullable.Unwrap;
1791 b.right = unwrap.Original;
1793 b.left = Convert.ImplicitConversion (ec, b.left, left, b.left.Location);
1794 b.right = Convert.ImplicitConversion (ec, b.right, right, b.right.Location);
1797 // Start a new concat expression using converted expression
1799 return StringConcat.Create (ec, b.left, b.right, b.loc);
1803 class PredefinedShiftOperator : PredefinedOperator {
1804 public PredefinedShiftOperator (TypeSpec ltype, Operator op_mask) :
1805 base (ltype, TypeManager.int32_type, op_mask)
1809 public override Expression ConvertResult (ResolveContext ec, Binary b)
1811 b.left = Convert.ImplicitConversion (ec, b.left, left, b.left.Location);
1813 Expression expr_tree_expr = Convert.ImplicitConversion (ec, b.right, TypeManager.int32_type, b.right.Location);
1815 int right_mask = left == TypeManager.int32_type || left == TypeManager.uint32_type ? 0x1f : 0x3f;
1818 // b = b.left >> b.right & (0x1f|0x3f)
1820 b.right = new Binary (Operator.BitwiseAnd,
1821 b.right, new IntConstant (right_mask, b.right.Location), b.loc).Resolve (ec);
1824 // Expression tree representation does not use & mask
1826 b.right = ReducedExpression.Create (b.right, expr_tree_expr).Resolve (ec);
1827 b.type = ReturnType;
1830 // Optimize shift by 0
1832 var c = b.right as Constant;
1833 if (c != null && c.IsDefaultValue)
1834 return ReducedExpression.Create (b.left, b).Resolve (ec);
1840 class PredefinedPointerOperator : PredefinedOperator {
1841 public PredefinedPointerOperator (TypeSpec ltype, TypeSpec rtype, Operator op_mask)
1842 : base (ltype, rtype, op_mask)
1846 public PredefinedPointerOperator (TypeSpec ltype, TypeSpec rtype, Operator op_mask, TypeSpec retType)
1847 : base (ltype, rtype, op_mask, retType)
1851 public PredefinedPointerOperator (TypeSpec type, Operator op_mask, TypeSpec return_type)
1852 : base (type, op_mask, return_type)
1856 public override bool IsApplicable (ResolveContext ec, Expression lexpr, Expression rexpr)
1859 if (!lexpr.Type.IsPointer)
1862 if (!Convert.ImplicitConversionExists (ec, lexpr, left))
1866 if (right == null) {
1867 if (!rexpr.Type.IsPointer)
1870 if (!Convert.ImplicitConversionExists (ec, rexpr, right))
1877 public override Expression ConvertResult (ResolveContext ec, Binary b)
1880 b.left = EmptyCast.Create (b.left, left);
1881 } else if (right != null) {
1882 b.right = EmptyCast.Create (b.right, right);
1885 TypeSpec r_type = ReturnType;
1886 Expression left_arg, right_arg;
1887 if (r_type == null) {
1890 right_arg = b.right;
1891 r_type = b.left.Type;
1895 r_type = b.right.Type;
1899 right_arg = b.right;
1902 return new PointerArithmetic (b.oper, left_arg, right_arg, r_type, b.loc).Resolve (ec);
1907 public enum Operator {
1908 Multiply = 0 | ArithmeticMask,
1909 Division = 1 | ArithmeticMask,
1910 Modulus = 2 | ArithmeticMask,
1911 Addition = 3 | ArithmeticMask | AdditionMask,
1912 Subtraction = 4 | ArithmeticMask | SubtractionMask,
1914 LeftShift = 5 | ShiftMask,
1915 RightShift = 6 | ShiftMask,
1917 LessThan = 7 | ComparisonMask | RelationalMask,
1918 GreaterThan = 8 | ComparisonMask | RelationalMask,
1919 LessThanOrEqual = 9 | ComparisonMask | RelationalMask,
1920 GreaterThanOrEqual = 10 | ComparisonMask | RelationalMask,
1921 Equality = 11 | ComparisonMask | EqualityMask,
1922 Inequality = 12 | ComparisonMask | EqualityMask,
1924 BitwiseAnd = 13 | BitwiseMask,
1925 ExclusiveOr = 14 | BitwiseMask,
1926 BitwiseOr = 15 | BitwiseMask,
1928 LogicalAnd = 16 | LogicalMask,
1929 LogicalOr = 17 | LogicalMask,
1934 ValuesOnlyMask = ArithmeticMask - 1,
1935 ArithmeticMask = 1 << 5,
1937 ComparisonMask = 1 << 7,
1938 EqualityMask = 1 << 8,
1939 BitwiseMask = 1 << 9,
1940 LogicalMask = 1 << 10,
1941 AdditionMask = 1 << 11,
1942 SubtractionMask = 1 << 12,
1943 RelationalMask = 1 << 13
1946 readonly Operator oper;
1947 protected Expression left, right;
1948 readonly bool is_compound;
1949 Expression enum_conversion;
1951 static PredefinedOperator[] standard_operators;
1952 static PredefinedOperator[] pointer_operators;
1954 public Binary (Operator oper, Expression left, Expression right, bool isCompound, Location loc)
1955 : this (oper, left, right, loc)
1957 this.is_compound = isCompound;
1960 public Binary (Operator oper, Expression left, Expression right, Location loc)
1968 public Operator Oper {
1975 /// Returns a stringified representation of the Operator
1977 string OperName (Operator oper)
1981 case Operator.Multiply:
1984 case Operator.Division:
1987 case Operator.Modulus:
1990 case Operator.Addition:
1993 case Operator.Subtraction:
1996 case Operator.LeftShift:
1999 case Operator.RightShift:
2002 case Operator.LessThan:
2005 case Operator.GreaterThan:
2008 case Operator.LessThanOrEqual:
2011 case Operator.GreaterThanOrEqual:
2014 case Operator.Equality:
2017 case Operator.Inequality:
2020 case Operator.BitwiseAnd:
2023 case Operator.BitwiseOr:
2026 case Operator.ExclusiveOr:
2029 case Operator.LogicalOr:
2032 case Operator.LogicalAnd:
2036 s = oper.ToString ();
2046 public static void Error_OperatorCannotBeApplied (ResolveContext ec, Expression left, Expression right, Operator oper, Location loc)
2048 new Binary (oper, left, right, loc).Error_OperatorCannotBeApplied (ec, left, right);
2051 public static void Error_OperatorCannotBeApplied (ResolveContext ec, Expression left, Expression right, string oper, Location loc)
2054 l = TypeManager.CSharpName (left.Type);
2055 r = TypeManager.CSharpName (right.Type);
2057 ec.Report.Error (19, loc, "Operator `{0}' cannot be applied to operands of type `{1}' and `{2}'",
2061 protected void Error_OperatorCannotBeApplied (ResolveContext ec, Expression left, Expression right)
2063 Error_OperatorCannotBeApplied (ec, left, right, OperName (oper), loc);
2067 // Converts operator to System.Linq.Expressions.ExpressionType enum name
2069 string GetOperatorExpressionTypeName ()
2072 case Operator.Addition:
2073 return is_compound ? "AddAssign" : "Add";
2074 case Operator.BitwiseAnd:
2075 return is_compound ? "AndAssign" : "And";
2076 case Operator.BitwiseOr:
2077 return is_compound ? "OrAssign" : "Or";
2078 case Operator.Division:
2079 return is_compound ? "DivideAssign" : "Divide";
2080 case Operator.ExclusiveOr:
2081 return is_compound ? "ExclusiveOrAssign" : "ExclusiveOr";
2082 case Operator.Equality:
2084 case Operator.GreaterThan:
2085 return "GreaterThan";
2086 case Operator.GreaterThanOrEqual:
2087 return "GreaterThanOrEqual";
2088 case Operator.Inequality:
2090 case Operator.LeftShift:
2091 return is_compound ? "LeftShiftAssign" : "LeftShift";
2092 case Operator.LessThan:
2094 case Operator.LessThanOrEqual:
2095 return "LessThanOrEqual";
2096 case Operator.LogicalAnd:
2098 case Operator.LogicalOr:
2100 case Operator.Modulus:
2101 return is_compound ? "ModuloAssign" : "Modulo";
2102 case Operator.Multiply:
2103 return is_compound ? "MultiplyAssign" : "Multiply";
2104 case Operator.RightShift:
2105 return is_compound ? "RightShiftAssign" : "RightShift";
2106 case Operator.Subtraction:
2107 return is_compound ? "SubtractAssign" : "Subtract";
2109 throw new NotImplementedException ("Unknown expression type operator " + oper.ToString ());
2113 static string GetOperatorMetadataName (Operator op)
2115 CSharp.Operator.OpType op_type;
2117 case Operator.Addition:
2118 op_type = CSharp.Operator.OpType.Addition; break;
2119 case Operator.BitwiseAnd:
2120 op_type = CSharp.Operator.OpType.BitwiseAnd; break;
2121 case Operator.BitwiseOr:
2122 op_type = CSharp.Operator.OpType.BitwiseOr; break;
2123 case Operator.Division:
2124 op_type = CSharp.Operator.OpType.Division; break;
2125 case Operator.Equality:
2126 op_type = CSharp.Operator.OpType.Equality; break;
2127 case Operator.ExclusiveOr:
2128 op_type = CSharp.Operator.OpType.ExclusiveOr; break;
2129 case Operator.GreaterThan:
2130 op_type = CSharp.Operator.OpType.GreaterThan; break;
2131 case Operator.GreaterThanOrEqual:
2132 op_type = CSharp.Operator.OpType.GreaterThanOrEqual; break;
2133 case Operator.Inequality:
2134 op_type = CSharp.Operator.OpType.Inequality; break;
2135 case Operator.LeftShift:
2136 op_type = CSharp.Operator.OpType.LeftShift; break;
2137 case Operator.LessThan:
2138 op_type = CSharp.Operator.OpType.LessThan; break;
2139 case Operator.LessThanOrEqual:
2140 op_type = CSharp.Operator.OpType.LessThanOrEqual; break;
2141 case Operator.Modulus:
2142 op_type = CSharp.Operator.OpType.Modulus; break;
2143 case Operator.Multiply:
2144 op_type = CSharp.Operator.OpType.Multiply; break;
2145 case Operator.RightShift:
2146 op_type = CSharp.Operator.OpType.RightShift; break;
2147 case Operator.Subtraction:
2148 op_type = CSharp.Operator.OpType.Subtraction; break;
2150 throw new InternalErrorException (op.ToString ());
2153 return CSharp.Operator.GetMetadataName (op_type);
2156 public static void EmitOperatorOpcode (EmitContext ec, Operator oper, TypeSpec l)
2161 case Operator.Multiply:
2162 if (ec.HasSet (EmitContext.Options.CheckedScope)) {
2163 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2164 opcode = OpCodes.Mul_Ovf;
2165 else if (!IsFloat (l))
2166 opcode = OpCodes.Mul_Ovf_Un;
2168 opcode = OpCodes.Mul;
2170 opcode = OpCodes.Mul;
2174 case Operator.Division:
2176 opcode = OpCodes.Div_Un;
2178 opcode = OpCodes.Div;
2181 case Operator.Modulus:
2183 opcode = OpCodes.Rem_Un;
2185 opcode = OpCodes.Rem;
2188 case Operator.Addition:
2189 if (ec.HasSet (EmitContext.Options.CheckedScope)) {
2190 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2191 opcode = OpCodes.Add_Ovf;
2192 else if (!IsFloat (l))
2193 opcode = OpCodes.Add_Ovf_Un;
2195 opcode = OpCodes.Add;
2197 opcode = OpCodes.Add;
2200 case Operator.Subtraction:
2201 if (ec.HasSet (EmitContext.Options.CheckedScope)) {
2202 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2203 opcode = OpCodes.Sub_Ovf;
2204 else if (!IsFloat (l))
2205 opcode = OpCodes.Sub_Ovf_Un;
2207 opcode = OpCodes.Sub;
2209 opcode = OpCodes.Sub;
2212 case Operator.RightShift:
2214 opcode = OpCodes.Shr_Un;
2216 opcode = OpCodes.Shr;
2219 case Operator.LeftShift:
2220 opcode = OpCodes.Shl;
2223 case Operator.Equality:
2224 opcode = OpCodes.Ceq;
2227 case Operator.Inequality:
2228 ec.Emit (OpCodes.Ceq);
2229 ec.Emit (OpCodes.Ldc_I4_0);
2231 opcode = OpCodes.Ceq;
2234 case Operator.LessThan:
2236 opcode = OpCodes.Clt_Un;
2238 opcode = OpCodes.Clt;
2241 case Operator.GreaterThan:
2243 opcode = OpCodes.Cgt_Un;
2245 opcode = OpCodes.Cgt;
2248 case Operator.LessThanOrEqual:
2249 if (IsUnsigned (l) || IsFloat (l))
2250 ec.Emit (OpCodes.Cgt_Un);
2252 ec.Emit (OpCodes.Cgt);
2253 ec.Emit (OpCodes.Ldc_I4_0);
2255 opcode = OpCodes.Ceq;
2258 case Operator.GreaterThanOrEqual:
2259 if (IsUnsigned (l) || IsFloat (l))
2260 ec.Emit (OpCodes.Clt_Un);
2262 ec.Emit (OpCodes.Clt);
2264 ec.Emit (OpCodes.Ldc_I4_0);
2266 opcode = OpCodes.Ceq;
2269 case Operator.BitwiseOr:
2270 opcode = OpCodes.Or;
2273 case Operator.BitwiseAnd:
2274 opcode = OpCodes.And;
2277 case Operator.ExclusiveOr:
2278 opcode = OpCodes.Xor;
2282 throw new InternalErrorException (oper.ToString ());
2288 static bool IsUnsigned (TypeSpec t)
2293 return (t == TypeManager.uint32_type || t == TypeManager.uint64_type ||
2294 t == TypeManager.ushort_type || t == TypeManager.byte_type);
2297 static bool IsFloat (TypeSpec t)
2299 return t == TypeManager.float_type || t == TypeManager.double_type;
2302 public static void Reset ()
2304 pointer_operators = standard_operators = null;
2307 Expression ResolveOperator (ResolveContext ec)
2309 TypeSpec l = left.Type;
2310 TypeSpec r = right.Type;
2312 bool primitives_only = false;
2314 if (standard_operators == null)
2315 CreateStandardOperatorsTable ();
2318 // Handles predefined primitive types
2320 if (TypeManager.IsPrimitiveType (l) && TypeManager.IsPrimitiveType (r)) {
2321 if ((oper & Operator.ShiftMask) == 0) {
2322 if (l != TypeManager.bool_type && !DoBinaryOperatorPromotion (ec))
2325 primitives_only = true;
2329 if (l.IsPointer || r.IsPointer)
2330 return ResolveOperatorPointer (ec, l, r);
2333 bool lenum = TypeManager.IsEnumType (l);
2334 bool renum = TypeManager.IsEnumType (r);
2335 if (lenum || renum) {
2336 expr = ResolveOperatorEnum (ec, lenum, renum, l, r);
2338 // TODO: Can this be ambiguous
2344 if ((oper == Operator.Addition || oper == Operator.Subtraction || (oper & Operator.EqualityMask) != 0) &&
2345 (TypeManager.IsDelegateType (l) || TypeManager.IsDelegateType (r))) {
2347 expr = ResolveOperatorDelegate (ec, l, r);
2349 // TODO: Can this be ambiguous
2355 expr = ResolveUserOperator (ec, l, r);
2359 // Predefined reference types equality
2360 if ((oper & Operator.EqualityMask) != 0) {
2361 expr = ResolveOperatorEqualityRerefence (ec, l, r);
2367 return ResolveOperatorPredefined (ec, standard_operators, primitives_only, null);
2370 // at least one of 'left' or 'right' is an enumeration constant (EnumConstant or SideEffectConstant or ...)
2371 // if 'left' is not an enumeration constant, create one from the type of 'right'
2372 Constant EnumLiftUp (ResolveContext ec, Constant left, Constant right, Location loc)
2375 case Operator.BitwiseOr:
2376 case Operator.BitwiseAnd:
2377 case Operator.ExclusiveOr:
2378 case Operator.Equality:
2379 case Operator.Inequality:
2380 case Operator.LessThan:
2381 case Operator.LessThanOrEqual:
2382 case Operator.GreaterThan:
2383 case Operator.GreaterThanOrEqual:
2384 if (TypeManager.IsEnumType (left.Type))
2387 if (left.IsZeroInteger)
2388 return left.TryReduce (ec, right.Type, loc);
2392 case Operator.Addition:
2393 case Operator.Subtraction:
2396 case Operator.Multiply:
2397 case Operator.Division:
2398 case Operator.Modulus:
2399 case Operator.LeftShift:
2400 case Operator.RightShift:
2401 if (TypeManager.IsEnumType (right.Type) || TypeManager.IsEnumType (left.Type))
2405 Error_OperatorCannotBeApplied (ec, this.left, this.right);
2410 // The `|' operator used on types which were extended is dangerous
2412 void CheckBitwiseOrOnSignExtended (ResolveContext ec)
2414 OpcodeCast lcast = left as OpcodeCast;
2415 if (lcast != null) {
2416 if (IsUnsigned (lcast.UnderlyingType))
2420 OpcodeCast rcast = right as OpcodeCast;
2421 if (rcast != null) {
2422 if (IsUnsigned (rcast.UnderlyingType))
2426 if (lcast == null && rcast == null)
2429 // FIXME: consider constants
2431 ec.Report.Warning (675, 3, loc,
2432 "The operator `|' used on the sign-extended type `{0}'. Consider casting to a smaller unsigned type first",
2433 TypeManager.CSharpName (lcast != null ? lcast.UnderlyingType : rcast.UnderlyingType));
2436 static void CreatePointerOperatorsTable ()
2438 var temp = new List<PredefinedPointerOperator> ();
2441 // Pointer arithmetic:
2443 // T* operator + (T* x, int y); T* operator - (T* x, int y);
2444 // T* operator + (T* x, uint y); T* operator - (T* x, uint y);
2445 // T* operator + (T* x, long y); T* operator - (T* x, long y);
2446 // T* operator + (T* x, ulong y); T* operator - (T* x, ulong y);
2448 temp.Add (new PredefinedPointerOperator (null, TypeManager.int32_type, Operator.AdditionMask | Operator.SubtractionMask));
2449 temp.Add (new PredefinedPointerOperator (null, TypeManager.uint32_type, Operator.AdditionMask | Operator.SubtractionMask));
2450 temp.Add (new PredefinedPointerOperator (null, TypeManager.int64_type, Operator.AdditionMask | Operator.SubtractionMask));
2451 temp.Add (new PredefinedPointerOperator (null, TypeManager.uint64_type, Operator.AdditionMask | Operator.SubtractionMask));
2454 // T* operator + (int y, T* x);
2455 // T* operator + (uint y, T *x);
2456 // T* operator + (long y, T *x);
2457 // T* operator + (ulong y, T *x);
2459 temp.Add (new PredefinedPointerOperator (TypeManager.int32_type, null, Operator.AdditionMask, null));
2460 temp.Add (new PredefinedPointerOperator (TypeManager.uint32_type, null, Operator.AdditionMask, null));
2461 temp.Add (new PredefinedPointerOperator (TypeManager.int64_type, null, Operator.AdditionMask, null));
2462 temp.Add (new PredefinedPointerOperator (TypeManager.uint64_type, null, Operator.AdditionMask, null));
2465 // long operator - (T* x, T *y)
2467 temp.Add (new PredefinedPointerOperator (null, Operator.SubtractionMask, TypeManager.int64_type));
2469 pointer_operators = temp.ToArray ();
2472 static void CreateStandardOperatorsTable ()
2474 var temp = new List<PredefinedOperator> ();
2475 TypeSpec bool_type = TypeManager.bool_type;
2477 temp.Add (new PredefinedOperator (TypeManager.int32_type, Operator.ArithmeticMask | Operator.BitwiseMask));
2478 temp.Add (new PredefinedOperator (TypeManager.uint32_type, Operator.ArithmeticMask | Operator.BitwiseMask));
2479 temp.Add (new PredefinedOperator (TypeManager.int64_type, Operator.ArithmeticMask | Operator.BitwiseMask));
2480 temp.Add (new PredefinedOperator (TypeManager.uint64_type, Operator.ArithmeticMask | Operator.BitwiseMask));
2481 temp.Add (new PredefinedOperator (TypeManager.float_type, Operator.ArithmeticMask));
2482 temp.Add (new PredefinedOperator (TypeManager.double_type, Operator.ArithmeticMask));
2483 temp.Add (new PredefinedOperator (TypeManager.decimal_type, Operator.ArithmeticMask));
2485 temp.Add (new PredefinedOperator (TypeManager.int32_type, Operator.ComparisonMask, bool_type));
2486 temp.Add (new PredefinedOperator (TypeManager.uint32_type, Operator.ComparisonMask, bool_type));
2487 temp.Add (new PredefinedOperator (TypeManager.int64_type, Operator.ComparisonMask, bool_type));
2488 temp.Add (new PredefinedOperator (TypeManager.uint64_type, Operator.ComparisonMask, bool_type));
2489 temp.Add (new PredefinedOperator (TypeManager.float_type, Operator.ComparisonMask, bool_type));
2490 temp.Add (new PredefinedOperator (TypeManager.double_type, Operator.ComparisonMask, bool_type));
2491 temp.Add (new PredefinedOperator (TypeManager.decimal_type, Operator.ComparisonMask, bool_type));
2493 temp.Add (new PredefinedOperator (TypeManager.string_type, Operator.EqualityMask, bool_type));
2495 temp.Add (new PredefinedStringOperator (TypeManager.string_type, Operator.AdditionMask));
2496 temp.Add (new PredefinedStringOperator (TypeManager.string_type, TypeManager.object_type, Operator.AdditionMask));
2497 temp.Add (new PredefinedStringOperator (TypeManager.object_type, TypeManager.string_type, Operator.AdditionMask));
2499 temp.Add (new PredefinedOperator (bool_type,
2500 Operator.BitwiseMask | Operator.LogicalMask | Operator.EqualityMask, bool_type));
2502 temp.Add (new PredefinedShiftOperator (TypeManager.int32_type, Operator.ShiftMask));
2503 temp.Add (new PredefinedShiftOperator (TypeManager.uint32_type, Operator.ShiftMask));
2504 temp.Add (new PredefinedShiftOperator (TypeManager.int64_type, Operator.ShiftMask));
2505 temp.Add (new PredefinedShiftOperator (TypeManager.uint64_type, Operator.ShiftMask));
2507 standard_operators = temp.ToArray ();
2511 // Rules used during binary numeric promotion
2513 static bool DoNumericPromotion (ResolveContext rc, ref Expression prim_expr, ref Expression second_expr, TypeSpec type)
2518 Constant c = prim_expr as Constant;
2520 temp = c.ConvertImplicitly (rc, type);
2527 if (type == TypeManager.uint32_type) {
2528 etype = prim_expr.Type;
2529 if (etype == TypeManager.int32_type || etype == TypeManager.short_type || etype == TypeManager.sbyte_type) {
2530 type = TypeManager.int64_type;
2532 if (type != second_expr.Type) {
2533 c = second_expr as Constant;
2535 temp = c.ConvertImplicitly (rc, type);
2537 temp = Convert.ImplicitNumericConversion (second_expr, type);
2543 } else if (type == TypeManager.uint64_type) {
2545 // A compile-time error occurs if the other operand is of type sbyte, short, int, or long
2547 if (type == TypeManager.int32_type || type == TypeManager.int64_type ||
2548 type == TypeManager.short_type || type == TypeManager.sbyte_type)
2552 temp = Convert.ImplicitNumericConversion (prim_expr, type);
2561 // 7.2.6.2 Binary numeric promotions
2563 public bool DoBinaryOperatorPromotion (ResolveContext ec)
2565 TypeSpec ltype = left.Type;
2566 TypeSpec rtype = right.Type;
2569 foreach (TypeSpec t in ConstantFold.BinaryPromotionsTypes) {
2571 return t == rtype || DoNumericPromotion (ec, ref right, ref left, t);
2574 return t == ltype || DoNumericPromotion (ec, ref left, ref right, t);
2577 TypeSpec int32 = TypeManager.int32_type;
2578 if (ltype != int32) {
2579 Constant c = left as Constant;
2581 temp = c.ConvertImplicitly (ec, int32);
2583 temp = Convert.ImplicitNumericConversion (left, int32);
2590 if (rtype != int32) {
2591 Constant c = right as Constant;
2593 temp = c.ConvertImplicitly (ec, int32);
2595 temp = Convert.ImplicitNumericConversion (right, int32);
2605 protected override Expression DoResolve (ResolveContext ec)
2610 if ((oper == Operator.Subtraction) && (left is ParenthesizedExpression)) {
2611 left = ((ParenthesizedExpression) left).Expr;
2612 left = left.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.Type);
2616 if (left.eclass == ExprClass.Type) {
2617 ec.Report.Error (75, loc, "To cast a negative value, you must enclose the value in parentheses");
2621 left = left.Resolve (ec);
2626 Constant lc = left as Constant;
2628 if (lc != null && lc.Type == TypeManager.bool_type &&
2629 ((oper == Operator.LogicalAnd && lc.IsDefaultValue) ||
2630 (oper == Operator.LogicalOr && !lc.IsDefaultValue))) {
2632 // FIXME: resolve right expression as unreachable
2633 // right.Resolve (ec);
2635 ec.Report.Warning (429, 4, loc, "Unreachable expression code detected");
2639 right = right.Resolve (ec);
2643 eclass = ExprClass.Value;
2644 Constant rc = right as Constant;
2646 // The conversion rules are ignored in enum context but why
2647 if (!ec.HasSet (ResolveContext.Options.EnumScope) && lc != null && rc != null && (TypeManager.IsEnumType (left.Type) || TypeManager.IsEnumType (right.Type))) {
2648 lc = EnumLiftUp (ec, lc, rc, loc);
2650 rc = EnumLiftUp (ec, rc, lc, loc);
2653 if (rc != null && lc != null) {
2654 int prev_e = ec.Report.Errors;
2655 Expression e = ConstantFold.BinaryFold (ec, oper, lc, rc, loc);
2659 if (e != null || ec.Report.Errors != prev_e)
2663 // Comparison warnings
2664 if ((oper & Operator.ComparisonMask) != 0) {
2665 if (left.Equals (right)) {
2666 ec.Report.Warning (1718, 3, loc, "A comparison made to same variable. Did you mean to compare something else?");
2668 CheckUselessComparison (ec, lc, right.Type);
2669 CheckUselessComparison (ec, rc, left.Type);
2672 if (left.Type == InternalType.Dynamic || right.Type == InternalType.Dynamic) {
2673 Arguments args = new Arguments (2);
2674 args.Add (new Argument (left));
2675 args.Add (new Argument (right));
2676 return new DynamicExpressionStatement (this, args, loc).Resolve (ec);
2679 if (RootContext.Version >= LanguageVersion.ISO_2 &&
2680 ((TypeManager.IsNullableType (left.Type) && (right is NullLiteral || TypeManager.IsNullableType (right.Type) || TypeManager.IsValueType (right.Type))) ||
2681 (TypeManager.IsValueType (left.Type) && right is NullLiteral) ||
2682 (TypeManager.IsNullableType (right.Type) && (left is NullLiteral || TypeManager.IsNullableType (left.Type) || TypeManager.IsValueType (left.Type))) ||
2683 (TypeManager.IsValueType (right.Type) && left is NullLiteral)))
2684 return new Nullable.LiftedBinaryOperator (oper, left, right, loc).Resolve (ec);
2686 return DoResolveCore (ec, left, right);
2689 protected Expression DoResolveCore (ResolveContext ec, Expression left_orig, Expression right_orig)
2691 Expression expr = ResolveOperator (ec);
2693 Error_OperatorCannotBeApplied (ec, left_orig, right_orig);
2695 if (left == null || right == null)
2696 throw new InternalErrorException ("Invalid conversion");
2698 if (oper == Operator.BitwiseOr)
2699 CheckBitwiseOrOnSignExtended (ec);
2704 public override SLE.Expression MakeExpression (BuilderContext ctx)
2706 var le = left.MakeExpression (ctx);
2707 var re = right.MakeExpression (ctx);
2708 bool is_checked = ctx.HasSet (BuilderContext.Options.CheckedScope);
2711 case Operator.Addition:
2712 return is_checked ? SLE.Expression.AddChecked (le, re) : SLE.Expression.Add (le, re);
2713 case Operator.BitwiseAnd:
2714 return SLE.Expression.And (le, re);
2715 case Operator.BitwiseOr:
2716 return SLE.Expression.Or (le, re);
2717 case Operator.Division:
2718 return SLE.Expression.Divide (le, re);
2719 case Operator.Equality:
2720 return SLE.Expression.Equal (le, re);
2721 case Operator.ExclusiveOr:
2722 return SLE.Expression.ExclusiveOr (le, re);
2723 case Operator.GreaterThan:
2724 return SLE.Expression.GreaterThan (le, re);
2725 case Operator.GreaterThanOrEqual:
2726 return SLE.Expression.GreaterThanOrEqual (le, re);
2727 case Operator.Inequality:
2728 return SLE.Expression.NotEqual (le, re);
2729 case Operator.LeftShift:
2730 return SLE.Expression.LeftShift (le, re);
2731 case Operator.LessThan:
2732 return SLE.Expression.LessThan (le, re);
2733 case Operator.LessThanOrEqual:
2734 return SLE.Expression.LessThanOrEqual (le, re);
2735 case Operator.LogicalAnd:
2736 return SLE.Expression.AndAlso (le, re);
2737 case Operator.LogicalOr:
2738 return SLE.Expression.OrElse (le, re);
2739 case Operator.Modulus:
2740 return SLE.Expression.Modulo (le, re);
2741 case Operator.Multiply:
2742 return is_checked ? SLE.Expression.MultiplyChecked (le, re) : SLE.Expression.Multiply (le, re);
2743 case Operator.RightShift:
2744 return SLE.Expression.RightShift (le, re);
2745 case Operator.Subtraction:
2746 return is_checked ? SLE.Expression.SubtractChecked (le, re) : SLE.Expression.Subtract (le, re);
2748 throw new NotImplementedException (oper.ToString ());
2753 // D operator + (D x, D y)
2754 // D operator - (D x, D y)
2755 // bool operator == (D x, D y)
2756 // bool operator != (D x, D y)
2758 Expression ResolveOperatorDelegate (ResolveContext ec, TypeSpec l, TypeSpec r)
2760 bool is_equality = (oper & Operator.EqualityMask) != 0;
2761 if (!TypeManager.IsEqual (l, r) && !TypeSpecComparer.Variant.IsEqual (r, l)) {
2763 if (right.eclass == ExprClass.MethodGroup || (r == InternalType.AnonymousMethod && !is_equality)) {
2764 tmp = Convert.ImplicitConversionRequired (ec, right, l, loc);
2769 } else if (left.eclass == ExprClass.MethodGroup || (l == InternalType.AnonymousMethod && !is_equality)) {
2770 tmp = Convert.ImplicitConversionRequired (ec, left, r, loc);
2781 // Resolve delegate equality as a user operator
2784 return ResolveUserOperator (ec, l, r);
2787 Arguments args = new Arguments (2);
2788 args.Add (new Argument (left));
2789 args.Add (new Argument (right));
2791 if (oper == Operator.Addition) {
2792 if (TypeManager.delegate_combine_delegate_delegate == null) {
2793 TypeManager.delegate_combine_delegate_delegate = TypeManager.GetPredefinedMethod (
2794 TypeManager.delegate_type, "Combine", loc, TypeManager.delegate_type, TypeManager.delegate_type);
2797 method = TypeManager.delegate_combine_delegate_delegate;
2799 if (TypeManager.delegate_remove_delegate_delegate == null) {
2800 TypeManager.delegate_remove_delegate_delegate = TypeManager.GetPredefinedMethod (
2801 TypeManager.delegate_type, "Remove", loc, TypeManager.delegate_type, TypeManager.delegate_type);
2804 method = TypeManager.delegate_remove_delegate_delegate;
2808 return new EmptyExpression (TypeManager.decimal_type);
2810 MethodGroupExpr mg = new MethodGroupExpr (method, TypeManager.delegate_type, loc);
2811 mg = mg.OverloadResolve (ec, ref args, false, loc);
2813 return new ClassCast (new UserOperatorCall (mg, args, CreateExpressionTree, loc), l);
2817 // Enumeration operators
2819 Expression ResolveOperatorEnum (ResolveContext ec, bool lenum, bool renum, TypeSpec ltype, TypeSpec rtype)
2822 // bool operator == (E x, E y);
2823 // bool operator != (E x, E y);
2824 // bool operator < (E x, E y);
2825 // bool operator > (E x, E y);
2826 // bool operator <= (E x, E y);
2827 // bool operator >= (E x, E y);
2829 // E operator & (E x, E y);
2830 // E operator | (E x, E y);
2831 // E operator ^ (E x, E y);
2833 // U operator - (E e, E f)
2834 // E operator - (E e, U x)
2836 // E operator + (U x, E e)
2837 // E operator + (E e, U x)
2839 if (!((oper & (Operator.ComparisonMask | Operator.BitwiseMask)) != 0 ||
2840 (oper == Operator.Subtraction && lenum) ||
2841 (oper == Operator.Addition && (lenum != renum || type != null)))) // type != null for lifted null
2844 Expression ltemp = left;
2845 Expression rtemp = right;
2846 TypeSpec underlying_type;
2849 if ((oper & (Operator.ComparisonMask | Operator.BitwiseMask)) != 0) {
2851 expr = Convert.ImplicitConversion (ec, left, rtype, loc);
2857 expr = Convert.ImplicitConversion (ec, right, ltype, loc);
2865 if (TypeManager.IsEqual (ltype, rtype)) {
2866 underlying_type = EnumSpec.GetUnderlyingType (ltype);
2868 if (left is Constant)
2869 left = ((Constant) left).ConvertExplicitly (false, underlying_type).Resolve (ec);
2871 left = EmptyCast.Create (left, underlying_type);
2873 if (right is Constant)
2874 right = ((Constant) right).ConvertExplicitly (false, underlying_type).Resolve (ec);
2876 right = EmptyCast.Create (right, underlying_type);
2878 underlying_type = EnumSpec.GetUnderlyingType (ltype);
2880 if (oper != Operator.Subtraction && oper != Operator.Addition) {
2881 Constant c = right as Constant;
2882 if (c == null || !c.IsDefaultValue)
2885 if (!Convert.ImplicitStandardConversionExists (right, underlying_type))
2888 right = Convert.ImplicitConversionStandard (ec, right, underlying_type, right.Location);
2891 if (left is Constant)
2892 left = ((Constant) left).ConvertExplicitly (false, underlying_type).Resolve (ec);
2894 left = EmptyCast.Create (left, underlying_type);
2897 underlying_type = EnumSpec.GetUnderlyingType (rtype);
2899 if (oper != Operator.Addition) {
2900 Constant c = left as Constant;
2901 if (c == null || !c.IsDefaultValue)
2904 if (!Convert.ImplicitStandardConversionExists (left, underlying_type))
2907 left = Convert.ImplicitConversionStandard (ec, left, underlying_type, left.Location);
2910 if (right is Constant)
2911 right = ((Constant) right).ConvertExplicitly (false, underlying_type).Resolve (ec);
2913 right = EmptyCast.Create (right, underlying_type);
2920 // C# specification uses explicit cast syntax which means binary promotion
2921 // should happen, however it seems that csc does not do that
2923 if (!DoBinaryOperatorPromotion (ec)) {
2929 TypeSpec res_type = null;
2930 if ((oper & Operator.BitwiseMask) != 0 || oper == Operator.Subtraction || oper == Operator.Addition) {
2931 TypeSpec promoted_type = lenum ? left.Type : right.Type;
2932 enum_conversion = Convert.ExplicitNumericConversion (
2933 new EmptyExpression (promoted_type), underlying_type);
2935 if (oper == Operator.Subtraction && renum && lenum)
2936 res_type = underlying_type;
2937 else if (oper == Operator.Addition && renum)
2943 expr = ResolveOperatorPredefined (ec, standard_operators, true, res_type);
2944 if (!is_compound || expr == null)
2952 // If the return type of the selected operator is implicitly convertible to the type of x
2954 if (Convert.ImplicitConversionExists (ec, expr, ltype))
2958 // Otherwise, if the selected operator is a predefined operator, if the return type of the
2959 // selected operator is explicitly convertible to the type of x, and if y is implicitly
2960 // convertible to the type of x or the operator is a shift operator, then the operation
2961 // is evaluated as x = (T)(x op y), where T is the type of x
2963 expr = Convert.ExplicitConversion (ec, expr, ltype, loc);
2967 if (Convert.ImplicitConversionExists (ec, ltemp, ltype))
2974 // 7.9.6 Reference type equality operators
2976 Binary ResolveOperatorEqualityRerefence (ResolveContext ec, TypeSpec l, TypeSpec r)
2979 // operator != (object a, object b)
2980 // operator == (object a, object b)
2983 // TODO: this method is almost equivalent to Convert.ImplicitReferenceConversion
2985 if (left.eclass == ExprClass.MethodGroup || right.eclass == ExprClass.MethodGroup)
2988 type = TypeManager.bool_type;
2990 var lgen = l as TypeParameterSpec;
2993 if (l is InternalType)
2998 // Only allow to compare same reference type parameter
3000 if (TypeManager.IsReferenceType (l)) {
3001 left = new BoxedCast (left, TypeManager.object_type);
3002 right = new BoxedCast (right, TypeManager.object_type);
3009 if (TypeManager.IsValueType (l))
3015 var rgen = r as TypeParameterSpec;
3018 // a, Both operands are reference-type values or the value null
3019 // b, One operand is a value of type T where T is a type-parameter and
3020 // the other operand is the value null. Furthermore T does not have the
3021 // value type constrain
3023 if (left is NullLiteral || right is NullLiteral) {
3025 if (lgen.HasSpecialStruct)
3028 left = new BoxedCast (left, TypeManager.object_type);
3033 if (rgen.HasSpecialStruct)
3036 right = new BoxedCast (right, TypeManager.object_type);
3042 // An interface is converted to the object before the
3043 // standard conversion is applied. It's not clear from the
3044 // standard but it looks like it works like that.
3047 if (!TypeManager.IsReferenceType (l))
3050 l = TypeManager.object_type;
3051 left = new BoxedCast (left, l);
3052 } else if (l.IsInterface) {
3053 l = TypeManager.object_type;
3054 } else if (TypeManager.IsStruct (l)) {
3059 if (!TypeManager.IsReferenceType (r))
3062 r = TypeManager.object_type;
3063 right = new BoxedCast (right, r);
3064 } else if (r.IsInterface) {
3065 r = TypeManager.object_type;
3066 } else if (TypeManager.IsStruct (r)) {
3071 const string ref_comparison = "Possible unintended reference comparison. " +
3072 "Consider casting the {0} side of the expression to `string' to compare the values";
3075 // A standard implicit conversion exists from the type of either
3076 // operand to the type of the other operand
3078 if (Convert.ImplicitReferenceConversionExists (left, r)) {
3079 if (l == TypeManager.string_type)
3080 ec.Report.Warning (253, 2, loc, ref_comparison, "right");
3085 if (Convert.ImplicitReferenceConversionExists (right, l)) {
3086 if (r == TypeManager.string_type)
3087 ec.Report.Warning (252, 2, loc, ref_comparison, "left");
3096 Expression ResolveOperatorPointer (ResolveContext ec, TypeSpec l, TypeSpec r)
3099 // bool operator == (void* x, void* y);
3100 // bool operator != (void* x, void* y);
3101 // bool operator < (void* x, void* y);
3102 // bool operator > (void* x, void* y);
3103 // bool operator <= (void* x, void* y);
3104 // bool operator >= (void* x, void* y);
3106 if ((oper & Operator.ComparisonMask) != 0) {
3109 temp = Convert.ImplicitConversion (ec, left, r, left.Location);
3116 temp = Convert.ImplicitConversion (ec, right, l, right.Location);
3122 type = TypeManager.bool_type;
3126 if (pointer_operators == null)
3127 CreatePointerOperatorsTable ();
3129 return ResolveOperatorPredefined (ec, pointer_operators, false, null);
3133 // Build-in operators method overloading
3135 protected virtual Expression ResolveOperatorPredefined (ResolveContext ec, PredefinedOperator [] operators, bool primitives_only, TypeSpec enum_type)
3137 PredefinedOperator best_operator = null;
3138 TypeSpec l = left.Type;
3139 TypeSpec r = right.Type;
3140 Operator oper_mask = oper & ~Operator.ValuesOnlyMask;
3142 foreach (PredefinedOperator po in operators) {
3143 if ((po.OperatorsMask & oper_mask) == 0)
3146 if (primitives_only) {
3147 if (!po.IsPrimitiveApplicable (l, r))
3150 if (!po.IsApplicable (ec, left, right))
3154 if (best_operator == null) {
3156 if (primitives_only)
3162 best_operator = po.ResolveBetterOperator (ec, best_operator);
3164 if (best_operator == null) {
3165 ec.Report.Error (34, loc, "Operator `{0}' is ambiguous on operands of type `{1}' and `{2}'",
3166 OperName (oper), TypeManager.CSharpName (l), TypeManager.CSharpName (r));
3173 if (best_operator == null)
3176 Expression expr = best_operator.ConvertResult (ec, this);
3179 // Optimize &/&& constant expressions with 0 value
3181 if (oper == Operator.BitwiseAnd || oper == Operator.LogicalAnd) {
3182 Constant rc = right as Constant;
3183 Constant lc = left as Constant;
3184 if ((lc != null && lc.IsDefaultValue) || (rc != null && rc.IsDefaultValue)) {
3186 // The result is a constant with side-effect
3188 Constant side_effect = rc == null ?
3189 new SideEffectConstant (lc, right, loc) :
3190 new SideEffectConstant (rc, left, loc);
3192 return ReducedExpression.Create (side_effect.Resolve (ec), expr);
3196 if (enum_type == null)
3200 // HACK: required by enum_conversion
3202 expr.Type = enum_type;
3203 return EmptyCast.Create (expr, enum_type);
3207 // Performs user-operator overloading
3209 protected virtual Expression ResolveUserOperator (ResolveContext ec, TypeSpec l, TypeSpec r)
3212 if (oper == Operator.LogicalAnd)
3213 user_oper = Operator.BitwiseAnd;
3214 else if (oper == Operator.LogicalOr)
3215 user_oper = Operator.BitwiseOr;
3219 string op = GetOperatorMetadataName (user_oper);
3221 MethodGroupExpr left_operators = MethodLookup (ec.Compiler, ec.CurrentType, l, MemberKind.Operator, op, 0, loc);
3222 MethodGroupExpr right_operators = null;
3224 if (!TypeManager.IsEqual (r, l)) {
3225 right_operators = MethodLookup (ec.Compiler, ec.CurrentType, r, MemberKind.Operator, op, 0, loc);
3226 if (right_operators == null && left_operators == null)
3228 } else if (left_operators == null) {
3232 Arguments args = new Arguments (2);
3233 Argument larg = new Argument (left);
3235 Argument rarg = new Argument (right);
3238 MethodGroupExpr union;
3241 // User-defined operator implementations always take precedence
3242 // over predefined operator implementations
3244 if (left_operators != null && right_operators != null) {
3245 if (IsPredefinedUserOperator (l, user_oper)) {
3246 union = right_operators.OverloadResolve (ec, ref args, true, loc);
3248 union = left_operators;
3249 } else if (IsPredefinedUserOperator (r, user_oper)) {
3250 union = left_operators.OverloadResolve (ec, ref args, true, loc);
3252 union = right_operators;
3254 union = MethodGroupExpr.MakeUnionSet (left_operators, right_operators, loc);
3256 } else if (left_operators != null) {
3257 union = left_operators;
3259 union = right_operators;
3262 union = union.OverloadResolve (ec, ref args, true, loc);
3266 Expression oper_expr;
3268 // TODO: CreateExpressionTree is allocated every time
3269 if (user_oper != oper) {
3270 oper_expr = new ConditionalLogicalOperator (union, args, CreateExpressionTree,
3271 oper == Operator.LogicalAnd, loc).Resolve (ec);
3273 oper_expr = new UserOperatorCall (union, args, CreateExpressionTree, loc);
3276 // This is used to check if a test 'x == null' can be optimized to a reference equals,
3277 // and not invoke user operator
3279 if ((oper & Operator.EqualityMask) != 0) {
3280 if ((left is NullLiteral && IsBuildInEqualityOperator (r)) ||
3281 (right is NullLiteral && IsBuildInEqualityOperator (l))) {
3282 type = TypeManager.bool_type;
3283 if (left is NullLiteral || right is NullLiteral)
3284 oper_expr = ReducedExpression.Create (this, oper_expr);
3285 } else if (l != r) {
3286 var mi = union.BestCandidate;
3289 // Two System.Delegate(s) are never equal
3291 if (mi.DeclaringType == TypeManager.multicast_delegate_type)
3302 public override TypeExpr ResolveAsTypeTerminal (IMemberContext ec, bool silent)
3307 private void CheckUselessComparison (ResolveContext ec, Constant c, TypeSpec type)
3309 if (c == null || !IsTypeIntegral (type)
3310 || c is StringConstant
3311 || c is BoolConstant
3312 || c is FloatConstant
3313 || c is DoubleConstant
3314 || c is DecimalConstant
3320 if (c is ULongConstant) {
3321 ulong uvalue = ((ULongConstant) c).Value;
3322 if (uvalue > long.MaxValue) {
3323 if (type == TypeManager.byte_type ||
3324 type == TypeManager.sbyte_type ||
3325 type == TypeManager.short_type ||
3326 type == TypeManager.ushort_type ||
3327 type == TypeManager.int32_type ||
3328 type == TypeManager.uint32_type ||
3329 type == TypeManager.int64_type ||
3330 type == TypeManager.char_type)
3331 WarnUselessComparison (ec, type);
3334 value = (long) uvalue;
3336 else if (c is ByteConstant)
3337 value = ((ByteConstant) c).Value;
3338 else if (c is SByteConstant)
3339 value = ((SByteConstant) c).Value;
3340 else if (c is ShortConstant)
3341 value = ((ShortConstant) c).Value;
3342 else if (c is UShortConstant)
3343 value = ((UShortConstant) c).Value;
3344 else if (c is IntConstant)
3345 value = ((IntConstant) c).Value;
3346 else if (c is UIntConstant)
3347 value = ((UIntConstant) c).Value;
3348 else if (c is LongConstant)
3349 value = ((LongConstant) c).Value;
3350 else if (c is CharConstant)
3351 value = ((CharConstant)c).Value;
3356 if (IsValueOutOfRange (value, type))
3357 WarnUselessComparison (ec, type);
3360 static bool IsValueOutOfRange (long value, TypeSpec type)
3362 if (IsTypeUnsigned (type) && value < 0)
3364 return type == TypeManager.sbyte_type && (value >= 0x80 || value < -0x80) ||
3365 type == TypeManager.byte_type && value >= 0x100 ||
3366 type == TypeManager.short_type && (value >= 0x8000 || value < -0x8000) ||
3367 type == TypeManager.ushort_type && value >= 0x10000 ||
3368 type == TypeManager.int32_type && (value >= 0x80000000 || value < -0x80000000) ||
3369 type == TypeManager.uint32_type && value >= 0x100000000;
3372 static bool IsBuildInEqualityOperator (TypeSpec t)
3374 return t == TypeManager.object_type || t == TypeManager.string_type ||
3375 t == TypeManager.delegate_type || TypeManager.IsDelegateType (t);
3378 static bool IsPredefinedUserOperator (TypeSpec t, Operator op)
3381 // Some predefined types have user operators
3383 return (op & Operator.EqualityMask) != 0 && (t == TypeManager.string_type || t == TypeManager.decimal_type);
3386 private static bool IsTypeIntegral (TypeSpec type)
3388 return type == TypeManager.uint64_type ||
3389 type == TypeManager.int64_type ||
3390 type == TypeManager.uint32_type ||
3391 type == TypeManager.int32_type ||
3392 type == TypeManager.ushort_type ||
3393 type == TypeManager.short_type ||
3394 type == TypeManager.sbyte_type ||
3395 type == TypeManager.byte_type ||
3396 type == TypeManager.char_type;
3399 private static bool IsTypeUnsigned (TypeSpec type)
3401 return type == TypeManager.uint64_type ||
3402 type == TypeManager.uint32_type ||
3403 type == TypeManager.ushort_type ||
3404 type == TypeManager.byte_type ||
3405 type == TypeManager.char_type;
3408 private void WarnUselessComparison (ResolveContext ec, TypeSpec type)
3410 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}'",
3411 TypeManager.CSharpName (type));
3415 /// EmitBranchable is called from Statement.EmitBoolExpression in the
3416 /// context of a conditional bool expression. This function will return
3417 /// false if it is was possible to use EmitBranchable, or true if it was.
3419 /// The expression's code is generated, and we will generate a branch to `target'
3420 /// if the resulting expression value is equal to isTrue
3422 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
3425 // This is more complicated than it looks, but its just to avoid
3426 // duplicated tests: basically, we allow ==, !=, >, <, >= and <=
3427 // but on top of that we want for == and != to use a special path
3428 // if we are comparing against null
3430 if ((oper == Operator.Equality || oper == Operator.Inequality) && (left is Constant || right is Constant)) {
3431 bool my_on_true = oper == Operator.Inequality ? on_true : !on_true;
3434 // put the constant on the rhs, for simplicity
3436 if (left is Constant) {
3437 Expression swap = right;
3442 if (((Constant) right).IsZeroInteger) {
3443 left.EmitBranchable (ec, target, my_on_true);
3446 if (right.Type == TypeManager.bool_type) {
3447 // right is a boolean, and it's not 'false' => it is 'true'
3448 left.EmitBranchable (ec, target, !my_on_true);
3452 } else if (oper == Operator.LogicalAnd) {
3455 Label tests_end = ec.DefineLabel ();
3457 left.EmitBranchable (ec, tests_end, false);
3458 right.EmitBranchable (ec, target, true);
3459 ec.MarkLabel (tests_end);
3462 // This optimizes code like this
3463 // if (true && i > 4)
3465 if (!(left is Constant))
3466 left.EmitBranchable (ec, target, false);
3468 if (!(right is Constant))
3469 right.EmitBranchable (ec, target, false);
3474 } else if (oper == Operator.LogicalOr){
3476 left.EmitBranchable (ec, target, true);
3477 right.EmitBranchable (ec, target, true);
3480 Label tests_end = ec.DefineLabel ();
3481 left.EmitBranchable (ec, tests_end, true);
3482 right.EmitBranchable (ec, target, false);
3483 ec.MarkLabel (tests_end);
3488 } else if (!(oper == Operator.LessThan || oper == Operator.GreaterThan ||
3489 oper == Operator.LessThanOrEqual || oper == Operator.GreaterThanOrEqual ||
3490 oper == Operator.Equality || oper == Operator.Inequality)) {
3491 base.EmitBranchable (ec, target, on_true);
3498 TypeSpec t = left.Type;
3499 bool is_float = IsFloat (t);
3500 bool is_unsigned = is_float || IsUnsigned (t);
3503 case Operator.Equality:
3505 ec.Emit (OpCodes.Beq, target);
3507 ec.Emit (OpCodes.Bne_Un, target);
3510 case Operator.Inequality:
3512 ec.Emit (OpCodes.Bne_Un, target);
3514 ec.Emit (OpCodes.Beq, target);
3517 case Operator.LessThan:
3519 if (is_unsigned && !is_float)
3520 ec.Emit (OpCodes.Blt_Un, target);
3522 ec.Emit (OpCodes.Blt, target);
3525 ec.Emit (OpCodes.Bge_Un, target);
3527 ec.Emit (OpCodes.Bge, target);
3530 case Operator.GreaterThan:
3532 if (is_unsigned && !is_float)
3533 ec.Emit (OpCodes.Bgt_Un, target);
3535 ec.Emit (OpCodes.Bgt, target);
3538 ec.Emit (OpCodes.Ble_Un, target);
3540 ec.Emit (OpCodes.Ble, target);
3543 case Operator.LessThanOrEqual:
3545 if (is_unsigned && !is_float)
3546 ec.Emit (OpCodes.Ble_Un, target);
3548 ec.Emit (OpCodes.Ble, target);
3551 ec.Emit (OpCodes.Bgt_Un, target);
3553 ec.Emit (OpCodes.Bgt, target);
3557 case Operator.GreaterThanOrEqual:
3559 if (is_unsigned && !is_float)
3560 ec.Emit (OpCodes.Bge_Un, target);
3562 ec.Emit (OpCodes.Bge, target);
3565 ec.Emit (OpCodes.Blt_Un, target);
3567 ec.Emit (OpCodes.Blt, target);
3570 throw new InternalErrorException (oper.ToString ());
3574 public override void Emit (EmitContext ec)
3576 EmitOperator (ec, left.Type);
3579 protected virtual void EmitOperator (EmitContext ec, TypeSpec l)
3582 // Handle short-circuit operators differently
3585 if ((oper & Operator.LogicalMask) != 0) {
3586 Label load_result = ec.DefineLabel ();
3587 Label end = ec.DefineLabel ();
3589 bool is_or = oper == Operator.LogicalOr;
3590 left.EmitBranchable (ec, load_result, is_or);
3592 ec.Emit (OpCodes.Br_S, end);
3594 ec.MarkLabel (load_result);
3595 ec.Emit (is_or ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
3601 // Optimize zero-based operations which cannot be optimized at expression level
3603 if (oper == Operator.Subtraction) {
3604 var lc = left as IntegralConstant;
3605 if (lc != null && lc.IsDefaultValue) {
3607 ec.Emit (OpCodes.Neg);
3614 EmitOperatorOpcode (ec, oper, l);
3617 // Nullable enum could require underlying type cast and we cannot simply wrap binary
3618 // expression because that would wrap lifted binary operation
3620 if (enum_conversion != null)
3621 enum_conversion.Emit (ec);
3624 public override void EmitSideEffect (EmitContext ec)
3626 if ((oper & Operator.LogicalMask) != 0 ||
3627 (ec.HasSet (EmitContext.Options.CheckedScope) && (oper == Operator.Multiply || oper == Operator.Addition || oper == Operator.Subtraction))) {
3628 base.EmitSideEffect (ec);
3630 left.EmitSideEffect (ec);
3631 right.EmitSideEffect (ec);
3635 protected override void CloneTo (CloneContext clonectx, Expression t)
3637 Binary target = (Binary) t;
3639 target.left = left.Clone (clonectx);
3640 target.right = right.Clone (clonectx);
3643 public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
3645 Arguments binder_args = new Arguments (4);
3647 MemberAccess sle = new MemberAccess (new MemberAccess (
3648 new QualifiedAliasMember (QualifiedAliasMember.GlobalAlias, "System", loc), "Linq", loc), "Expressions", loc);
3650 CSharpBinderFlags flags = 0;
3651 if (ec.HasSet (ResolveContext.Options.CheckedScope))
3652 flags = CSharpBinderFlags.CheckedContext;
3654 if ((oper & Operator.LogicalMask) != 0)
3655 flags |= CSharpBinderFlags.BinaryOperationLogical;
3657 binder_args.Add (new Argument (new EnumConstant (new IntLiteral ((int) flags, loc), TypeManager.binder_flags)));
3658 binder_args.Add (new Argument (new MemberAccess (new MemberAccess (sle, "ExpressionType", loc), GetOperatorExpressionTypeName (), loc)));
3659 binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));
3660 binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation ("[]", args.CreateDynamicBinderArguments (ec), loc)));
3662 return new Invocation (DynamicExpressionStatement.GetBinder ("BinaryOperation", loc), binder_args);
3665 public override Expression CreateExpressionTree (ResolveContext ec)
3667 return CreateExpressionTree (ec, null);
3670 Expression CreateExpressionTree (ResolveContext ec, MethodGroupExpr method)
3673 bool lift_arg = false;
3676 case Operator.Addition:
3677 if (method == null && ec.HasSet (ResolveContext.Options.CheckedScope) && !IsFloat (type))
3678 method_name = "AddChecked";
3680 method_name = "Add";
3682 case Operator.BitwiseAnd:
3683 method_name = "And";
3685 case Operator.BitwiseOr:
3688 case Operator.Division:
3689 method_name = "Divide";
3691 case Operator.Equality:
3692 method_name = "Equal";
3695 case Operator.ExclusiveOr:
3696 method_name = "ExclusiveOr";
3698 case Operator.GreaterThan:
3699 method_name = "GreaterThan";
3702 case Operator.GreaterThanOrEqual:
3703 method_name = "GreaterThanOrEqual";
3706 case Operator.Inequality:
3707 method_name = "NotEqual";
3710 case Operator.LeftShift:
3711 method_name = "LeftShift";
3713 case Operator.LessThan:
3714 method_name = "LessThan";
3717 case Operator.LessThanOrEqual:
3718 method_name = "LessThanOrEqual";
3721 case Operator.LogicalAnd:
3722 method_name = "AndAlso";
3724 case Operator.LogicalOr:
3725 method_name = "OrElse";
3727 case Operator.Modulus:
3728 method_name = "Modulo";
3730 case Operator.Multiply:
3731 if (method == null && ec.HasSet (ResolveContext.Options.CheckedScope) && !IsFloat (type))
3732 method_name = "MultiplyChecked";
3734 method_name = "Multiply";
3736 case Operator.RightShift:
3737 method_name = "RightShift";
3739 case Operator.Subtraction:
3740 if (method == null && ec.HasSet (ResolveContext.Options.CheckedScope) && !IsFloat (type))
3741 method_name = "SubtractChecked";
3743 method_name = "Subtract";
3747 throw new InternalErrorException ("Unknown expression tree binary operator " + oper);
3750 Arguments args = new Arguments (2);
3751 args.Add (new Argument (left.CreateExpressionTree (ec)));
3752 args.Add (new Argument (right.CreateExpressionTree (ec)));
3753 if (method != null) {
3755 args.Add (new Argument (new BoolConstant (false, loc)));
3757 args.Add (new Argument (method.CreateExpressionTree (ec)));
3760 return CreateExpressionFactoryCall (ec, method_name, args);
3765 // Represents the operation a + b [+ c [+ d [+ ...]]], where a is a string
3766 // b, c, d... may be strings or objects.
3768 public class StringConcat : Expression {
3769 Arguments arguments;
3771 public StringConcat (Expression left, Expression right, Location loc)
3774 type = TypeManager.string_type;
3775 eclass = ExprClass.Value;
3777 arguments = new Arguments (2);
3780 public static StringConcat Create (ResolveContext rc, Expression left, Expression right, Location loc)
3782 if (left.eclass == ExprClass.Unresolved || right.eclass == ExprClass.Unresolved)
3783 throw new ArgumentException ();
3785 var s = new StringConcat (left, right, loc);
3786 s.Append (rc, left);
3787 s.Append (rc, right);
3791 public override Expression CreateExpressionTree (ResolveContext ec)
3793 Argument arg = arguments [0];
3794 return CreateExpressionAddCall (ec, arg, arg.CreateExpressionTree (ec), 1);
3798 // Creates nested calls tree from an array of arguments used for IL emit
3800 Expression CreateExpressionAddCall (ResolveContext ec, Argument left, Expression left_etree, int pos)
3802 Arguments concat_args = new Arguments (2);
3803 Arguments add_args = new Arguments (3);
3805 concat_args.Add (left);
3806 add_args.Add (new Argument (left_etree));
3808 concat_args.Add (arguments [pos]);
3809 add_args.Add (new Argument (arguments [pos].CreateExpressionTree (ec)));
3811 MethodGroupExpr method = CreateConcatMemberExpression ().Resolve (ec) as MethodGroupExpr;
3815 method = method.OverloadResolve (ec, ref concat_args, false, loc);
3819 add_args.Add (new Argument (method.CreateExpressionTree (ec)));
3821 Expression expr = CreateExpressionFactoryCall (ec, "Add", add_args);
3822 if (++pos == arguments.Count)
3825 left = new Argument (new EmptyExpression (method.BestCandidate.ReturnType));
3826 return CreateExpressionAddCall (ec, left, expr, pos);
3829 protected override Expression DoResolve (ResolveContext ec)
3834 void Append (ResolveContext rc, Expression operand)
3839 StringConstant sc = operand as StringConstant;
3841 if (arguments.Count != 0) {
3842 Argument last_argument = arguments [arguments.Count - 1];
3843 StringConstant last_expr_constant = last_argument.Expr as StringConstant;
3844 if (last_expr_constant != null) {
3845 last_argument.Expr = new StringConstant (
3846 last_expr_constant.Value + sc.Value, sc.Location).Resolve (rc);
3852 // Multiple (3+) concatenation are resolved as multiple StringConcat instances
3854 StringConcat concat_oper = operand as StringConcat;
3855 if (concat_oper != null) {
3856 arguments.AddRange (concat_oper.arguments);
3861 arguments.Add (new Argument (operand));
3864 Expression CreateConcatMemberExpression ()
3866 return new MemberAccess (new MemberAccess (new QualifiedAliasMember ("global", "System", loc), "String", loc), "Concat", loc);
3869 public override void Emit (EmitContext ec)
3871 Expression concat = new Invocation (CreateConcatMemberExpression (), arguments, true);
3872 concat = concat.Resolve (new ResolveContext (ec.MemberContext));
3877 public override SLE.Expression MakeExpression (BuilderContext ctx)
3879 if (arguments.Count != 2)
3880 throw new NotImplementedException ("arguments.Count != 2");
3882 var concat = typeof (string).GetMethod ("Concat", new[] { typeof (object), typeof (object) });
3883 return SLE.Expression.Add (arguments[0].Expr.MakeExpression (ctx), arguments[1].Expr.MakeExpression (ctx), concat);
3888 // User-defined conditional logical operator
3890 public class ConditionalLogicalOperator : UserOperatorCall {
3891 readonly bool is_and;
3894 public ConditionalLogicalOperator (MethodGroupExpr oper_method, Arguments arguments,
3895 ExpressionTreeExpression expr_tree, bool is_and, Location loc)
3896 : base (oper_method, arguments, expr_tree, loc)
3898 this.is_and = is_and;
3899 eclass = ExprClass.Unresolved;
3902 protected override Expression DoResolve (ResolveContext ec)
3904 var method = mg.BestCandidate;
3905 type = method.ReturnType;
3906 AParametersCollection pd = method.Parameters;
3907 if (!TypeManager.IsEqual (type, type) || !TypeManager.IsEqual (type, pd.Types [0]) || !TypeManager.IsEqual (type, pd.Types [1])) {
3908 ec.Report.Error (217, loc,
3909 "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",
3910 TypeManager.CSharpSignature (method));
3914 Expression left_dup = new EmptyExpression (type);
3915 Expression op_true = GetOperatorTrue (ec, left_dup, loc);
3916 Expression op_false = GetOperatorFalse (ec, left_dup, loc);
3917 if (op_true == null || op_false == null) {
3918 ec.Report.Error (218, loc,
3919 "The type `{0}' must have operator `true' and operator `false' defined when `{1}' is used as a short circuit operator",
3920 TypeManager.CSharpName (type), TypeManager.CSharpSignature (method));
3924 oper = is_and ? op_false : op_true;
3925 eclass = ExprClass.Value;
3929 public override void Emit (EmitContext ec)
3931 Label end_target = ec.DefineLabel ();
3934 // Emit and duplicate left argument
3936 arguments [0].Expr.Emit (ec);
3937 ec.Emit (OpCodes.Dup);
3938 arguments.RemoveAt (0);
3940 oper.EmitBranchable (ec, end_target, true);
3942 ec.MarkLabel (end_target);
3946 public class PointerArithmetic : Expression {
3947 Expression left, right;
3951 // We assume that `l' is always a pointer
3953 public PointerArithmetic (Binary.Operator op, Expression l, Expression r, TypeSpec t, Location loc)
3962 public override Expression CreateExpressionTree (ResolveContext ec)
3964 Error_PointerInsideExpressionTree (ec);
3968 protected override Expression DoResolve (ResolveContext ec)
3970 eclass = ExprClass.Variable;
3972 if (left.Type == TypeManager.void_ptr_type) {
3973 ec.Report.Error (242, loc, "The operation in question is undefined on void pointers");
3980 public override void Emit (EmitContext ec)
3982 TypeSpec op_type = left.Type;
3984 // It must be either array or fixed buffer
3986 if (TypeManager.HasElementType (op_type)) {
3987 element = TypeManager.GetElementType (op_type);
3989 FieldExpr fe = left as FieldExpr;
3991 element = ((FixedFieldSpec) (fe.Spec)).ElementType;
3996 int size = GetTypeSize (element);
3997 TypeSpec rtype = right.Type;
3999 if ((op & Binary.Operator.SubtractionMask) != 0 && rtype.IsPointer){
4001 // handle (pointer - pointer)
4005 ec.Emit (OpCodes.Sub);
4009 ec.Emit (OpCodes.Sizeof, element);
4012 ec.Emit (OpCodes.Div);
4014 ec.Emit (OpCodes.Conv_I8);
4017 // handle + and - on (pointer op int)
4019 Constant left_const = left as Constant;
4020 if (left_const != null) {
4022 // Optimize ((T*)null) pointer operations
4024 if (left_const.IsDefaultValue) {
4025 left = EmptyExpression.Null;
4033 var right_const = right as Constant;
4034 if (right_const != null) {
4036 // Optimize 0-based arithmetic
4038 if (right_const.IsDefaultValue)
4042 right = new IntConstant (size, right.Location);
4044 right = new SizeOf (new TypeExpression (element, right.Location), right.Location);
4046 // TODO: Should be the checks resolve context sensitive?
4047 ResolveContext rc = new ResolveContext (ec.MemberContext, ResolveContext.Options.UnsafeScope);
4048 right = new Binary (Binary.Operator.Multiply, right, right_const, loc).Resolve (rc);
4054 if (rtype == TypeManager.sbyte_type || rtype == TypeManager.byte_type ||
4055 rtype == TypeManager.short_type || rtype == TypeManager.ushort_type) {
4056 ec.Emit (OpCodes.Conv_I);
4057 } else if (rtype == TypeManager.uint32_type) {
4058 ec.Emit (OpCodes.Conv_U);
4061 if (right_const == null && size != 1){
4063 ec.Emit (OpCodes.Sizeof, element);
4066 if (rtype == TypeManager.int64_type || rtype == TypeManager.uint64_type)
4067 ec.Emit (OpCodes.Conv_I8);
4069 Binary.EmitOperatorOpcode (ec, Binary.Operator.Multiply, rtype);
4072 if (left_const == null) {
4073 if (rtype == TypeManager.int64_type)
4074 ec.Emit (OpCodes.Conv_I);
4075 else if (rtype == TypeManager.uint64_type)
4076 ec.Emit (OpCodes.Conv_U);
4078 Binary.EmitOperatorOpcode (ec, op, op_type);
4085 // A boolean-expression is an expression that yields a result
4088 public class BooleanExpression : ShimExpression
4090 public BooleanExpression (Expression expr)
4093 this.loc = expr.Location;
4096 public override Expression CreateExpressionTree (ResolveContext ec)
4098 // TODO: We should emit IsTrue (v4) instead of direct user operator
4099 // call but that would break csc compatibility
4100 return base.CreateExpressionTree (ec);
4103 protected override Expression DoResolve (ResolveContext ec)
4105 // A boolean-expression is required to be of a type
4106 // that can be implicitly converted to bool or of
4107 // a type that implements operator true
4109 expr = expr.Resolve (ec);
4113 Assign ass = expr as Assign;
4114 if (ass != null && ass.Source is Constant) {
4115 ec.Report.Warning (665, 3, loc,
4116 "Assignment in conditional expression is always constant. Did you mean to use `==' instead ?");
4119 if (expr.Type == TypeManager.bool_type)
4122 if (expr.Type == InternalType.Dynamic) {
4123 Arguments args = new Arguments (1);
4124 args.Add (new Argument (expr));
4125 return new DynamicUnaryConversion ("IsTrue", args, loc).Resolve (ec);
4128 type = TypeManager.bool_type;
4129 Expression converted = Convert.ImplicitConversion (ec, expr, type, loc);
4130 if (converted != null)
4134 // If no implicit conversion to bool exists, try using `operator true'
4136 converted = GetOperatorTrue (ec, expr, loc);
4137 if (converted == null) {
4138 expr.Error_ValueCannotBeConverted (ec, loc, type, false);
4147 /// Implements the ternary conditional operator (?:)
4149 public class Conditional : Expression {
4150 Expression expr, true_expr, false_expr;
4152 public Conditional (BooleanExpression expr, Expression true_expr, Expression false_expr)
4155 this.true_expr = true_expr;
4156 this.false_expr = false_expr;
4157 this.loc = expr.Location;
4160 public Expression Expr {
4166 public Expression TrueExpr {
4172 public Expression FalseExpr {
4178 public override Expression CreateExpressionTree (ResolveContext ec)
4180 Arguments args = new Arguments (3);
4181 args.Add (new Argument (expr.CreateExpressionTree (ec)));
4182 args.Add (new Argument (true_expr.CreateExpressionTree (ec)));
4183 args.Add (new Argument (false_expr.CreateExpressionTree (ec)));
4184 return CreateExpressionFactoryCall (ec, "Condition", args);
4187 protected override Expression DoResolve (ResolveContext ec)
4189 expr = expr.Resolve (ec);
4190 true_expr = true_expr.Resolve (ec);
4191 false_expr = false_expr.Resolve (ec);
4193 if (true_expr == null || false_expr == null || expr == null)
4196 eclass = ExprClass.Value;
4197 TypeSpec true_type = true_expr.Type;
4198 TypeSpec false_type = false_expr.Type;
4202 // First, if an implicit conversion exists from true_expr
4203 // to false_expr, then the result type is of type false_expr.Type
4205 if (!TypeManager.IsEqual (true_type, false_type)) {
4206 Expression conv = Convert.ImplicitConversion (ec, true_expr, false_type, loc);
4209 // Check if both can convert implicitly to each other's type
4211 if (Convert.ImplicitConversion (ec, false_expr, true_type, loc) != null) {
4212 ec.Report.Error (172, true_expr.Location,
4213 "Type of conditional expression cannot be determined as `{0}' and `{1}' convert implicitly to each other",
4214 TypeManager.CSharpName (true_type), TypeManager.CSharpName (false_type));
4219 } else if ((conv = Convert.ImplicitConversion (ec, false_expr, true_type, loc)) != null) {
4222 ec.Report.Error (173, true_expr.Location,
4223 "Type of conditional expression cannot be determined because there is no implicit conversion between `{0}' and `{1}'",
4224 TypeManager.CSharpName (true_type), TypeManager.CSharpName (false_type));
4229 // Dead code optimalization
4230 Constant c = expr as Constant;
4232 bool is_false = c.IsDefaultValue;
4233 ec.Report.Warning (429, 4, is_false ? true_expr.Location : false_expr.Location, "Unreachable expression code detected");
4234 return ReducedExpression.Create (is_false ? false_expr : true_expr, this).Resolve (ec);
4240 public override TypeExpr ResolveAsTypeTerminal (IMemberContext ec, bool silent)
4245 public override void Emit (EmitContext ec)
4247 Label false_target = ec.DefineLabel ();
4248 Label end_target = ec.DefineLabel ();
4250 expr.EmitBranchable (ec, false_target, false);
4251 true_expr.Emit (ec);
4253 if (type.IsInterface) {
4254 LocalBuilder temp = ec.GetTemporaryLocal (type);
4255 ec.Emit (OpCodes.Stloc, temp);
4256 ec.Emit (OpCodes.Ldloc, temp);
4257 ec.FreeTemporaryLocal (temp, type);
4260 ec.Emit (OpCodes.Br, end_target);
4261 ec.MarkLabel (false_target);
4262 false_expr.Emit (ec);
4263 ec.MarkLabel (end_target);
4266 protected override void CloneTo (CloneContext clonectx, Expression t)
4268 Conditional target = (Conditional) t;
4270 target.expr = expr.Clone (clonectx);
4271 target.true_expr = true_expr.Clone (clonectx);
4272 target.false_expr = false_expr.Clone (clonectx);
4276 public abstract class VariableReference : Expression, IAssignMethod, IMemoryLocation, IVariableReference {
4277 LocalTemporary temp;
4280 public abstract HoistedVariable GetHoistedVariable (AnonymousExpression ae);
4281 public abstract bool IsFixed { get; }
4282 public abstract bool IsRef { get; }
4283 public abstract string Name { get; }
4284 public abstract void SetHasAddressTaken ();
4287 // Variable IL data, it has to be protected to encapsulate hoisted variables
4289 protected abstract ILocalVariable Variable { get; }
4292 // Variable flow-analysis data
4294 public abstract VariableInfo VariableInfo { get; }
4297 public virtual void AddressOf (EmitContext ec, AddressOp mode)
4299 HoistedVariable hv = GetHoistedVariable (ec);
4301 hv.AddressOf (ec, mode);
4305 Variable.EmitAddressOf (ec);
4308 public HoistedVariable GetHoistedVariable (ResolveContext rc)
4310 return GetHoistedVariable (rc.CurrentAnonymousMethod);
4313 public HoistedVariable GetHoistedVariable (EmitContext ec)
4315 return GetHoistedVariable (ec.CurrentAnonymousMethod);
4318 public override string GetSignatureForError ()
4323 public override void Emit (EmitContext ec)
4328 public override void EmitSideEffect (EmitContext ec)
4334 // This method is used by parameters that are references, that are
4335 // being passed as references: we only want to pass the pointer (that
4336 // is already stored in the parameter, not the address of the pointer,
4337 // and not the value of the variable).
4339 public void EmitLoad (EmitContext ec)
4344 public void Emit (EmitContext ec, bool leave_copy)
4346 Report.Debug (64, "VARIABLE EMIT", this, Variable, type, IsRef, loc);
4348 HoistedVariable hv = GetHoistedVariable (ec);
4350 hv.Emit (ec, leave_copy);
4358 // If we are a reference, we loaded on the stack a pointer
4359 // Now lets load the real value
4361 ec.EmitLoadFromPtr (type);
4365 ec.Emit (OpCodes.Dup);
4368 temp = new LocalTemporary (Type);
4374 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy,
4375 bool prepare_for_load)
4377 HoistedVariable hv = GetHoistedVariable (ec);
4379 hv.EmitAssign (ec, source, leave_copy, prepare_for_load);
4383 New n_source = source as New;
4384 if (n_source != null) {
4385 if (!n_source.Emit (ec, this)) {
4398 ec.Emit (OpCodes.Dup);
4400 temp = new LocalTemporary (Type);
4406 ec.EmitStoreFromPtr (type);
4408 Variable.EmitAssign (ec);
4416 public bool IsHoisted {
4417 get { return GetHoistedVariable ((AnonymousExpression) null) != null; }
4424 public class LocalVariableReference : VariableReference {
4425 readonly string name;
4427 public LocalInfo local_info;
4430 public LocalVariableReference (Block block, string name, Location l)
4438 // Setting `is_readonly' to false will allow you to create a writable
4439 // reference to a read-only variable. This is used by foreach and using.
4441 public LocalVariableReference (Block block, string name, Location l,
4442 LocalInfo local_info, bool is_readonly)
4443 : this (block, name, l)
4445 this.local_info = local_info;
4446 this.is_readonly = is_readonly;
4449 public override VariableInfo VariableInfo {
4450 get { return local_info.VariableInfo; }
4453 public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
4455 return local_info.HoistedVariant;
4459 // A local variable is always fixed
4461 public override bool IsFixed {
4462 get { return true; }
4465 public override bool IsRef {
4466 get { return false; }
4469 public bool IsReadOnly {
4470 get { return is_readonly; }
4473 public override string Name {
4474 get { return name; }
4477 public bool VerifyAssigned (ResolveContext ec)
4479 VariableInfo variable_info = local_info.VariableInfo;
4480 return variable_info == null || variable_info.IsAssigned (ec, loc);
4483 void ResolveLocalInfo ()
4485 if (local_info == null) {
4486 local_info = Block.GetLocalInfo (Name);
4487 type = local_info.VariableType;
4488 is_readonly = local_info.ReadOnly;
4492 public override void SetHasAddressTaken ()
4494 local_info.AddressTaken = true;
4497 public override Expression CreateExpressionTree (ResolveContext ec)
4499 HoistedVariable hv = GetHoistedVariable (ec);
4501 return hv.CreateExpressionTree ();
4503 Arguments arg = new Arguments (1);
4504 arg.Add (new Argument (this));
4505 return CreateExpressionFactoryCall (ec, "Constant", arg);
4508 Expression DoResolveBase (ResolveContext ec)
4510 Expression e = Block.GetConstantExpression (Name);
4512 return e.Resolve (ec);
4514 VerifyAssigned (ec);
4517 // If we are referencing a variable from the external block
4518 // flag it for capturing
4520 if (ec.MustCaptureVariable (local_info)) {
4521 if (local_info.AddressTaken)
4522 AnonymousMethodExpression.Error_AddressOfCapturedVar (ec, this, loc);
4524 if (ec.IsVariableCapturingRequired) {
4525 AnonymousMethodStorey storey = local_info.Block.Explicit.CreateAnonymousMethodStorey (ec);
4526 storey.CaptureLocalVariable (ec, local_info);
4530 eclass = ExprClass.Variable;
4531 type = local_info.VariableType;
4535 protected override Expression DoResolve (ResolveContext ec)
4537 ResolveLocalInfo ();
4538 local_info.Used = true;
4540 if (type == null && local_info.Type is VarExpr) {
4541 local_info.VariableType = TypeManager.object_type;
4542 Error_VariableIsUsedBeforeItIsDeclared (ec.Report, Name);
4546 return DoResolveBase (ec);
4549 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
4551 ResolveLocalInfo ();
4554 if (right_side == EmptyExpression.OutAccess.Instance)
4555 local_info.Used = true;
4557 // Infer implicitly typed local variable
4559 VarExpr ve = local_info.Type as VarExpr;
4561 if (!ve.InferType (ec, right_side))
4563 type = local_info.VariableType = ve.Type;
4570 if (right_side == EmptyExpression.OutAccess.Instance) {
4571 code = 1657; msg = "Cannot pass `{0}' as a ref or out argument because it is a `{1}'";
4572 } else if (right_side == EmptyExpression.LValueMemberAccess) {
4573 code = 1654; msg = "Cannot assign to members of `{0}' because it is a `{1}'";
4574 } else if (right_side == EmptyExpression.LValueMemberOutAccess) {
4575 code = 1655; msg = "Cannot pass members of `{0}' as ref or out arguments because it is a `{1}'";
4576 } else if (right_side == EmptyExpression.UnaryAddress) {
4577 code = 459; msg = "Cannot take the address of {1} `{0}'";
4579 code = 1656; msg = "Cannot assign to `{0}' because it is a `{1}'";
4581 ec.Report.Error (code, loc, msg, Name, local_info.GetReadOnlyContext ());
4582 } else if (VariableInfo != null) {
4583 VariableInfo.SetAssigned (ec);
4586 return DoResolveBase (ec);
4589 public override int GetHashCode ()
4591 return Name.GetHashCode ();
4594 public override bool Equals (object obj)
4596 LocalVariableReference lvr = obj as LocalVariableReference;
4600 return Name == lvr.Name && Block == lvr.Block;
4603 protected override ILocalVariable Variable {
4604 get { return local_info; }
4607 public override string ToString ()
4609 return String.Format ("{0} ({1}:{2})", GetType (), Name, loc);
4612 protected override void CloneTo (CloneContext clonectx, Expression t)
4614 LocalVariableReference target = (LocalVariableReference) t;
4616 target.Block = clonectx.LookupBlock (Block);
4617 if (local_info != null)
4618 target.local_info = clonectx.LookupVariable (local_info);
4623 /// This represents a reference to a parameter in the intermediate
4626 public class ParameterReference : VariableReference {
4627 readonly ToplevelParameterInfo pi;
4629 public ParameterReference (ToplevelParameterInfo pi, Location loc)
4635 public override bool IsRef {
4636 get { return (pi.Parameter.ModFlags & Parameter.Modifier.ISBYREF) != 0; }
4639 bool HasOutModifier {
4640 get { return pi.Parameter.ModFlags == Parameter.Modifier.OUT; }
4643 public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
4645 return pi.Parameter.HoistedVariant;
4649 // A ref or out parameter is classified as a moveable variable, even
4650 // if the argument given for the parameter is a fixed variable
4652 public override bool IsFixed {
4653 get { return !IsRef; }
4656 public override string Name {
4657 get { return Parameter.Name; }
4660 public Parameter Parameter {
4661 get { return pi.Parameter; }
4664 public override VariableInfo VariableInfo {
4665 get { return pi.VariableInfo; }
4668 protected override ILocalVariable Variable {
4669 get { return Parameter; }
4672 public bool IsAssigned (ResolveContext ec, Location loc)
4674 // HACK: Variables are not captured in probing mode
4675 if (ec.IsInProbingMode)
4678 if (!ec.DoFlowAnalysis || !HasOutModifier || ec.CurrentBranching.IsAssigned (VariableInfo))
4681 ec.Report.Error (269, loc, "Use of unassigned out parameter `{0}'", Name);
4685 public override void SetHasAddressTaken ()
4687 Parameter.HasAddressTaken = true;
4690 void SetAssigned (ResolveContext ec)
4692 if (HasOutModifier && ec.DoFlowAnalysis)
4693 ec.CurrentBranching.SetAssigned (VariableInfo);
4696 bool DoResolveBase (ResolveContext ec)
4698 type = pi.ParameterType;
4699 eclass = ExprClass.Variable;
4701 AnonymousExpression am = ec.CurrentAnonymousMethod;
4705 Block b = ec.CurrentBlock;
4708 IParameterData[] p = b.Toplevel.Parameters.FixedParameters;
4709 for (int i = 0; i < p.Length; ++i) {
4710 if (p [i] != Parameter)
4714 // Don't capture local parameters
4716 if (b == ec.CurrentBlock.Toplevel && !am.IsIterator)
4720 ec.Report.Error (1628, loc,
4721 "Parameter `{0}' cannot be used inside `{1}' when using `ref' or `out' modifier",
4722 Name, am.ContainerType);
4725 if (pi.Parameter.HasAddressTaken)
4726 AnonymousMethodExpression.Error_AddressOfCapturedVar (ec, this, loc);
4728 if (ec.IsVariableCapturingRequired && !b.Toplevel.IsExpressionTree) {
4729 AnonymousMethodStorey storey = pi.Block.CreateAnonymousMethodStorey (ec);
4730 storey.CaptureParameter (ec, this);
4742 public override int GetHashCode ()
4744 return Name.GetHashCode ();
4747 public override bool Equals (object obj)
4749 ParameterReference pr = obj as ParameterReference;
4753 return Name == pr.Name;
4756 public override void AddressOf (EmitContext ec, AddressOp mode)
4759 // ParameterReferences might already be a reference
4766 base.AddressOf (ec, mode);
4769 protected override void CloneTo (CloneContext clonectx, Expression target)
4774 public override Expression CreateExpressionTree (ResolveContext ec)
4776 HoistedVariable hv = GetHoistedVariable (ec);
4778 return hv.CreateExpressionTree ();
4780 return Parameter.ExpressionTreeVariableReference ();
4784 // Notice that for ref/out parameters, the type exposed is not the
4785 // same type exposed externally.
4788 // externally we expose "int&"
4789 // here we expose "int".
4791 // We record this in "is_ref". This means that the type system can treat
4792 // the type as it is expected, but when we generate the code, we generate
4793 // the alternate kind of code.
4795 protected override Expression DoResolve (ResolveContext ec)
4797 if (!DoResolveBase (ec))
4800 // HACK: Variables are not captured in probing mode
4801 if (ec.IsInProbingMode)
4804 if (HasOutModifier && ec.DoFlowAnalysis &&
4805 (!ec.OmitStructFlowAnalysis || !VariableInfo.TypeInfo.IsStruct) && !IsAssigned (ec, loc))
4811 override public Expression DoResolveLValue (ResolveContext ec, Expression right_side)
4813 if (!DoResolveBase (ec))
4816 // HACK: parameters are not captured when probing is on
4817 if (!ec.IsInProbingMode)
4823 static public void EmitLdArg (EmitContext ec, int x)
4826 case 0: ec.Emit (OpCodes.Ldarg_0); break;
4827 case 1: ec.Emit (OpCodes.Ldarg_1); break;
4828 case 2: ec.Emit (OpCodes.Ldarg_2); break;
4829 case 3: ec.Emit (OpCodes.Ldarg_3); break;
4831 if (x > byte.MaxValue)
4832 ec.Emit (OpCodes.Ldarg, x);
4834 ec.Emit (OpCodes.Ldarg_S, (byte) x);
4841 /// Invocation of methods or delegates.
4843 public class Invocation : ExpressionStatement
4845 protected Arguments arguments;
4846 protected Expression expr;
4847 protected MethodGroupExpr mg;
4848 bool arguments_resolved;
4851 // arguments is an ArrayList, but we do not want to typecast,
4852 // as it might be null.
4854 public Invocation (Expression expr, Arguments arguments)
4856 SimpleName sn = expr as SimpleName;
4858 this.expr = sn.GetMethodGroup ();
4862 this.arguments = arguments;
4864 loc = expr.Location;
4867 public Invocation (Expression expr, Arguments arguments, bool arguments_resolved)
4868 : this (expr, arguments)
4870 this.arguments_resolved = arguments_resolved;
4873 public override Expression CreateExpressionTree (ResolveContext ec)
4875 Expression instance = mg.IsInstance ?
4876 mg.InstanceExpression.CreateExpressionTree (ec) :
4877 new NullLiteral (loc);
4879 var args = Arguments.CreateForExpressionTree (ec, arguments,
4881 mg.CreateExpressionTree (ec));
4884 MemberExpr.Error_BaseAccessInExpressionTree (ec, loc);
4886 return CreateExpressionFactoryCall (ec, "Call", args);
4889 protected override Expression DoResolve (ResolveContext ec)
4891 Expression member_expr = expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
4892 if (member_expr == null)
4896 // Next, evaluate all the expressions in the argument list
4898 bool dynamic_arg = false;
4899 if (arguments != null && !arguments_resolved)
4900 arguments.Resolve (ec, out dynamic_arg);
4902 TypeSpec expr_type = member_expr.Type;
4903 mg = member_expr as MethodGroupExpr;
4905 bool dynamic_member = expr_type == InternalType.Dynamic;
4907 if (!dynamic_member) {
4908 Expression invoke = null;
4911 if (expr_type != null && TypeManager.IsDelegateType (expr_type)) {
4912 invoke = new DelegateInvocation (member_expr, arguments, loc);
4913 invoke = invoke.Resolve (ec);
4914 if (invoke == null || !dynamic_arg)
4917 MemberExpr me = member_expr as MemberExpr;
4919 member_expr.Error_UnexpectedKind (ec, ResolveFlags.MethodGroup, loc);
4923 mg = ec.LookupExtensionMethod (me.Type, me.Name, -1, loc);
4925 ec.Report.Error (1955, loc, "The member `{0}' cannot be used as method or delegate",
4926 member_expr.GetSignatureForError ());
4930 ((ExtensionMethodGroupExpr) mg).ExtensionExpression = me.InstanceExpression;
4934 if (invoke == null) {
4935 mg = DoResolveOverload (ec);
4941 if (dynamic_arg || dynamic_member)
4942 return DoResolveDynamic (ec, member_expr);
4944 var method = mg.BestCandidate;
4945 if (method != null) {
4946 type = method.ReturnType;
4948 // TODO: this is a copy of mg.ResolveMemberAccess method
4949 Expression iexpr = mg.InstanceExpression;
4950 if (method.IsStatic) {
4951 if (iexpr == null ||
4952 iexpr is This || iexpr is EmptyExpression ||
4953 mg.IdenticalTypeName) {
4954 mg.InstanceExpression = null;
4956 MemberExpr.error176 (ec, loc, mg.GetSignatureForError ());
4960 if (iexpr == null || iexpr == EmptyExpression.Null) {
4961 SimpleName.Error_ObjectRefRequired (ec, loc, mg.GetSignatureForError ());
4967 // Only base will allow this invocation to happen.
4969 if (mg.IsBase && method.IsAbstract){
4970 Error_CannotCallAbstractBase (ec, TypeManager.CSharpSignature (method));
4974 if (arguments == null && method.DeclaringType == TypeManager.object_type && method.Name == Destructor.MetadataName) {
4976 ec.Report.Error (250, loc, "Do not directly call your base class Finalize method. It is called automatically from your destructor");
4978 ec.Report.Error (245, loc, "Destructors and object.Finalize cannot be called directly. Consider calling IDisposable.Dispose if available");
4982 IsSpecialMethodInvocation (ec, method, loc);
4984 if (mg.InstanceExpression != null)
4985 mg.InstanceExpression.CheckMarshalByRefAccess (ec);
4987 eclass = ExprClass.Value;
4991 Expression DoResolveDynamic (ResolveContext ec, Expression memberExpr)
4994 DynamicMemberBinder dmb = memberExpr as DynamicMemberBinder;
4996 args = dmb.Arguments;
4997 if (arguments != null)
4998 args.AddRange (arguments);
4999 } else if (mg == null) {
5000 if (arguments == null)
5001 args = new Arguments (1);
5005 args.Insert (0, new Argument (memberExpr));
5009 ec.Report.Error (1971, loc,
5010 "The base call to method `{0}' cannot be dynamically dispatched. Consider casting the dynamic arguments or eliminating the base access",
5017 if (mg.IsStatic != mg.IsInstance) {
5019 args = new Arguments (1);
5022 args.Insert (0, new Argument (new TypeOf (new TypeExpression (mg.DeclaringType, loc), loc).Resolve (ec), Argument.AType.DynamicTypeName));
5024 MemberAccess ma = expr as MemberAccess;
5026 args.Insert (0, new Argument (ma.Left.Resolve (ec)));
5028 args.Insert (0, new Argument (new This (loc).Resolve (ec)));
5033 return new DynamicInvocation (expr as ATypeNameExpression, args, loc).Resolve (ec);
5036 protected virtual MethodGroupExpr DoResolveOverload (ResolveContext ec)
5038 return mg.OverloadResolve (ec, ref arguments, false, loc);
5041 public static bool IsSpecialMethodInvocation (ResolveContext ec, MethodSpec method, Location loc)
5043 if (!method.IsReservedMethod)
5046 if (ec.HasSet (ResolveContext.Options.InvokeSpecialName))
5049 ec.Report.SymbolRelatedToPreviousError (method);
5050 ec.Report.Error (571, loc, "`{0}': cannot explicitly call operator or accessor",
5051 method.GetSignatureForError ());
5056 static Type[] GetVarargsTypes (MethodSpec mb, Arguments arguments)
5058 AParametersCollection pd = mb.Parameters;
5060 Argument a = arguments [pd.Count - 1];
5061 Arglist list = (Arglist) a.Expr;
5063 return list.ArgumentTypes;
5067 /// is_base tells whether we want to force the use of the `call'
5068 /// opcode instead of using callvirt. Call is required to call
5069 /// a specific method, while callvirt will always use the most
5070 /// recent method in the vtable.
5072 /// is_static tells whether this is an invocation on a static method
5074 /// instance_expr is an expression that represents the instance
5075 /// it must be non-null if is_static is false.
5077 /// method is the method to invoke.
5079 /// Arguments is the list of arguments to pass to the method or constructor.
5081 public static void EmitCall (EmitContext ec, bool is_base,
5082 Expression instance_expr,
5083 MethodSpec method, Arguments Arguments, Location loc)
5085 EmitCall (ec, is_base, instance_expr, method, Arguments, loc, false, false);
5088 // `dup_args' leaves an extra copy of the arguments on the stack
5089 // `omit_args' does not leave any arguments at all.
5090 // So, basically, you could make one call with `dup_args' set to true,
5091 // and then another with `omit_args' set to true, and the two calls
5092 // would have the same set of arguments. However, each argument would
5093 // only have been evaluated once.
5094 public static void EmitCall (EmitContext ec, bool is_base,
5095 Expression instance_expr,
5096 MethodSpec method, Arguments Arguments, Location loc,
5097 bool dup_args, bool omit_args)
5099 bool struct_call = false;
5100 bool this_call = false;
5101 LocalTemporary this_arg = null;
5103 TypeSpec decl_type = method.DeclaringType;
5105 // Speed up the check by not doing it on not allowed targets
5106 if (method.ReturnType == TypeManager.void_type && method.IsConditionallyExcluded (loc))
5109 bool is_static = method.IsStatic;
5111 this_call = instance_expr is This;
5112 if (TypeManager.IsStruct (decl_type) || TypeManager.IsEnumType (decl_type))
5116 // If this is ourselves, push "this"
5120 TypeSpec iexpr_type = instance_expr.Type;
5123 // Push the instance expression
5125 if (TypeManager.IsValueType (iexpr_type) || TypeManager.IsGenericParameter (iexpr_type)) {
5127 // Special case: calls to a function declared in a
5128 // reference-type with a value-type argument need
5129 // to have their value boxed.
5130 if (TypeManager.IsStruct (decl_type) ||
5131 TypeManager.IsGenericParameter (iexpr_type)) {
5133 // If the expression implements IMemoryLocation, then
5134 // we can optimize and use AddressOf on the
5137 // If not we have to use some temporary storage for
5139 if (instance_expr is IMemoryLocation) {
5140 ((IMemoryLocation)instance_expr).
5141 AddressOf (ec, AddressOp.LoadStore);
5143 LocalTemporary temp = new LocalTemporary (iexpr_type);
5144 instance_expr.Emit (ec);
5146 temp.AddressOf (ec, AddressOp.Load);
5149 // avoid the overhead of doing this all the time.
5151 t = ReferenceContainer.MakeType (iexpr_type);
5153 instance_expr.Emit (ec);
5155 // FIXME: should use instance_expr is IMemoryLocation + constraint.
5156 // to help JIT to produce better code
5157 ec.Emit (OpCodes.Box, instance_expr.Type);
5158 t = TypeManager.object_type;
5161 instance_expr.Emit (ec);
5162 t = instance_expr.Type;
5166 ec.Emit (OpCodes.Dup);
5167 if (Arguments != null && Arguments.Count != 0) {
5168 this_arg = new LocalTemporary (t);
5169 this_arg.Store (ec);
5175 if (!omit_args && Arguments != null)
5176 Arguments.Emit (ec, dup_args, this_arg);
5179 if (is_static || struct_call || is_base || (this_call && !method.IsVirtual)) {
5180 call_op = OpCodes.Call;
5182 call_op = OpCodes.Callvirt;
5184 if ((instance_expr != null) && (instance_expr.Type.IsGenericParameter))
5185 ec.Emit (OpCodes.Constrained, instance_expr.Type);
5188 if (method.Parameters.HasArglist) {
5189 Type[] varargs_types = GetVarargsTypes (method, Arguments);
5190 ec.Emit (call_op, method, varargs_types);
5197 // and DoFoo is not virtual, you can omit the callvirt,
5198 // because you don't need the null checking behavior.
5200 ec.Emit (call_op, method);
5203 public override void Emit (EmitContext ec)
5205 mg.EmitCall (ec, arguments);
5208 public override void EmitStatement (EmitContext ec)
5213 // Pop the return value if there is one
5215 if (type != TypeManager.void_type)
5216 ec.Emit (OpCodes.Pop);
5219 protected override void CloneTo (CloneContext clonectx, Expression t)
5221 Invocation target = (Invocation) t;
5223 if (arguments != null)
5224 target.arguments = arguments.Clone (clonectx);
5226 target.expr = expr.Clone (clonectx);
5229 public override SLE.Expression MakeExpression (BuilderContext ctx)
5231 return MakeExpression (ctx, mg.InstanceExpression, (MethodSpec) mg, arguments);
5234 public static SLE.Expression MakeExpression (BuilderContext ctx, Expression instance, MethodSpec mi, Arguments args)
5236 var instance_expr = instance == null ? null : instance.MakeExpression (ctx);
5237 return SLE.Expression.Call (instance_expr, (MethodInfo) mi.GetMetaInfo (), Arguments.MakeExpression (args, ctx));
5242 /// Implements the new expression
5244 public class New : ExpressionStatement, IMemoryLocation {
5245 protected Arguments Arguments;
5248 // During bootstrap, it contains the RequestedType,
5249 // but if `type' is not null, it *might* contain a NewDelegate
5250 // (because of field multi-initialization)
5252 protected Expression RequestedType;
5254 protected MethodGroupExpr method;
5256 public New (Expression requested_type, Arguments arguments, Location l)
5258 RequestedType = requested_type;
5259 Arguments = arguments;
5264 /// Converts complex core type syntax like 'new int ()' to simple constant
5266 public static Constant Constantify (TypeSpec t)
5268 if (t == TypeManager.int32_type)
5269 return new IntConstant (0, Location.Null);
5270 if (t == TypeManager.uint32_type)
5271 return new UIntConstant (0, Location.Null);
5272 if (t == TypeManager.int64_type)
5273 return new LongConstant (0, Location.Null);
5274 if (t == TypeManager.uint64_type)
5275 return new ULongConstant (0, Location.Null);
5276 if (t == TypeManager.float_type)
5277 return new FloatConstant (0, Location.Null);
5278 if (t == TypeManager.double_type)
5279 return new DoubleConstant (0, Location.Null);
5280 if (t == TypeManager.short_type)
5281 return new ShortConstant (0, Location.Null);
5282 if (t == TypeManager.ushort_type)
5283 return new UShortConstant (0, Location.Null);
5284 if (t == TypeManager.sbyte_type)
5285 return new SByteConstant (0, Location.Null);
5286 if (t == TypeManager.byte_type)
5287 return new ByteConstant (0, Location.Null);
5288 if (t == TypeManager.char_type)
5289 return new CharConstant ('\0', Location.Null);
5290 if (t == TypeManager.bool_type)
5291 return new BoolConstant (false, Location.Null);
5292 if (t == TypeManager.decimal_type)
5293 return new DecimalConstant (0, Location.Null);
5294 if (TypeManager.IsEnumType (t))
5295 return new EnumConstant (Constantify (EnumSpec.GetUnderlyingType (t)), t);
5296 if (TypeManager.IsNullableType (t))
5297 return Nullable.LiftedNull.Create (t, Location.Null);
5303 // Checks whether the type is an interface that has the
5304 // [ComImport, CoClass] attributes and must be treated
5307 public Expression CheckComImport (ResolveContext ec)
5309 if (!type.IsInterface)
5313 // Turn the call into:
5314 // (the-interface-stated) (new class-referenced-in-coclassattribute ())
5316 var real_class = type.MemberDefinition.GetAttributeCoClass ();
5317 if (real_class == null)
5320 New proxy = new New (new TypeExpression (real_class, loc), Arguments, loc);
5321 Cast cast = new Cast (new TypeExpression (type, loc), proxy, loc);
5322 return cast.Resolve (ec);
5325 public override Expression CreateExpressionTree (ResolveContext ec)
5328 if (method == null) {
5329 args = new Arguments (1);
5330 args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
5332 args = Arguments.CreateForExpressionTree (ec,
5334 method.CreateExpressionTree (ec));
5337 return CreateExpressionFactoryCall (ec, "New", args);
5340 protected override Expression DoResolve (ResolveContext ec)
5343 // The New DoResolve might be called twice when initializing field
5344 // expressions (see EmitFieldInitializers, the call to
5345 // GetInitializerExpression will perform a resolve on the expression,
5346 // and later the assign will trigger another resolution
5348 // This leads to bugs (#37014)
5351 if (RequestedType is NewDelegate)
5352 return RequestedType;
5356 TypeExpr texpr = RequestedType.ResolveAsTypeTerminal (ec, false);
5362 if (type.IsPointer) {
5363 ec.Report.Error (1919, loc, "Unsafe type `{0}' cannot be used in an object creation expression",
5364 TypeManager.CSharpName (type));
5368 if (Arguments == null) {
5369 Constant c = Constantify (type);
5371 return ReducedExpression.Create (c.Resolve (ec), this);
5374 if (TypeManager.IsDelegateType (type)) {
5375 return (new NewDelegate (type, Arguments, loc)).Resolve (ec);
5378 var tparam = type as TypeParameterSpec;
5379 if (tparam != null) {
5380 if (!tparam.HasSpecialConstructor && !tparam.HasSpecialStruct) {
5381 ec.Report.Error (304, loc,
5382 "Cannot create an instance of the variable type `{0}' because it does not have the new() constraint",
5383 TypeManager.CSharpName (type));
5386 if ((Arguments != null) && (Arguments.Count != 0)) {
5387 ec.Report.Error (417, loc,
5388 "`{0}': cannot provide arguments when creating an instance of a variable type",
5389 TypeManager.CSharpName (type));
5392 if (TypeManager.activator_create_instance == null) {
5393 TypeSpec activator_type = TypeManager.CoreLookupType (ec.Compiler, "System", "Activator", MemberKind.Class, true);
5394 if (activator_type != null) {
5395 TypeManager.activator_create_instance = TypeManager.GetPredefinedMethod (
5396 activator_type, MemberFilter.Method ("CreateInstance", 1, ParametersCompiled.EmptyReadOnlyParameters, null), loc);
5400 eclass = ExprClass.Value;
5404 if (type.IsStatic) {
5405 ec.Report.SymbolRelatedToPreviousError (type);
5406 ec.Report.Error (712, loc, "Cannot create an instance of the static class `{0}'", TypeManager.CSharpName (type));
5410 if (type.IsInterface || type.IsAbstract){
5411 if (!TypeManager.IsGenericType (type)) {
5412 RequestedType = CheckComImport (ec);
5413 if (RequestedType != null)
5414 return RequestedType;
5417 ec.Report.SymbolRelatedToPreviousError (type);
5418 ec.Report.Error (144, loc, "Cannot create an instance of the abstract class or interface `{0}'", TypeManager.CSharpName (type));
5422 bool is_struct = TypeManager.IsStruct (type);
5423 eclass = ExprClass.Value;
5426 // SRE returns a match for .ctor () on structs (the object constructor),
5427 // so we have to manually ignore it.
5429 if (is_struct && Arguments == null)
5432 // For member-lookup, treat 'new Foo (bar)' as call to 'foo.ctor (bar)', where 'foo' is of type 'Foo'.
5433 Expression ml = MemberLookupFinal (ec, type, type, ConstructorInfo.ConstructorName, 0,
5434 MemberKind.Constructor, BindingRestriction.AccessibleOnly | BindingRestriction.DeclaredOnly, loc);
5437 if (Arguments != null) {
5438 Arguments.Resolve (ec, out dynamic);
5446 method = ml as MethodGroupExpr;
5447 if (method == null) {
5448 ml.Error_UnexpectedKind (ec, ResolveFlags.MethodGroup, loc);
5452 method = method.OverloadResolve (ec, ref Arguments, false, loc);
5457 Arguments.Insert (0, new Argument (new TypeOf (texpr, loc).Resolve (ec), Argument.AType.DynamicTypeName));
5458 return new DynamicConstructorBinder (type, Arguments, loc).Resolve (ec);
5464 bool DoEmitTypeParameter (EmitContext ec)
5466 var ctor_factory = TypeManager.activator_create_instance.MakeGenericMethod (type);
5467 var tparam = (TypeParameterSpec) type;
5469 if (tparam.IsReferenceType) {
5470 ec.Emit (OpCodes.Call, ctor_factory);
5474 // Allow DoEmit() to be called multiple times.
5475 // We need to create a new LocalTemporary each time since
5476 // you can't share LocalBuilders among ILGeneators.
5477 LocalTemporary temp = new LocalTemporary (type);
5479 Label label_activator = ec.DefineLabel ();
5480 Label label_end = ec.DefineLabel ();
5482 temp.AddressOf (ec, AddressOp.Store);
5483 ec.Emit (OpCodes.Initobj, type);
5486 ec.Emit (OpCodes.Box, type);
5487 ec.Emit (OpCodes.Brfalse, label_activator);
5489 temp.AddressOf (ec, AddressOp.Store);
5490 ec.Emit (OpCodes.Initobj, type);
5492 ec.Emit (OpCodes.Br_S, label_end);
5494 ec.MarkLabel (label_activator);
5496 ec.Emit (OpCodes.Call, ctor_factory);
5497 ec.MarkLabel (label_end);
5502 // This Emit can be invoked in two contexts:
5503 // * As a mechanism that will leave a value on the stack (new object)
5504 // * As one that wont (init struct)
5506 // If we are dealing with a ValueType, we have a few
5507 // situations to deal with:
5509 // * The target is a ValueType, and we have been provided
5510 // the instance (this is easy, we are being assigned).
5512 // * The target of New is being passed as an argument,
5513 // to a boxing operation or a function that takes a
5516 // In this case, we need to create a temporary variable
5517 // that is the argument of New.
5519 // Returns whether a value is left on the stack
5521 // *** Implementation note ***
5523 // To benefit from this optimization, each assignable expression
5524 // has to manually cast to New and call this Emit.
5526 // TODO: It's worth to implement it for arrays and fields
5528 public virtual bool Emit (EmitContext ec, IMemoryLocation target)
5530 bool is_value_type = TypeManager.IsValueType (type);
5531 VariableReference vr = target as VariableReference;
5533 if (target != null && is_value_type && (vr != null || method == null)) {
5534 target.AddressOf (ec, AddressOp.Store);
5535 } else if (vr != null && vr.IsRef) {
5539 if (Arguments != null)
5540 Arguments.Emit (ec);
5542 if (is_value_type) {
5543 if (method == null) {
5544 ec.Emit (OpCodes.Initobj, type);
5549 ec.Emit (OpCodes.Call, method.BestCandidate);
5554 if (type is TypeParameterSpec)
5555 return DoEmitTypeParameter (ec);
5557 ec.Emit (OpCodes.Newobj, method.BestCandidate);
5561 public override void Emit (EmitContext ec)
5563 LocalTemporary v = null;
5564 if (method == null && TypeManager.IsValueType (type)) {
5565 // TODO: Use temporary variable from pool
5566 v = new LocalTemporary (type);
5573 public override void EmitStatement (EmitContext ec)
5575 LocalTemporary v = null;
5576 if (method == null && TypeManager.IsValueType (type)) {
5577 // TODO: Use temporary variable from pool
5578 v = new LocalTemporary (type);
5582 ec.Emit (OpCodes.Pop);
5585 public virtual bool HasInitializer {
5591 public void AddressOf (EmitContext ec, AddressOp mode)
5593 EmitAddressOf (ec, mode);
5596 protected virtual IMemoryLocation EmitAddressOf (EmitContext ec, AddressOp mode)
5598 LocalTemporary value_target = new LocalTemporary (type);
5600 if (type is TypeParameterSpec) {
5601 DoEmitTypeParameter (ec);
5602 value_target.Store (ec);
5603 value_target.AddressOf (ec, mode);
5604 return value_target;
5607 if (!TypeManager.IsStruct (type)){
5609 // We throw an exception. So far, I believe we only need to support
5611 // foreach (int j in new StructType ())
5614 throw new Exception ("AddressOf should not be used for classes");
5617 value_target.AddressOf (ec, AddressOp.Store);
5619 if (method == null) {
5620 ec.Emit (OpCodes.Initobj, type);
5622 if (Arguments != null)
5623 Arguments.Emit (ec);
5625 ec.Emit (OpCodes.Call, method.BestCandidate);
5628 value_target.AddressOf (ec, mode);
5629 return value_target;
5632 protected override void CloneTo (CloneContext clonectx, Expression t)
5634 New target = (New) t;
5636 target.RequestedType = RequestedType.Clone (clonectx);
5637 if (Arguments != null){
5638 target.Arguments = Arguments.Clone (clonectx);
5642 public override SLE.Expression MakeExpression (BuilderContext ctx)
5644 return SLE.Expression.New ((ConstructorInfo) method.BestCandidate.GetMetaInfo (), Arguments.MakeExpression (Arguments, ctx));
5648 public class ArrayInitializer : ShimExpression
5650 List<Expression> elements;
5652 public ArrayInitializer (List<Expression> init, Location loc)
5658 public ArrayInitializer (int count, Location loc)
5661 elements = new List<Expression> (count);
5664 public ArrayInitializer (Location loc)
5669 public void Add (Expression expr)
5671 elements.Add (expr);
5674 protected override void CloneTo (CloneContext clonectx, Expression t)
5676 var target = (ArrayInitializer) t;
5678 target.elements = new List<Expression> (elements.Count);
5679 foreach (var element in elements)
5680 target.elements.Add (element.Clone (clonectx));
5682 base.CloneTo (clonectx, t);
5686 get { return elements.Count; }
5689 protected override Expression DoResolve (ResolveContext rc)
5691 throw new NotImplementedException ();
5694 public Expression this [int index] {
5695 get { return elements [index]; }
5700 /// 14.5.10.2: Represents an array creation expression.
5704 /// There are two possible scenarios here: one is an array creation
5705 /// expression that specifies the dimensions and optionally the
5706 /// initialization data and the other which does not need dimensions
5707 /// specified but where initialization data is mandatory.
5709 class ArrayCreation : Expression
5711 FullNamedExpression requested_base_type;
5712 ArrayInitializer initializers;
5715 // The list of Argument types.
5716 // This is used to construct the `newarray' or constructor signature
5718 protected List<Expression> arguments;
5720 protected TypeSpec array_element_type;
5721 bool expect_initializers = false;
5722 int num_arguments = 0;
5723 protected int dimensions;
5724 protected readonly string rank;
5725 Expression first_emit;
5726 LocalTemporary first_emit_temp;
5728 protected List<Expression> array_data;
5730 Dictionary<int, int> bounds;
5732 // The number of constants in array initializers
5733 int const_initializers_count;
5734 bool only_constant_initializers;
5736 public ArrayCreation (FullNamedExpression requested_base_type, List<Expression> exprs, string rank, ArrayInitializer initializers, Location l)
5738 this.requested_base_type = requested_base_type;
5739 this.initializers = initializers;
5743 arguments = new List<Expression> (exprs);
5744 num_arguments = arguments.Count;
5747 public ArrayCreation (FullNamedExpression requested_base_type, string rank, ArrayInitializer initializers, Location l)
5749 this.requested_base_type = requested_base_type;
5750 this.initializers = initializers;
5754 //this.rank = rank.Substring (0, rank.LastIndexOf ('['));
5756 //string tmp = rank.Substring (rank.LastIndexOf ('['));
5758 //dimensions = tmp.Length - 1;
5759 expect_initializers = true;
5762 protected override void Error_NegativeArrayIndex (ResolveContext ec, Location loc)
5764 ec.Report.Error (248, loc, "Cannot create an array with a negative size");
5767 bool CheckIndices (ResolveContext ec, ArrayInitializer probe, int idx, bool specified_dims, int child_bounds)
5769 if (specified_dims) {
5770 Expression a = arguments [idx];
5775 Constant c = a as Constant;
5777 c = c.ImplicitConversionRequired (ec, TypeManager.int32_type, a.Location);
5781 ec.Report.Error (150, a.Location, "A constant value is expected");
5785 int value = (int) c.GetValue ();
5787 if (value != probe.Count) {
5788 ec.Report.Error (847, loc, "An array initializer of length `{0}' was expected", value.ToString ());
5792 bounds [idx] = value;
5795 only_constant_initializers = true;
5796 for (int i = 0; i < probe.Count; ++i) {
5798 if (o is ArrayInitializer) {
5799 var sub_probe = o as ArrayInitializer;
5800 if (idx + 1 >= dimensions){
5801 ec.Report.Error (623, loc, "Array initializers can only be used in a variable or field initializer. Try using a new expression instead");
5805 bool ret = CheckIndices (ec, sub_probe, idx + 1, specified_dims, child_bounds - 1);
5808 } else if (child_bounds > 1) {
5809 ec.Report.Error (846, o.Location, "A nested array initializer was expected");
5811 Expression element = ResolveArrayElement (ec, o);
5812 if (element == null)
5815 // Initializers with the default values can be ignored
5816 Constant c = element as Constant;
5818 if (!c.IsDefaultInitializer (array_element_type)) {
5819 ++const_initializers_count;
5822 only_constant_initializers = false;
5825 array_data.Add (element);
5832 public override Expression CreateExpressionTree (ResolveContext ec)
5836 if (array_data == null) {
5837 args = new Arguments (arguments.Count + 1);
5838 args.Add (new Argument (new TypeOf (new TypeExpression (array_element_type, loc), loc)));
5839 foreach (Expression a in arguments)
5840 args.Add (new Argument (a.CreateExpressionTree (ec)));
5842 return CreateExpressionFactoryCall (ec, "NewArrayBounds", args);
5845 if (dimensions > 1) {
5846 ec.Report.Error (838, loc, "An expression tree cannot contain a multidimensional array initializer");
5850 args = new Arguments (array_data == null ? 1 : array_data.Count + 1);
5851 args.Add (new Argument (new TypeOf (new TypeExpression (array_element_type, loc), loc)));
5852 if (array_data != null) {
5853 for (int i = 0; i < array_data.Count; ++i) {
5854 Expression e = array_data [i];
5855 args.Add (new Argument (e.CreateExpressionTree (ec)));
5859 return CreateExpressionFactoryCall (ec, "NewArrayInit", args);
5862 public void UpdateIndices ()
5865 for (var probe = initializers; probe != null;) {
5866 if (probe.Count > 0 && probe [0] is ArrayInitializer) {
5867 Expression e = new IntConstant (probe.Count, Location.Null);
5870 bounds [i++] = probe.Count;
5872 probe = (ArrayInitializer) probe[0];
5875 Expression e = new IntConstant (probe.Count, Location.Null);
5878 bounds [i++] = probe.Count;
5884 protected virtual Expression ResolveArrayElement (ResolveContext ec, Expression element)
5886 element = element.Resolve (ec);
5887 if (element == null)
5890 if (element is CompoundAssign.TargetExpression) {
5891 if (first_emit != null)
5892 throw new InternalErrorException ("Can only handle one mutator at a time");
5893 first_emit = element;
5894 element = first_emit_temp = new LocalTemporary (element.Type);
5897 return Convert.ImplicitConversionRequired (
5898 ec, element, array_element_type, loc);
5901 protected bool ResolveInitializers (ResolveContext ec)
5903 if (initializers == null) {
5904 return !expect_initializers;
5908 // We use this to store all the date values in the order in which we
5909 // will need to store them in the byte blob later
5911 array_data = new List<Expression> ();
5912 bounds = new Dictionary<int, int> ();
5914 if (arguments != null)
5915 return CheckIndices (ec, initializers, 0, true, dimensions);
5917 arguments = new List<Expression> ();
5919 if (!CheckIndices (ec, initializers, 0, false, dimensions))
5928 // Resolved the type of the array
5930 bool ResolveArrayType (ResolveContext ec)
5932 if (requested_base_type is VarExpr) {
5933 ec.Report.Error (820, loc, "An implicitly typed local variable declarator cannot use an array initializer");
5937 StringBuilder array_qualifier = new StringBuilder ();
5940 // `In the first form allocates an array instace of the type that results
5941 // from deleting each of the individual expression from the expression list'
5943 if (num_arguments > 0) {
5944 array_qualifier.Append ("[");
5945 for (int i = num_arguments-1; i > 0; i--)
5946 array_qualifier.Append (",");
5947 array_qualifier.Append ("]");
5950 array_qualifier.Append (rank);
5955 TypeExpr array_type_expr;
5956 array_type_expr = new ComposedCast (requested_base_type, array_qualifier.ToString (), loc);
5957 array_type_expr = array_type_expr.ResolveAsTypeTerminal (ec, false);
5958 if (array_type_expr == null)
5961 type = array_type_expr.Type;
5962 var ac = type as ArrayContainer;
5964 ec.Report.Error (622, loc, "Can only use array initializer expressions to assign to array types. Try using a new expression instead");
5968 array_element_type = ac.Element;
5969 dimensions = ac.Rank;
5974 protected override Expression DoResolve (ResolveContext ec)
5979 if (!ResolveArrayType (ec))
5983 // First step is to validate the initializers and fill
5984 // in any missing bits
5986 if (!ResolveInitializers (ec))
5989 for (int i = 0; i < arguments.Count; ++i) {
5990 Expression e = arguments[i].Resolve (ec);
5994 arguments [i] = ConvertExpressionToArrayIndex (ec, e);
5997 eclass = ExprClass.Value;
6001 byte [] MakeByteBlob ()
6006 int count = array_data.Count;
6008 TypeSpec element_type = array_element_type;
6009 if (TypeManager.IsEnumType (element_type))
6010 element_type = EnumSpec.GetUnderlyingType (element_type);
6012 factor = GetTypeSize (element_type);
6014 throw new Exception ("unrecognized type in MakeByteBlob: " + element_type);
6016 data = new byte [(count * factor + 3) & ~3];
6019 for (int i = 0; i < count; ++i) {
6020 object v = array_data [i];
6022 if (v is EnumConstant)
6023 v = ((EnumConstant) v).Child;
6025 if (v is Constant && !(v is StringConstant))
6026 v = ((Constant) v).GetValue ();
6032 if (element_type == TypeManager.int64_type){
6033 if (!(v is Expression)){
6034 long val = (long) v;
6036 for (int j = 0; j < factor; ++j) {
6037 data [idx + j] = (byte) (val & 0xFF);
6041 } else if (element_type == TypeManager.uint64_type){
6042 if (!(v is Expression)){
6043 ulong val = (ulong) v;
6045 for (int j = 0; j < factor; ++j) {
6046 data [idx + j] = (byte) (val & 0xFF);
6050 } else if (element_type == TypeManager.float_type) {
6051 if (!(v is Expression)){
6052 element = BitConverter.GetBytes ((float) v);
6054 for (int j = 0; j < factor; ++j)
6055 data [idx + j] = element [j];
6056 if (!BitConverter.IsLittleEndian)
6057 System.Array.Reverse (data, idx, 4);
6059 } else if (element_type == TypeManager.double_type) {
6060 if (!(v is Expression)){
6061 element = BitConverter.GetBytes ((double) v);
6063 for (int j = 0; j < factor; ++j)
6064 data [idx + j] = element [j];
6066 // FIXME: Handle the ARM float format.
6067 if (!BitConverter.IsLittleEndian)
6068 System.Array.Reverse (data, idx, 8);
6070 } else if (element_type == TypeManager.char_type){
6071 if (!(v is Expression)){
6072 int val = (int) ((char) v);
6074 data [idx] = (byte) (val & 0xff);
6075 data [idx+1] = (byte) (val >> 8);
6077 } else if (element_type == TypeManager.short_type){
6078 if (!(v is Expression)){
6079 int val = (int) ((short) v);
6081 data [idx] = (byte) (val & 0xff);
6082 data [idx+1] = (byte) (val >> 8);
6084 } else if (element_type == TypeManager.ushort_type){
6085 if (!(v is Expression)){
6086 int val = (int) ((ushort) v);
6088 data [idx] = (byte) (val & 0xff);
6089 data [idx+1] = (byte) (val >> 8);
6091 } else if (element_type == TypeManager.int32_type) {
6092 if (!(v is Expression)){
6095 data [idx] = (byte) (val & 0xff);
6096 data [idx+1] = (byte) ((val >> 8) & 0xff);
6097 data [idx+2] = (byte) ((val >> 16) & 0xff);
6098 data [idx+3] = (byte) (val >> 24);
6100 } else if (element_type == TypeManager.uint32_type) {
6101 if (!(v is Expression)){
6102 uint val = (uint) v;
6104 data [idx] = (byte) (val & 0xff);
6105 data [idx+1] = (byte) ((val >> 8) & 0xff);
6106 data [idx+2] = (byte) ((val >> 16) & 0xff);
6107 data [idx+3] = (byte) (val >> 24);
6109 } else if (element_type == TypeManager.sbyte_type) {
6110 if (!(v is Expression)){
6111 sbyte val = (sbyte) v;
6112 data [idx] = (byte) val;
6114 } else if (element_type == TypeManager.byte_type) {
6115 if (!(v is Expression)){
6116 byte val = (byte) v;
6117 data [idx] = (byte) val;
6119 } else if (element_type == TypeManager.bool_type) {
6120 if (!(v is Expression)){
6121 bool val = (bool) v;
6122 data [idx] = (byte) (val ? 1 : 0);
6124 } else if (element_type == TypeManager.decimal_type){
6125 if (!(v is Expression)){
6126 int [] bits = Decimal.GetBits ((decimal) v);
6129 // FIXME: For some reason, this doesn't work on the MS runtime.
6130 int [] nbits = new int [4];
6131 nbits [0] = bits [3];
6132 nbits [1] = bits [2];
6133 nbits [2] = bits [0];
6134 nbits [3] = bits [1];
6136 for (int j = 0; j < 4; j++){
6137 data [p++] = (byte) (nbits [j] & 0xff);
6138 data [p++] = (byte) ((nbits [j] >> 8) & 0xff);
6139 data [p++] = (byte) ((nbits [j] >> 16) & 0xff);
6140 data [p++] = (byte) (nbits [j] >> 24);
6144 throw new Exception ("Unrecognized type in MakeByteBlob: " + element_type);
6154 public override SLE.Expression MakeExpression (BuilderContext ctx)
6156 var initializers = new SLE.Expression [array_data.Count];
6157 for (var i = 0; i < initializers.Length; i++) {
6158 if (array_data [i] == null)
6159 initializers [i] = SLE.Expression.Default (array_element_type.GetMetaInfo ());
6161 initializers [i] = array_data [i].MakeExpression (ctx);
6164 return SLE.Expression.NewArrayInit (array_element_type.GetMetaInfo (), initializers);
6168 // Emits the initializers for the array
6170 void EmitStaticInitializers (EmitContext ec)
6172 // FIXME: This should go to Resolve !
6173 if (TypeManager.void_initializearray_array_fieldhandle == null) {
6174 TypeManager.void_initializearray_array_fieldhandle = TypeManager.GetPredefinedMethod (
6175 TypeManager.runtime_helpers_type, "InitializeArray", loc,
6176 TypeManager.array_type, TypeManager.runtime_field_handle_type);
6177 if (TypeManager.void_initializearray_array_fieldhandle == null)
6182 // First, the static data
6186 byte [] data = MakeByteBlob ();
6188 fb = RootContext.MakeStaticData (data);
6190 ec.Emit (OpCodes.Dup);
6191 ec.Emit (OpCodes.Ldtoken, fb);
6192 ec.Emit (OpCodes.Call, TypeManager.void_initializearray_array_fieldhandle);
6196 // Emits pieces of the array that can not be computed at compile
6197 // time (variables and string locations).
6199 // This always expect the top value on the stack to be the array
6201 void EmitDynamicInitializers (EmitContext ec, bool emitConstants)
6203 int dims = bounds.Count;
6204 int [] current_pos = new int [dims];
6206 for (int i = 0; i < array_data.Count; i++){
6208 Expression e = array_data [i];
6209 var c = e as Constant;
6211 // Constant can be initialized via StaticInitializer
6212 if (c == null || (c != null && emitConstants && !c.IsDefaultInitializer (array_element_type))) {
6213 TypeSpec etype = e.Type;
6215 ec.Emit (OpCodes.Dup);
6217 for (int idx = 0; idx < dims; idx++)
6218 ec.EmitInt (current_pos [idx]);
6221 // If we are dealing with a struct, get the
6222 // address of it, so we can store it.
6224 if ((dims == 1) && TypeManager.IsStruct (etype) &&
6225 (!TypeManager.IsBuiltinOrEnum (etype) ||
6226 etype == TypeManager.decimal_type)) {
6228 ec.Emit (OpCodes.Ldelema, etype);
6233 ec.EmitArrayStore ((ArrayContainer) type);
6239 for (int j = dims - 1; j >= 0; j--){
6241 if (current_pos [j] < bounds [j])
6243 current_pos [j] = 0;
6248 public override void Emit (EmitContext ec)
6250 if (first_emit != null) {
6251 first_emit.Emit (ec);
6252 first_emit_temp.Store (ec);
6255 foreach (Expression e in arguments)
6258 ec.EmitArrayNew ((ArrayContainer) type);
6260 if (initializers == null)
6263 // Emit static initializer for arrays which have contain more than 2 items and
6264 // the static initializer will initialize at least 25% of array values.
6265 // NOTE: const_initializers_count does not contain default constant values.
6266 if (const_initializers_count > 2 && const_initializers_count * 4 > (array_data.Count) &&
6267 (TypeManager.IsPrimitiveType (array_element_type) || TypeManager.IsEnumType (array_element_type))) {
6268 EmitStaticInitializers (ec);
6270 if (!only_constant_initializers)
6271 EmitDynamicInitializers (ec, false);
6273 EmitDynamicInitializers (ec, true);
6276 if (first_emit_temp != null)
6277 first_emit_temp.Release (ec);
6280 public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
6282 // no multi dimensional or jagged arrays
6283 if (arguments.Count != 1 || array_element_type.IsArray) {
6284 base.EncodeAttributeValue (rc, enc, targetType);
6288 // No array covariance, except for array -> object
6289 if (type != targetType) {
6290 if (targetType != TypeManager.object_type) {
6291 base.EncodeAttributeValue (rc, enc, targetType);
6298 // Single dimensional array of 0 size
6299 if (array_data == null) {
6300 IntConstant ic = arguments[0] as IntConstant;
6301 if (ic == null || !ic.IsDefaultValue) {
6302 base.EncodeAttributeValue (rc, enc, targetType);
6304 enc.Stream.Write (0);
6310 enc.Stream.Write ((int) array_data.Count);
6311 foreach (var element in array_data) {
6312 element.EncodeAttributeValue (rc, enc, array_element_type);
6316 protected override void CloneTo (CloneContext clonectx, Expression t)
6318 ArrayCreation target = (ArrayCreation) t;
6320 if (requested_base_type != null)
6321 target.requested_base_type = (FullNamedExpression)requested_base_type.Clone (clonectx);
6323 if (arguments != null){
6324 target.arguments = new List<Expression> (arguments.Count);
6325 foreach (Expression e in arguments)
6326 target.arguments.Add (e.Clone (clonectx));
6329 if (initializers != null)
6330 target.initializers = (ArrayInitializer) initializers.Clone (clonectx);
6335 // Represents an implicitly typed array epxression
6337 class ImplicitlyTypedArrayCreation : ArrayCreation
6339 public ImplicitlyTypedArrayCreation (string rank, ArrayInitializer initializers, Location loc)
6340 : base (null, rank, initializers, loc)
6342 if (rank.Length > 2) {
6343 while (rank [++dimensions] == ',');
6349 protected override Expression DoResolve (ResolveContext ec)
6354 if (!ResolveInitializers (ec))
6357 if (array_element_type == null || array_element_type == TypeManager.null_type ||
6358 array_element_type == TypeManager.void_type || array_element_type == InternalType.AnonymousMethod ||
6359 array_element_type == InternalType.MethodGroup ||
6360 arguments.Count != dimensions) {
6361 Error_NoBestType (ec);
6366 // At this point we found common base type for all initializer elements
6367 // but we have to be sure that all static initializer elements are of
6370 UnifyInitializerElement (ec);
6372 type = TypeManager.GetConstructedType (array_element_type, rank);
6373 eclass = ExprClass.Value;
6377 void Error_NoBestType (ResolveContext ec)
6379 ec.Report.Error (826, loc,
6380 "The type of an implicitly typed array cannot be inferred from the initializer. Try specifying array type explicitly");
6384 // Converts static initializer only
6386 void UnifyInitializerElement (ResolveContext ec)
6388 for (int i = 0; i < array_data.Count; ++i) {
6389 Expression e = (Expression)array_data[i];
6391 array_data [i] = Convert.ImplicitConversion (ec, e, array_element_type, Location.Null);
6395 protected override Expression ResolveArrayElement (ResolveContext ec, Expression element)
6397 element = element.Resolve (ec);
6398 if (element == null)
6401 if (array_element_type == null) {
6402 if (element.Type != TypeManager.null_type)
6403 array_element_type = element.Type;
6408 if (Convert.ImplicitConversionExists (ec, element, array_element_type)) {
6412 if (Convert.ImplicitConversionExists (ec, new TypeExpression (array_element_type, loc), element.Type)) {
6413 array_element_type = element.Type;
6417 Error_NoBestType (ec);
6422 public sealed class CompilerGeneratedThis : This
6424 public static This Instance = new CompilerGeneratedThis ();
6426 private CompilerGeneratedThis ()
6427 : base (Location.Null)
6431 public CompilerGeneratedThis (TypeSpec type, Location loc)
6437 protected override Expression DoResolve (ResolveContext ec)
6439 eclass = ExprClass.Variable;
6441 type = ec.CurrentType;
6443 is_struct = TypeManager.IsStruct (type);
6447 public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
6454 /// Represents the `this' construct
6457 public class This : VariableReference
6459 sealed class ThisVariable : ILocalVariable
6461 public static readonly ILocalVariable Instance = new ThisVariable ();
6463 public void Emit (EmitContext ec)
6465 ec.Emit (OpCodes.Ldarg_0);
6468 public void EmitAssign (EmitContext ec)
6470 throw new InvalidOperationException ();
6473 public void EmitAddressOf (EmitContext ec)
6475 ec.Emit (OpCodes.Ldarg_0);
6480 VariableInfo variable_info;
6481 protected bool is_struct;
6483 public This (Block block, Location loc)
6489 public This (Location loc)
6494 public override VariableInfo VariableInfo {
6495 get { return variable_info; }
6498 public override bool IsFixed {
6499 get { return false; }
6502 public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
6507 AnonymousMethodStorey storey = ae.Storey;
6508 while (storey != null) {
6509 AnonymousMethodStorey temp = storey.Parent as AnonymousMethodStorey;
6511 return storey.HoistedThis;
6519 public override bool IsRef {
6520 get { return is_struct; }
6523 protected override ILocalVariable Variable {
6524 get { return ThisVariable.Instance; }
6527 public static bool IsThisAvailable (ResolveContext ec)
6529 if (ec.IsStatic || ec.HasAny (ResolveContext.Options.FieldInitializerScope | ResolveContext.Options.BaseInitializer | ResolveContext.Options.ConstantScope))
6532 if (ec.CurrentAnonymousMethod == null)
6535 if (TypeManager.IsStruct (ec.CurrentType) && ec.CurrentIterator == null)
6541 public bool ResolveBase (ResolveContext ec)
6543 eclass = ExprClass.Variable;
6544 type = ec.CurrentType;
6546 if (!IsThisAvailable (ec)) {
6547 if (ec.IsStatic && !ec.HasSet (ResolveContext.Options.ConstantScope)) {
6548 ec.Report.Error (26, loc, "Keyword `this' is not valid in a static property, static method, or static field initializer");
6549 } else if (ec.CurrentAnonymousMethod != null) {
6550 ec.Report.Error (1673, loc,
6551 "Anonymous methods inside structs cannot access instance members of `this'. " +
6552 "Consider copying `this' to a local variable outside the anonymous method and using the local instead");
6554 ec.Report.Error (27, loc, "Keyword `this' is not available in the current context");
6558 is_struct = TypeManager.IsStruct (type);
6560 if (block != null) {
6561 if (block.Toplevel.ThisVariable != null)
6562 variable_info = block.Toplevel.ThisVariable.VariableInfo;
6564 AnonymousExpression am = ec.CurrentAnonymousMethod;
6565 if (am != null && ec.IsVariableCapturingRequired) {
6566 am.SetHasThisAccess ();
6574 // Called from Invocation to check if the invocation is correct
6576 public override void CheckMarshalByRefAccess (ResolveContext ec)
6578 if ((variable_info != null) && !(TypeManager.IsStruct (type) && ec.OmitStructFlowAnalysis) &&
6579 !variable_info.IsAssigned (ec)) {
6580 ec.Report.Error (188, loc,
6581 "The `this' object cannot be used before all of its fields are assigned to");
6582 variable_info.SetAssigned (ec);
6586 public override Expression CreateExpressionTree (ResolveContext ec)
6588 Arguments args = new Arguments (1);
6589 args.Add (new Argument (this));
6591 // Use typeless constant for ldarg.0 to save some
6592 // space and avoid problems with anonymous stories
6593 return CreateExpressionFactoryCall (ec, "Constant", args);
6596 protected override Expression DoResolve (ResolveContext ec)
6602 override public Expression DoResolveLValue (ResolveContext ec, Expression right_side)
6604 if (!ResolveBase (ec))
6607 if (variable_info != null)
6608 variable_info.SetAssigned (ec);
6610 if (ec.CurrentType.IsClass){
6611 if (right_side == EmptyExpression.UnaryAddress)
6612 ec.Report.Error (459, loc, "Cannot take the address of `this' because it is read-only");
6613 else if (right_side == EmptyExpression.OutAccess.Instance)
6614 ec.Report.Error (1605, loc, "Cannot pass `this' as a ref or out argument because it is read-only");
6616 ec.Report.Error (1604, loc, "Cannot assign to `this' because it is read-only");
6622 public override int GetHashCode()
6624 return block.GetHashCode ();
6627 public override string Name {
6628 get { return "this"; }
6631 public override bool Equals (object obj)
6633 This t = obj as This;
6637 return block == t.block;
6640 protected override void CloneTo (CloneContext clonectx, Expression t)
6642 This target = (This) t;
6644 target.block = clonectx.LookupBlock (block);
6647 public override void SetHasAddressTaken ()
6654 /// Represents the `__arglist' construct
6656 public class ArglistAccess : Expression
6658 public ArglistAccess (Location loc)
6663 public override Expression CreateExpressionTree (ResolveContext ec)
6665 throw new NotSupportedException ("ET");
6668 protected override Expression DoResolve (ResolveContext ec)
6670 eclass = ExprClass.Variable;
6671 type = TypeManager.runtime_argument_handle_type;
6673 if (ec.HasSet (ResolveContext.Options.FieldInitializerScope) || !ec.CurrentBlock.Toplevel.Parameters.HasArglist) {
6674 ec.Report.Error (190, loc,
6675 "The __arglist construct is valid only within a variable argument method");
6681 public override void Emit (EmitContext ec)
6683 ec.Emit (OpCodes.Arglist);
6686 protected override void CloneTo (CloneContext clonectx, Expression target)
6693 /// Represents the `__arglist (....)' construct
6695 class Arglist : Expression
6697 Arguments Arguments;
6699 public Arglist (Location loc)
6704 public Arglist (Arguments args, Location l)
6710 public Type[] ArgumentTypes {
6712 if (Arguments == null)
6713 return System.Type.EmptyTypes;
6715 var retval = new Type [Arguments.Count];
6716 for (int i = 0; i < retval.Length; i++)
6717 retval[i] = Arguments[i].Expr.Type.GetMetaInfo ();
6723 public override Expression CreateExpressionTree (ResolveContext ec)
6725 ec.Report.Error (1952, loc, "An expression tree cannot contain a method with variable arguments");
6729 protected override Expression DoResolve (ResolveContext ec)
6731 eclass = ExprClass.Variable;
6732 type = InternalType.Arglist;
6733 if (Arguments != null) {
6734 bool dynamic; // Can be ignored as there is always only 1 overload
6735 Arguments.Resolve (ec, out dynamic);
6741 public override void Emit (EmitContext ec)
6743 if (Arguments != null)
6744 Arguments.Emit (ec);
6747 protected override void CloneTo (CloneContext clonectx, Expression t)
6749 Arglist target = (Arglist) t;
6751 if (Arguments != null)
6752 target.Arguments = Arguments.Clone (clonectx);
6757 /// Implements the typeof operator
6759 public class TypeOf : Expression {
6760 Expression QueriedType;
6761 protected TypeSpec typearg;
6763 public TypeOf (Expression queried_type, Location l)
6765 QueriedType = queried_type;
6769 public override Expression CreateExpressionTree (ResolveContext ec)
6771 Arguments args = new Arguments (2);
6772 args.Add (new Argument (this));
6773 args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
6774 return CreateExpressionFactoryCall (ec, "Constant", args);
6777 protected override Expression DoResolve (ResolveContext ec)
6779 TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec, false);
6783 typearg = texpr.Type;
6785 if (typearg == TypeManager.void_type) {
6786 ec.Report.Error (673, loc, "System.Void cannot be used from C#. Use typeof (void) to get the void type object");
6787 } else if (typearg.IsPointer && !ec.IsUnsafe){
6788 UnsafeError (ec, loc);
6789 } else if (texpr is DynamicTypeExpr) {
6790 ec.Report.Error (1962, QueriedType.Location,
6791 "The typeof operator cannot be used on the dynamic type");
6794 type = TypeManager.type_type;
6796 return DoResolveBase ();
6799 protected Expression DoResolveBase ()
6801 if (TypeManager.system_type_get_type_from_handle == null) {
6802 TypeManager.system_type_get_type_from_handle = TypeManager.GetPredefinedMethod (
6803 TypeManager.type_type, "GetTypeFromHandle", loc, TypeManager.runtime_handle_type);
6806 // Even though what is returned is a type object, it's treated as a value by the compiler.
6807 // In particular, 'typeof (Foo).X' is something totally different from 'Foo.X'.
6808 eclass = ExprClass.Value;
6812 public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
6814 // Target type is not System.Type therefore must be object
6815 // and we need to use different encoding sequence
6816 if (targetType != type)
6820 var gi = typearg as InflatedTypeSpec;
6822 // TODO: This has to be recursive, handle arrays, etc.
6823 // I could probably do it after CustomAttribute encoder rewrite
6824 foreach (var ta in gi.TypeArguments) {
6825 if (ta.IsGenericParameter) {
6826 ec.Report.SymbolRelatedToPreviousError (typearg);
6827 ec.Report.Error (416, loc, "`{0}': an attribute argument cannot use type parameters",
6828 TypeManager.CSharpName (typearg));
6836 if (!enc.EncodeTypeName (typearg)) {
6837 rc.Compiler.Report.SymbolRelatedToPreviousError (typearg);
6838 rc.Compiler.Report.Error (416, loc, "`{0}': an attribute argument cannot use type parameters",
6839 TypeManager.CSharpName (typearg));
6843 public override void Emit (EmitContext ec)
6845 ec.Emit (OpCodes.Ldtoken, typearg);
6846 ec.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
6849 public TypeSpec TypeArgument {
6855 protected override void CloneTo (CloneContext clonectx, Expression t)
6857 TypeOf target = (TypeOf) t;
6858 if (QueriedType != null)
6859 target.QueriedType = QueriedType.Clone (clonectx);
6864 /// Implements the `typeof (void)' operator
6866 public class TypeOfVoid : TypeOf {
6867 public TypeOfVoid (Location l) : base (null, l)
6872 protected override Expression DoResolve (ResolveContext ec)
6874 type = TypeManager.type_type;
6875 typearg = TypeManager.void_type;
6877 return DoResolveBase ();
6881 class TypeOfMethod : TypeOfMember<MethodSpec>
6883 public TypeOfMethod (MethodSpec method, Location loc)
6884 : base (method, loc)
6888 protected override Expression DoResolve (ResolveContext ec)
6890 if (member.IsConstructor) {
6891 type = TypeManager.ctorinfo_type;
6893 type = TypeManager.ctorinfo_type = TypeManager.CoreLookupType (ec.Compiler, "System.Reflection", "ConstructorInfo", MemberKind.Class, true);
6895 type = TypeManager.methodinfo_type;
6897 type = TypeManager.methodinfo_type = TypeManager.CoreLookupType (ec.Compiler, "System.Reflection", "MethodInfo", MemberKind.Class, true);
6900 return base.DoResolve (ec);
6903 public override void Emit (EmitContext ec)
6905 ec.Emit (OpCodes.Ldtoken, member);
6908 ec.Emit (OpCodes.Castclass, type);
6911 protected override string GetMethodName {
6912 get { return "GetMethodFromHandle"; }
6915 protected override string RuntimeHandleName {
6916 get { return "RuntimeMethodHandle"; }
6919 protected override MethodSpec TypeFromHandle {
6921 return TypeManager.methodbase_get_type_from_handle;
6924 TypeManager.methodbase_get_type_from_handle = value;
6928 protected override MethodSpec TypeFromHandleGeneric {
6930 return TypeManager.methodbase_get_type_from_handle_generic;
6933 TypeManager.methodbase_get_type_from_handle_generic = value;
6937 protected override string TypeName {
6938 get { return "MethodBase"; }
6942 abstract class TypeOfMember<T> : Expression where T : MemberSpec
6944 protected readonly T member;
6946 protected TypeOfMember (T member, Location loc)
6948 this.member = member;
6952 public override Expression CreateExpressionTree (ResolveContext ec)
6954 Arguments args = new Arguments (2);
6955 args.Add (new Argument (this));
6956 args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
6957 return CreateExpressionFactoryCall (ec, "Constant", args);
6960 protected override Expression DoResolve (ResolveContext ec)
6962 bool is_generic = member.DeclaringType.IsGenericOrParentIsGeneric;
6963 var mi = is_generic ? TypeFromHandleGeneric : TypeFromHandle;
6966 TypeSpec t = TypeManager.CoreLookupType (ec.Compiler, "System.Reflection", TypeName, MemberKind.Class, true);
6967 TypeSpec handle_type = TypeManager.CoreLookupType (ec.Compiler, "System", RuntimeHandleName, MemberKind.Struct, true);
6969 if (t == null || handle_type == null)
6972 mi = TypeManager.GetPredefinedMethod (t, GetMethodName, loc,
6974 new TypeSpec[] { handle_type, TypeManager.runtime_handle_type } :
6975 new TypeSpec[] { handle_type } );
6978 TypeFromHandleGeneric = mi;
6980 TypeFromHandle = mi;
6983 eclass = ExprClass.Value;
6987 public override void Emit (EmitContext ec)
6989 bool is_generic = member.DeclaringType.IsGenericOrParentIsGeneric;
6992 mi = TypeFromHandleGeneric;
6993 ec.Emit (OpCodes.Ldtoken, member.DeclaringType);
6995 mi = TypeFromHandle;
6998 ec.Emit (OpCodes.Call, mi);
7001 protected abstract string GetMethodName { get; }
7002 protected abstract string RuntimeHandleName { get; }
7003 protected abstract MethodSpec TypeFromHandle { get; set; }
7004 protected abstract MethodSpec TypeFromHandleGeneric { get; set; }
7005 protected abstract string TypeName { get; }
7008 class TypeOfField : TypeOfMember<FieldSpec>
7010 public TypeOfField (FieldSpec field, Location loc)
7015 protected override Expression DoResolve (ResolveContext ec)
7017 if (TypeManager.fieldinfo_type == null)
7018 TypeManager.fieldinfo_type = TypeManager.CoreLookupType (ec.Compiler, "System.Reflection", TypeName, MemberKind.Class, true);
7020 type = TypeManager.fieldinfo_type;
7021 return base.DoResolve (ec);
7024 public override void Emit (EmitContext ec)
7026 ec.Emit (OpCodes.Ldtoken, member);
7030 protected override string GetMethodName {
7031 get { return "GetFieldFromHandle"; }
7034 protected override string RuntimeHandleName {
7035 get { return "RuntimeFieldHandle"; }
7038 protected override MethodSpec TypeFromHandle {
7040 return TypeManager.fieldinfo_get_field_from_handle;
7043 TypeManager.fieldinfo_get_field_from_handle = value;
7047 protected override MethodSpec TypeFromHandleGeneric {
7049 return TypeManager.fieldinfo_get_field_from_handle_generic;
7052 TypeManager.fieldinfo_get_field_from_handle_generic = value;
7056 protected override string TypeName {
7057 get { return "FieldInfo"; }
7062 /// Implements the sizeof expression
7064 public class SizeOf : Expression {
7065 readonly Expression QueriedType;
7066 TypeSpec type_queried;
7068 public SizeOf (Expression queried_type, Location l)
7070 this.QueriedType = queried_type;
7074 public override Expression CreateExpressionTree (ResolveContext ec)
7076 Error_PointerInsideExpressionTree (ec);
7080 protected override Expression DoResolve (ResolveContext ec)
7082 TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec, false);
7086 type_queried = texpr.Type;
7087 if (TypeManager.IsEnumType (type_queried))
7088 type_queried = EnumSpec.GetUnderlyingType (type_queried);
7090 int size_of = GetTypeSize (type_queried);
7092 return new IntConstant (size_of, loc).Resolve (ec);
7095 if (!TypeManager.VerifyUnmanaged (ec.Compiler, type_queried, loc)){
7100 ec.Report.Error (233, loc,
7101 "`{0}' does not have a predefined size, therefore sizeof can only be used in an unsafe context (consider using System.Runtime.InteropServices.Marshal.SizeOf)",
7102 TypeManager.CSharpName (type_queried));
7105 type = TypeManager.int32_type;
7106 eclass = ExprClass.Value;
7110 public override void Emit (EmitContext ec)
7112 ec.Emit (OpCodes.Sizeof, type_queried);
7115 protected override void CloneTo (CloneContext clonectx, Expression t)
7121 /// Implements the qualified-alias-member (::) expression.
7123 public class QualifiedAliasMember : MemberAccess
7125 readonly string alias;
7126 public static readonly string GlobalAlias = "global";
7128 public QualifiedAliasMember (string alias, string identifier, Location l)
7129 : base (null, identifier, l)
7134 public QualifiedAliasMember (string alias, string identifier, TypeArguments targs, Location l)
7135 : base (null, identifier, targs, l)
7140 public QualifiedAliasMember (string alias, string identifier, int arity, Location l)
7141 : base (null, identifier, arity, l)
7146 public override FullNamedExpression ResolveAsTypeStep (IMemberContext ec, bool silent)
7148 if (alias == GlobalAlias) {
7149 expr = GlobalRootNamespace.Instance;
7150 return base.ResolveAsTypeStep (ec, silent);
7153 int errors = ec.Compiler.Report.Errors;
7154 expr = ec.LookupNamespaceAlias (alias);
7156 if (errors == ec.Compiler.Report.Errors)
7157 ec.Compiler.Report.Error (432, loc, "Alias `{0}' not found", alias);
7161 FullNamedExpression fne = base.ResolveAsTypeStep (ec, silent);
7165 if (expr.eclass == ExprClass.Type) {
7167 ec.Compiler.Report.Error (431, loc,
7168 "Alias `{0}' cannot be used with '::' since it denotes a type. Consider replacing '::' with '.'", alias);
7176 protected override Expression DoResolve (ResolveContext ec)
7178 return ResolveAsTypeStep (ec, false);
7181 protected override void Error_IdentifierNotFound (IMemberContext rc, TypeSpec expr_type, string identifier)
7183 rc.Compiler.Report.Error (687, loc,
7184 "A namespace alias qualifier `{0}' did not resolve to a namespace or a type",
7185 GetSignatureForError ());
7188 public override string GetSignatureForError ()
7191 if (targs != null) {
7192 name = Name + "<" + targs.GetSignatureForError () + ">";
7195 return alias + "::" + name;
7198 protected override void CloneTo (CloneContext clonectx, Expression t)
7205 /// Implements the member access expression
7207 public class MemberAccess : ATypeNameExpression {
7208 protected Expression expr;
7210 public MemberAccess (Expression expr, string id)
7211 : base (id, expr.Location)
7216 public MemberAccess (Expression expr, string identifier, Location loc)
7217 : base (identifier, loc)
7222 public MemberAccess (Expression expr, string identifier, TypeArguments args, Location loc)
7223 : base (identifier, args, loc)
7228 public MemberAccess (Expression expr, string identifier, int arity, Location loc)
7229 : base (identifier, arity, loc)
7234 Expression DoResolve (ResolveContext ec, Expression right_side)
7237 throw new Exception ();
7240 // Resolve the expression with flow analysis turned off, we'll do the definite
7241 // assignment checks later. This is because we don't know yet what the expression
7242 // will resolve to - it may resolve to a FieldExpr and in this case we must do the
7243 // definite assignment check on the actual field and not on the whole struct.
7246 SimpleName original = expr as SimpleName;
7247 Expression expr_resolved;
7248 using (ec.Set (ResolveContext.Options.OmitStructFlowAnalysis)) {
7249 expr_resolved = expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.Type | ResolveFlags.Intermediate);
7252 if (expr_resolved == null)
7255 Namespace ns = expr_resolved as Namespace;
7257 FullNamedExpression retval = ns.Lookup (ec.Compiler, Name, Arity, loc);
7260 ns.Error_NamespaceDoesNotExist (loc, Name, Arity, ec);
7261 else if (HasTypeArguments)
7262 retval = new GenericTypeExpr (retval.Type, targs, loc).ResolveAsTypeStep (ec, false);
7267 TypeSpec expr_type = expr_resolved.Type;
7268 if (expr_type == InternalType.Dynamic) {
7269 Arguments args = new Arguments (1);
7270 args.Add (new Argument (expr_resolved.Resolve (ec)));
7271 expr = new DynamicMemberBinder (Name, args, loc);
7272 if (right_side != null)
7273 return expr.DoResolveLValue (ec, right_side);
7275 return expr.Resolve (ec);
7278 if (expr_type.IsPointer || expr_type == TypeManager.void_type ||
7279 expr_type == TypeManager.null_type || expr_type == InternalType.AnonymousMethod) {
7280 Unary.Error_OperatorCannotBeApplied (ec, loc, ".", expr_type);
7284 Constant c = expr_resolved as Constant;
7285 if (c != null && c.GetValue () == null) {
7286 ec.Report.Warning (1720, 1, loc, "Expression will always cause a `{0}'",
7287 "System.NullReferenceException");
7290 var arity = HasTypeArguments ? targs.Count : -1;
7292 var member_lookup = MemberLookup (ec.Compiler,
7293 ec.CurrentType, expr_type, expr_type, Name, arity, BindingRestriction.NoOverrides, loc);
7295 if (member_lookup == null) {
7296 ExprClass expr_eclass = expr_resolved.eclass;
7299 // Extension methods are not allowed on all expression types
7301 if (expr_eclass == ExprClass.Value || expr_eclass == ExprClass.Variable ||
7302 expr_eclass == ExprClass.IndexerAccess || expr_eclass == ExprClass.PropertyAccess ||
7303 expr_eclass == ExprClass.EventAccess) {
7304 ExtensionMethodGroupExpr ex_method_lookup = ec.LookupExtensionMethod (expr_type, Name, arity, loc);
7305 if (ex_method_lookup != null) {
7306 ex_method_lookup.ExtensionExpression = expr_resolved;
7308 if (HasTypeArguments) {
7309 if (!targs.Resolve (ec))
7312 ex_method_lookup.SetTypeArguments (ec, targs);
7315 return ex_method_lookup.Resolve (ec);
7319 expr = expr_resolved;
7320 member_lookup = Error_MemberLookupFailed (ec,
7321 ec.CurrentType, expr_type, expr_type, Name, arity, null,
7322 MemberKind.All, BindingRestriction.AccessibleOnly);
7323 if (member_lookup == null)
7327 TypeExpr texpr = member_lookup as TypeExpr;
7328 if (texpr != null) {
7329 if (!(expr_resolved is TypeExpr) &&
7330 (original == null || !original.IdenticalNameAndTypeName (ec, expr_resolved, loc))) {
7331 ec.Report.Error (572, loc, "`{0}': cannot reference a type through an expression; try `{1}' instead",
7332 Name, member_lookup.GetSignatureForError ());
7336 if (!texpr.CheckAccessLevel (ec.MemberContext)) {
7337 ec.Report.SymbolRelatedToPreviousError (member_lookup.Type);
7338 ErrorIsInaccesible (loc, TypeManager.CSharpName (member_lookup.Type), ec.Report);
7342 GenericTypeExpr ct = expr_resolved as GenericTypeExpr;
7343 if (ct != null && Arity > 0) {
7344 ct = new GenericTypeExpr (member_lookup.Type, targs, loc);
7345 return ct.ResolveAsTypeStep (ec, false);
7348 return member_lookup;
7351 MemberExpr me = (MemberExpr) member_lookup;
7352 me = me.ResolveMemberAccess (ec, expr_resolved, loc, original);
7356 if (HasTypeArguments) {
7357 if (!targs.Resolve (ec))
7360 me.SetTypeArguments (ec, targs);
7363 if (original != null && (!TypeManager.IsValueType (expr_type) || me is PropertyExpr)) {
7364 if (me.IsInstance) {
7365 LocalVariableReference var = expr_resolved as LocalVariableReference;
7366 if (var != null && !var.VerifyAssigned (ec))
7371 // The following DoResolve/DoResolveLValue will do the definite assignment
7374 if (right_side != null)
7375 return me.DoResolveLValue (ec, right_side);
7377 return me.Resolve (ec);
7380 protected override Expression DoResolve (ResolveContext ec)
7382 return DoResolve (ec, null);
7385 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
7387 return DoResolve (ec, right_side);
7390 public override FullNamedExpression ResolveAsTypeStep (IMemberContext ec, bool silent)
7392 return ResolveNamespaceOrType (ec, silent);
7395 public FullNamedExpression ResolveNamespaceOrType (IMemberContext rc, bool silent)
7397 FullNamedExpression expr_resolved = expr.ResolveAsTypeStep (rc, silent);
7399 if (expr_resolved == null)
7402 Namespace ns = expr_resolved as Namespace;
7404 FullNamedExpression retval = ns.Lookup (rc.Compiler, Name, Arity, loc);
7406 if (retval == null) {
7408 ns.Error_NamespaceDoesNotExist (loc, Name, Arity, rc);
7409 } else if (HasTypeArguments) {
7410 retval = new GenericTypeExpr (retval.Type, targs, loc).ResolveAsTypeStep (rc, silent);
7416 TypeExpr tnew_expr = expr_resolved.ResolveAsTypeTerminal (rc, false);
7417 if (tnew_expr == null)
7420 TypeSpec expr_type = tnew_expr.Type;
7421 if (TypeManager.IsGenericParameter (expr_type)) {
7422 rc.Compiler.Report.Error (704, loc, "A nested type cannot be specified through a type parameter `{0}'",
7423 tnew_expr.GetSignatureForError ());
7427 var nested = MemberCache.FindNestedType (expr_type, Name, Arity);
7428 if (nested == null) {
7432 Error_IdentifierNotFound (rc, expr_type, Name);
7437 if (!IsMemberAccessible (rc.CurrentType ?? InternalType.FakeInternalType, nested, out extra_check)) {
7438 ErrorIsInaccesible (loc, nested.GetSignatureForError (), rc.Compiler.Report);
7442 if (HasTypeArguments) {
7443 texpr = new GenericTypeExpr (nested, targs, loc);
7445 texpr = new TypeExpression (nested, loc);
7448 return texpr.ResolveAsTypeStep (rc, false);
7451 protected virtual void Error_IdentifierNotFound (IMemberContext rc, TypeSpec expr_type, string identifier)
7453 var nested = MemberCache.FindNestedType (expr_type, Name, -System.Math.Max (1, Arity));
7455 if (nested != null) {
7456 Error_TypeArgumentsCannotBeUsed (rc.Compiler.Report, expr.Location, nested, Arity);
7460 var member_lookup = MemberLookup (rc.Compiler,
7461 rc.CurrentType, expr_type, expr_type, identifier, -1,
7462 MemberKind.All, BindingRestriction.None, loc);
7464 if (member_lookup == null) {
7465 rc.Compiler.Report.Error (426, loc, "The nested type `{0}' does not exist in the type `{1}'",
7466 Name, expr_type.GetSignatureForError ());
7468 // TODO: Report.SymbolRelatedToPreviousError
7469 member_lookup.Error_UnexpectedKind (rc.Compiler.Report, null, "type", loc);
7473 protected override void Error_TypeDoesNotContainDefinition (ResolveContext ec, TypeSpec type, string name)
7475 if (RootContext.Version > LanguageVersion.ISO_2 && !ec.Compiler.IsRuntimeBinder &&
7476 ((expr.eclass & (ExprClass.Value | ExprClass.Variable)) != 0)) {
7477 ec.Report.Error (1061, loc, "Type `{0}' does not contain a definition for `{1}' and no " +
7478 "extension method `{1}' of type `{0}' could be found " +
7479 "(are you missing a using directive or an assembly reference?)",
7480 TypeManager.CSharpName (type), name);
7484 base.Error_TypeDoesNotContainDefinition (ec, type, name);
7487 public override string GetSignatureForError ()
7489 return expr.GetSignatureForError () + "." + base.GetSignatureForError ();
7492 public Expression Left {
7498 protected override void CloneTo (CloneContext clonectx, Expression t)
7500 MemberAccess target = (MemberAccess) t;
7502 target.expr = expr.Clone (clonectx);
7507 /// Implements checked expressions
7509 public class CheckedExpr : Expression {
7511 public Expression Expr;
7513 public CheckedExpr (Expression e, Location l)
7519 public override Expression CreateExpressionTree (ResolveContext ec)
7521 using (ec.With (ResolveContext.Options.AllCheckStateFlags, true))
7522 return Expr.CreateExpressionTree (ec);
7525 protected override Expression DoResolve (ResolveContext ec)
7527 using (ec.With (ResolveContext.Options.AllCheckStateFlags, true))
7528 Expr = Expr.Resolve (ec);
7533 if (Expr is Constant || Expr is MethodGroupExpr || Expr is AnonymousMethodExpression || Expr is DefaultValueExpression)
7536 eclass = Expr.eclass;
7541 public override void Emit (EmitContext ec)
7543 using (ec.With (EmitContext.Options.AllCheckStateFlags, true))
7547 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
7549 using (ec.With (EmitContext.Options.AllCheckStateFlags, true))
7550 Expr.EmitBranchable (ec, target, on_true);
7553 public override SLE.Expression MakeExpression (BuilderContext ctx)
7555 using (ctx.With (BuilderContext.Options.AllCheckStateFlags, true)) {
7556 return Expr.MakeExpression (ctx);
7560 protected override void CloneTo (CloneContext clonectx, Expression t)
7562 CheckedExpr target = (CheckedExpr) t;
7564 target.Expr = Expr.Clone (clonectx);
7569 /// Implements the unchecked expression
7571 public class UnCheckedExpr : Expression {
7573 public Expression Expr;
7575 public UnCheckedExpr (Expression e, Location l)
7581 public override Expression CreateExpressionTree (ResolveContext ec)
7583 using (ec.With (ResolveContext.Options.AllCheckStateFlags, false))
7584 return Expr.CreateExpressionTree (ec);
7587 protected override Expression DoResolve (ResolveContext ec)
7589 using (ec.With (ResolveContext.Options.AllCheckStateFlags, false))
7590 Expr = Expr.Resolve (ec);
7595 if (Expr is Constant || Expr is MethodGroupExpr || Expr is AnonymousMethodExpression || Expr is DefaultValueExpression)
7598 eclass = Expr.eclass;
7603 public override void Emit (EmitContext ec)
7605 using (ec.With (EmitContext.Options.AllCheckStateFlags, false))
7609 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
7611 using (ec.With (EmitContext.Options.AllCheckStateFlags, false))
7612 Expr.EmitBranchable (ec, target, on_true);
7615 protected override void CloneTo (CloneContext clonectx, Expression t)
7617 UnCheckedExpr target = (UnCheckedExpr) t;
7619 target.Expr = Expr.Clone (clonectx);
7624 /// An Element Access expression.
7626 /// During semantic analysis these are transformed into
7627 /// IndexerAccess, ArrayAccess or a PointerArithmetic.
7629 public class ElementAccess : Expression {
7630 public Arguments Arguments;
7631 public Expression Expr;
7633 public ElementAccess (Expression e, Arguments args)
7637 this.Arguments = args;
7640 public override Expression CreateExpressionTree (ResolveContext ec)
7642 Arguments args = Arguments.CreateForExpressionTree (ec, Arguments,
7643 Expr.CreateExpressionTree (ec));
7645 return CreateExpressionFactoryCall (ec, "ArrayIndex", args);
7648 Expression MakePointerAccess (ResolveContext ec, TypeSpec t)
7650 if (Arguments.Count != 1){
7651 ec.Report.Error (196, loc, "A pointer must be indexed by only one value");
7655 if (Arguments [0] is NamedArgument)
7656 Error_NamedArgument ((NamedArgument) Arguments[0], ec.Report);
7658 Expression p = new PointerArithmetic (Binary.Operator.Addition, Expr, Arguments [0].Expr.Resolve (ec), t, loc);
7659 return new Indirection (p, loc).Resolve (ec);
7662 protected override Expression DoResolve (ResolveContext ec)
7664 Expr = Expr.Resolve (ec);
7669 // We perform some simple tests, and then to "split" the emit and store
7670 // code we create an instance of a different class, and return that.
7672 // I am experimenting with this pattern.
7674 TypeSpec t = Expr.Type;
7676 if (t == TypeManager.array_type){
7677 ec.Report.Error (21, loc, "Cannot apply indexing with [] to an expression of type `System.Array'");
7682 return (new ArrayAccess (this, loc)).Resolve (ec);
7684 return MakePointerAccess (ec, t);
7686 FieldExpr fe = Expr as FieldExpr;
7688 var ff = fe.Spec as FixedFieldSpec;
7690 return MakePointerAccess (ec, ff.ElementType);
7693 return (new IndexerAccess (this, loc)).Resolve (ec);
7696 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
7698 Expr = Expr.Resolve (ec);
7704 return (new ArrayAccess (this, loc)).DoResolveLValue (ec, right_side);
7707 return MakePointerAccess (ec, type);
7709 if (Expr.eclass != ExprClass.Variable && TypeManager.IsStruct (type))
7710 Error_CannotModifyIntermediateExpressionValue (ec);
7712 return (new IndexerAccess (this, loc)).DoResolveLValue (ec, right_side);
7715 public override void Emit (EmitContext ec)
7717 throw new Exception ("Should never be reached");
7720 public static void Error_NamedArgument (NamedArgument na, Report Report)
7722 Report.Error (1742, na.Location, "An element access expression cannot use named argument");
7725 public override string GetSignatureForError ()
7727 return Expr.GetSignatureForError ();
7730 protected override void CloneTo (CloneContext clonectx, Expression t)
7732 ElementAccess target = (ElementAccess) t;
7734 target.Expr = Expr.Clone (clonectx);
7735 if (Arguments != null)
7736 target.Arguments = Arguments.Clone (clonectx);
7741 /// Implements array access
7743 public class ArrayAccess : Expression, IDynamicAssign, IMemoryLocation {
7745 // Points to our "data" repository
7749 LocalTemporary temp;
7753 public ArrayAccess (ElementAccess ea_data, Location l)
7759 public override Expression CreateExpressionTree (ResolveContext ec)
7761 return ea.CreateExpressionTree (ec);
7764 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
7766 return DoResolve (ec);
7769 protected override Expression DoResolve (ResolveContext ec)
7771 // dynamic is used per argument in ConvertExpressionToArrayIndex case
7773 ea.Arguments.Resolve (ec, out dynamic);
7775 TypeSpec t = ea.Expr.Type;
7776 int rank = ea.Arguments.Count;
7777 if (t.GetMetaInfo ().GetArrayRank () != rank) {
7778 ec.Report.Error (22, ea.Location, "Wrong number of indexes `{0}' inside [], expected `{1}'",
7779 ea.Arguments.Count.ToString (), t.GetMetaInfo ().GetArrayRank ().ToString ());
7783 type = TypeManager.GetElementType (t);
7784 if (type.IsPointer && !ec.IsUnsafe) {
7785 UnsafeError (ec, ea.Location);
7788 foreach (Argument a in ea.Arguments) {
7789 if (a is NamedArgument)
7790 ElementAccess.Error_NamedArgument ((NamedArgument) a, ec.Report);
7792 a.Expr = ConvertExpressionToArrayIndex (ec, a.Expr);
7795 eclass = ExprClass.Variable;
7800 protected override void Error_NegativeArrayIndex (ResolveContext ec, Location loc)
7802 ec.Report.Warning (251, 2, loc, "Indexing an array with a negative index (array indices always start at zero)");
7805 MethodInfo FetchAddressMethod ()
7807 ModuleBuilder mb = RootContext.ToplevelTypes.Builder;
7808 int arg_count = ea.Arguments.Count;
7809 var args = new Type [arg_count];
7812 var ret_type = TypeManager.GetReferenceType (type);
7814 for (int i = 0; i < arg_count; i++){
7815 //args [i++] = a.Type;
7816 args[i] = TypeManager.int32_type.GetMetaInfo ();
7819 address = mb.GetArrayMethod (
7820 ea.Expr.Type.GetMetaInfo (), "Address",
7821 CallingConventions.HasThis |
7822 CallingConventions.Standard,
7829 // Load the array arguments into the stack.
7831 void LoadArrayAndArguments (EmitContext ec)
7835 for (int i = 0; i < ea.Arguments.Count; ++i) {
7836 ea.Arguments [i].Emit (ec);
7840 public void Emit (EmitContext ec, bool leave_copy)
7842 var ac = ea.Expr.Type as ArrayContainer;
7845 ec.EmitLoadFromPtr (type);
7847 LoadArrayAndArguments (ec);
7848 ec.EmitArrayLoad (ac);
7852 ec.Emit (OpCodes.Dup);
7853 temp = new LocalTemporary (this.type);
7858 public override void Emit (EmitContext ec)
7863 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
7865 var ac = (ArrayContainer) ea.Expr.Type;
7866 TypeSpec t = source.Type;
7867 prepared = prepare_for_load;
7870 AddressOf (ec, AddressOp.LoadStore);
7871 ec.Emit (OpCodes.Dup);
7873 LoadArrayAndArguments (ec);
7876 // If we are dealing with a struct, get the
7877 // address of it, so we can store it.
7879 // The stobj opcode used by value types will need
7880 // an address on the stack, not really an array/array
7883 if (ac.Rank == 1 && TypeManager.IsStruct (t) &&
7884 (!TypeManager.IsBuiltinOrEnum (t) ||
7885 t == TypeManager.decimal_type)) {
7887 ec.Emit (OpCodes.Ldelema, t);
7893 ec.Emit (OpCodes.Dup);
7894 temp = new LocalTemporary (this.type);
7899 ec.EmitStoreFromPtr (t);
7901 ec.EmitArrayStore (ac);
7910 public void EmitNew (EmitContext ec, New source, bool leave_copy)
7912 if (!source.Emit (ec, this)) {
7914 throw new NotImplementedException ();
7919 throw new NotImplementedException ();
7922 public void AddressOf (EmitContext ec, AddressOp mode)
7924 int rank = ea.Expr.Type.GetMetaInfo ().GetArrayRank ();
7926 LoadArrayAndArguments (ec);
7929 ec.Emit (OpCodes.Ldelema, type);
7931 MethodInfo address = FetchAddressMethod ();
7932 ec.Emit (OpCodes.Call, address);
7937 public SLE.Expression MakeAssignExpression (BuilderContext ctx)
7939 return SLE.Expression.ArrayAccess (
7940 ea.Expr.MakeExpression (ctx),
7941 Arguments.MakeExpression (ea.Arguments, ctx));
7945 public override SLE.Expression MakeExpression (BuilderContext ctx)
7947 return SLE.Expression.ArrayIndex (
7948 ea.Expr.MakeExpression (ctx),
7949 Arguments.MakeExpression (ea.Arguments, ctx));
7954 /// Expressions that represent an indexer call.
7956 public class IndexerAccess : Expression, IDynamicAssign
7958 class IndexerMethodGroupExpr : MethodGroupExpr
7960 IEnumerable<IndexerSpec> candidates;
7962 public IndexerMethodGroupExpr (IEnumerable<IndexerSpec> indexers, Location loc)
7963 : base (FilterAccessors (indexers).ToList (), null, loc)
7965 candidates = indexers;
7968 public IndexerSpec BestIndexer ()
7970 return candidates.Where (l => l.Get == BestCandidate || l.Set == BestCandidate).First ();
7973 static IEnumerable<MemberSpec> FilterAccessors (IEnumerable<IndexerSpec> indexers)
7975 foreach (IndexerSpec i in indexers) {
7983 protected override IList<MemberSpec> GetBaseTypeMethods (ResolveContext rc, TypeSpec type)
7985 candidates = GetIndexersForType (type);
7986 if (candidates == null)
7989 return FilterAccessors (candidates).ToList ();
7992 public override string Name {
7998 protected override int GetApplicableParametersCount (MethodSpec method, AParametersCollection parameters)
8001 // Here is the trick, decrease number of arguments by 1 when only
8002 // available property method is setter. This makes overload resolution
8003 // work correctly for indexers.
8006 if (method.Name [0] == 'g')
8007 return parameters.Count;
8009 return parameters.Count - 1;
8014 // Points to our "data" repository
8017 bool is_base_indexer;
8019 LocalTemporary temp;
8020 LocalTemporary prepared_value;
8021 Expression set_expr;
8023 protected TypeSpec indexer_type;
8024 protected TypeSpec current_type;
8025 protected Expression instance_expr;
8026 protected Arguments arguments;
8028 public IndexerAccess (ElementAccess ea, Location loc)
8029 : this (ea.Expr, false, loc)
8031 this.arguments = ea.Arguments;
8034 protected IndexerAccess (Expression instance_expr, bool is_base_indexer,
8037 this.instance_expr = instance_expr;
8038 this.is_base_indexer = is_base_indexer;
8042 static string GetAccessorName (bool isSet)
8044 return isSet ? "set" : "get";
8047 public override Expression CreateExpressionTree (ResolveContext ec)
8049 Arguments args = Arguments.CreateForExpressionTree (ec, arguments,
8050 instance_expr.CreateExpressionTree (ec),
8051 new TypeOfMethod (spec.Get, loc));
8053 return CreateExpressionFactoryCall (ec, "Call", args);
8056 static IEnumerable<IndexerSpec> GetIndexersForType (TypeSpec lookup_type)
8058 return MemberCache.FindIndexers (lookup_type, BindingRestriction.AccessibleOnly | BindingRestriction.NoOverrides);
8061 protected virtual void CommonResolve (ResolveContext ec)
8063 indexer_type = instance_expr.Type;
8064 current_type = ec.CurrentType;
8067 protected override Expression DoResolve (ResolveContext ec)
8069 return ResolveAccessor (ec, null);
8072 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
8074 if (right_side == EmptyExpression.OutAccess.Instance) {
8075 right_side.DoResolveLValue (ec, this);
8079 // if the indexer returns a value type, and we try to set a field in it
8080 if (right_side == EmptyExpression.LValueMemberAccess || right_side == EmptyExpression.LValueMemberOutAccess) {
8081 Error_CannotModifyIntermediateExpressionValue (ec);
8084 return ResolveAccessor (ec, right_side);
8087 Expression ResolveAccessor (ResolveContext ec, Expression right_side)
8093 arguments.Resolve (ec, out dynamic);
8095 if (indexer_type == InternalType.Dynamic) {
8098 var ilist = GetIndexersForType (/*current_type,*/ indexer_type);
8099 if (ilist == null) {
8100 ec.Report.Error (21, loc, "Cannot apply indexing with [] to an expression of type `{0}'",
8101 TypeManager.CSharpName (indexer_type));
8105 var mg = new IndexerMethodGroupExpr (ilist, loc);
8106 mg = mg.OverloadResolve (ec, ref arguments, false, loc) as IndexerMethodGroupExpr;
8111 spec = mg.BestIndexer ();
8115 Arguments args = new Arguments (arguments.Count + 1);
8116 if (is_base_indexer) {
8117 ec.Report.Error (1972, loc, "The indexer base access cannot be dynamically dispatched. Consider casting the dynamic arguments or eliminating the base access");
8119 args.Add (new Argument (instance_expr));
8121 args.AddRange (arguments);
8123 var expr = new DynamicIndexBinder (args, loc);
8124 if (right_side != null)
8125 return expr.ResolveLValue (ec, right_side);
8127 return expr.Resolve (ec);
8130 type = spec.MemberType;
8131 if (type.IsPointer && !ec.IsUnsafe)
8132 UnsafeError (ec, loc);
8134 MethodSpec accessor;
8135 if (right_side == null) {
8136 accessor = spec.Get;
8138 accessor = spec.Set;
8139 if (!spec.HasSet && spec.HasGet) {
8140 ec.Report.SymbolRelatedToPreviousError (spec);
8141 ec.Report.Error (200, loc, "The read only property or indexer `{0}' cannot be assigned to",
8142 spec.GetSignatureForError ());
8146 set_expr = Convert.ImplicitConversion (ec, right_side, type, loc);
8149 if (accessor == null || accessor.Kind == MemberKind.FakeMethod) {
8150 ec.Report.SymbolRelatedToPreviousError (spec);
8151 ec.Report.Error (154, loc, "The property or indexer `{0}' cannot be used in this context because it lacks a `{1}' accessor",
8152 spec.GetSignatureForError (), GetAccessorName (right_side != null));
8157 // Only base will allow this invocation to happen.
8159 if (spec.IsAbstract && this is BaseIndexerAccess) {
8160 Error_CannotCallAbstractBase (ec, spec.GetSignatureForError ());
8163 bool must_do_cs1540_check;
8164 if (!IsMemberAccessible (ec.CurrentType, accessor, out must_do_cs1540_check)) {
8165 if (spec.HasDifferentAccessibility) {
8166 ec.Report.SymbolRelatedToPreviousError (accessor);
8167 ec.Report.Error (271, loc, "The property or indexer `{0}' cannot be used in this context because a `{1}' accessor is inaccessible",
8168 TypeManager.GetFullNameSignature (spec), GetAccessorName (right_side != null));
8170 ec.Report.SymbolRelatedToPreviousError (spec);
8171 ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (spec), ec.Report);
8175 instance_expr.CheckMarshalByRefAccess (ec);
8177 if (must_do_cs1540_check && (instance_expr != EmptyExpression.Null) &&
8178 !TypeManager.IsInstantiationOfSameGenericType (instance_expr.Type, ec.CurrentType) &&
8179 !TypeManager.IsNestedChildOf (ec.CurrentType, instance_expr.Type) &&
8180 !TypeManager.IsSubclassOf (instance_expr.Type, ec.CurrentType)) {
8181 ec.Report.SymbolRelatedToPreviousError (accessor);
8182 Error_CannotAccessProtected (ec, loc, spec, instance_expr.Type, ec.CurrentType);
8186 eclass = ExprClass.IndexerAccess;
8190 public override void Emit (EmitContext ec)
8195 public void Emit (EmitContext ec, bool leave_copy)
8198 prepared_value.Emit (ec);
8200 Invocation.EmitCall (ec, is_base_indexer, instance_expr, spec.Get,
8201 arguments, loc, false, false);
8205 ec.Emit (OpCodes.Dup);
8206 temp = new LocalTemporary (Type);
8212 // source is ignored, because we already have a copy of it from the
8213 // LValue resolution and we have already constructed a pre-cached
8214 // version of the arguments (ea.set_arguments);
8216 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
8218 prepared = prepare_for_load;
8219 Expression value = set_expr;
8222 Invocation.EmitCall (ec, is_base_indexer, instance_expr, spec.Get,
8223 arguments, loc, true, false);
8225 prepared_value = new LocalTemporary (type);
8226 prepared_value.Store (ec);
8228 prepared_value.Release (ec);
8231 ec.Emit (OpCodes.Dup);
8232 temp = new LocalTemporary (Type);
8235 } else if (leave_copy) {
8236 temp = new LocalTemporary (Type);
8243 arguments.Add (new Argument (value));
8245 Invocation.EmitCall (ec, is_base_indexer, instance_expr, spec.Set, arguments, loc, false, prepared);
8253 public override string GetSignatureForError ()
8255 return spec.GetSignatureForError ();
8259 public SLE.Expression MakeAssignExpression (BuilderContext ctx)
8261 var value = new[] { set_expr.MakeExpression (ctx) };
8262 var args = Arguments.MakeExpression (arguments, ctx).Concat (value);
8264 return SLE.Expression.Block (
8265 SLE.Expression.Call (instance_expr.MakeExpression (ctx), (MethodInfo) spec.Set.GetMetaInfo (), args),
8270 public override SLE.Expression MakeExpression (BuilderContext ctx)
8272 var args = Arguments.MakeExpression (arguments, ctx);
8273 return SLE.Expression.Call (instance_expr.MakeExpression (ctx), (MethodInfo) spec.Get.GetMetaInfo (), args);
8276 protected override void CloneTo (CloneContext clonectx, Expression t)
8278 IndexerAccess target = (IndexerAccess) t;
8280 if (arguments != null)
8281 target.arguments = arguments.Clone (clonectx);
8283 if (instance_expr != null)
8284 target.instance_expr = instance_expr.Clone (clonectx);
8289 /// The base operator for method names
8291 public class BaseAccess : Expression {
8292 public readonly string Identifier;
8295 public BaseAccess (string member, Location l)
8297 this.Identifier = member;
8301 public BaseAccess (string member, TypeArguments args, Location l)
8307 public override Expression CreateExpressionTree (ResolveContext ec)
8309 throw new NotSupportedException ("ET");
8312 protected override Expression DoResolve (ResolveContext ec)
8314 Expression c = CommonResolve (ec);
8320 // MethodGroups use this opportunity to flag an error on lacking ()
8322 if (!(c is MethodGroupExpr))
8323 return c.Resolve (ec);
8327 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
8329 Expression c = CommonResolve (ec);
8335 // MethodGroups use this opportunity to flag an error on lacking ()
8337 if (! (c is MethodGroupExpr))
8338 return c.DoResolveLValue (ec, right_side);
8343 Expression CommonResolve (ResolveContext ec)
8345 Expression member_lookup;
8346 TypeSpec current_type = ec.CurrentType;
8347 TypeSpec base_type = current_type.BaseType;
8349 if (!This.IsThisAvailable (ec)) {
8351 ec.Report.Error (1511, loc, "Keyword `base' is not available in a static method");
8353 ec.Report.Error (1512, loc, "Keyword `base' is not available in the current context");
8358 var arity = args == null ? -1 : args.Count;
8359 member_lookup = MemberLookup (ec.Compiler, ec.CurrentType, null, base_type, Identifier, arity,
8360 MemberKind.All, BindingRestriction.AccessibleOnly, loc);
8361 if (member_lookup == null) {
8362 Error_MemberLookupFailed (ec, ec.CurrentType, base_type, base_type, Identifier, arity,
8363 null, MemberKind.All, BindingRestriction.AccessibleOnly);
8370 left = new TypeExpression (base_type, loc);
8372 left = ec.GetThis (loc);
8374 MemberExpr me = member_lookup as MemberExpr;
8376 if (member_lookup is TypeExpression){
8377 ec.Report.Error (582, loc, "{0}: Can not reference a type through an expression, try `{1}' instead",
8378 Identifier, member_lookup.GetSignatureForError ());
8380 ec.Report.Error (582, loc, "{0}: Can not reference a {1} through an expression",
8381 Identifier, member_lookup.ExprClassName);
8387 me = me.ResolveMemberAccess (ec, left, loc, null);
8394 me.SetTypeArguments (ec, args);
8400 public override void Emit (EmitContext ec)
8402 throw new Exception ("Should never be called");
8405 protected override void CloneTo (CloneContext clonectx, Expression t)
8407 BaseAccess target = (BaseAccess) t;
8410 target.args = args.Clone ();
8415 /// The base indexer operator
8417 public class BaseIndexerAccess : IndexerAccess {
8418 public BaseIndexerAccess (Arguments args, Location loc)
8419 : base (null, true, loc)
8421 this.arguments = args;
8424 protected override void CommonResolve (ResolveContext ec)
8426 instance_expr = ec.GetThis (loc);
8428 current_type = ec.CurrentType.BaseType;
8429 indexer_type = current_type;
8432 public override Expression CreateExpressionTree (ResolveContext ec)
8434 MemberExpr.Error_BaseAccessInExpressionTree (ec, loc);
8435 return base.CreateExpressionTree (ec);
8440 /// This class exists solely to pass the Type around and to be a dummy
8441 /// that can be passed to the conversion functions (this is used by
8442 /// foreach implementation to typecast the object return value from
8443 /// get_Current into the proper type. All code has been generated and
8444 /// we only care about the side effect conversions to be performed
8446 /// This is also now used as a placeholder where a no-action expression
8447 /// is needed (the `New' class).
8449 public class EmptyExpression : Expression {
8450 public static readonly Expression Null = new EmptyExpression ();
8452 public class OutAccess : EmptyExpression
8454 public static readonly OutAccess Instance = new OutAccess ();
8456 public override Expression DoResolveLValue (ResolveContext rc, Expression right_side)
8458 rc.Report.Error (206, right_side.Location,
8459 "A property, indexer or dynamic member access may not be passed as `ref' or `out' parameter");
8465 public static readonly EmptyExpression LValueMemberAccess = new EmptyExpression ();
8466 public static readonly EmptyExpression LValueMemberOutAccess = new EmptyExpression ();
8467 public static readonly EmptyExpression UnaryAddress = new EmptyExpression ();
8469 static EmptyExpression temp = new EmptyExpression ();
8470 public static EmptyExpression Grab ()
8472 EmptyExpression retval = temp == null ? new EmptyExpression () : temp;
8477 public static void Release (EmptyExpression e)
8484 // FIXME: Don't set to object
8485 type = TypeManager.object_type;
8486 eclass = ExprClass.Value;
8487 loc = Location.Null;
8490 public EmptyExpression (TypeSpec t)
8493 eclass = ExprClass.Value;
8494 loc = Location.Null;
8497 public override Expression CreateExpressionTree (ResolveContext ec)
8499 throw new NotSupportedException ("ET");
8502 protected override Expression DoResolve (ResolveContext ec)
8507 public override void Emit (EmitContext ec)
8509 // nothing, as we only exist to not do anything.
8512 public override void EmitSideEffect (EmitContext ec)
8517 // This is just because we might want to reuse this bad boy
8518 // instead of creating gazillions of EmptyExpressions.
8519 // (CanImplicitConversion uses it)
8521 public void SetType (TypeSpec t)
8528 // Empty statement expression
8530 public sealed class EmptyExpressionStatement : ExpressionStatement
8532 public static readonly EmptyExpressionStatement Instance = new EmptyExpressionStatement ();
8534 private EmptyExpressionStatement ()
8536 loc = Location.Null;
8539 public override Expression CreateExpressionTree (ResolveContext ec)
8544 public override void EmitStatement (EmitContext ec)
8549 protected override Expression DoResolve (ResolveContext ec)
8551 eclass = ExprClass.Value;
8552 type = TypeManager.object_type;
8556 public override void Emit (EmitContext ec)
8562 public class UserCast : Expression {
8566 public UserCast (MethodSpec method, Expression source, Location l)
8568 this.method = method;
8569 this.source = source;
8570 type = method.ReturnType;
8574 public Expression Source {
8580 public override Expression CreateExpressionTree (ResolveContext ec)
8582 Arguments args = new Arguments (3);
8583 args.Add (new Argument (source.CreateExpressionTree (ec)));
8584 args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
8585 args.Add (new Argument (new TypeOfMethod (method, loc)));
8586 return CreateExpressionFactoryCall (ec, "Convert", args);
8589 protected override Expression DoResolve (ResolveContext ec)
8591 ObsoleteAttribute oa = method.GetAttributeObsolete ();
8593 AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc, ec.Report);
8595 eclass = ExprClass.Value;
8599 public override void Emit (EmitContext ec)
8602 ec.Emit (OpCodes.Call, method);
8605 public override string GetSignatureForError ()
8607 return TypeManager.CSharpSignature (method);
8610 public override SLE.Expression MakeExpression (BuilderContext ctx)
8612 return SLE.Expression.Convert (source.MakeExpression (ctx), type.GetMetaInfo (), (MethodInfo) method.GetMetaInfo ());
8617 // This class is used to "construct" the type during a typecast
8618 // operation. Since the Type.GetType class in .NET can parse
8619 // the type specification, we just use this to construct the type
8620 // one bit at a time.
8622 public class ComposedCast : TypeExpr {
8623 FullNamedExpression left;
8626 public ComposedCast (FullNamedExpression left, string dim)
8627 : this (left, dim, left.Location)
8631 public ComposedCast (FullNamedExpression left, string dim, Location l)
8638 protected override TypeExpr DoResolveAsTypeStep (IMemberContext ec)
8640 TypeExpr lexpr = left.ResolveAsTypeTerminal (ec, false);
8644 TypeSpec ltype = lexpr.Type;
8645 if ((dim.Length > 0) && (dim [0] == '?')) {
8646 TypeExpr nullable = new Nullable.NullableType (lexpr, loc);
8648 nullable = new ComposedCast (nullable, dim.Substring (1), loc);
8649 return nullable.ResolveAsTypeTerminal (ec, false);
8652 if (dim == "*" && !TypeManager.VerifyUnmanaged (ec.Compiler, ltype, loc))
8655 if (dim.Length != 0 && dim [0] == '[') {
8656 if (TypeManager.IsSpecialType (ltype)) {
8657 ec.Compiler.Report.Error (611, loc, "Array elements cannot be of type `{0}'", TypeManager.CSharpName (ltype));
8661 if (ltype.IsStatic) {
8662 ec.Compiler.Report.SymbolRelatedToPreviousError (ltype);
8663 ec.Compiler.Report.Error (719, loc, "Array elements cannot be of static type `{0}'",
8664 TypeManager.CSharpName (ltype));
8669 type = TypeManager.GetConstructedType (ltype, dim);
8674 throw new InternalErrorException ("Couldn't create computed type " + ltype + dim);
8676 if (type.IsPointer && !ec.IsUnsafe){
8677 UnsafeError (ec.Compiler.Report, loc);
8680 eclass = ExprClass.Type;
8684 public override string GetSignatureForError ()
8686 return left.GetSignatureForError () + dim;
8690 public class FixedBufferPtr : Expression {
8693 public FixedBufferPtr (Expression array, TypeSpec array_type, Location l)
8698 type = PointerContainer.MakeType (array_type);
8699 eclass = ExprClass.Value;
8702 public override Expression CreateExpressionTree (ResolveContext ec)
8704 Error_PointerInsideExpressionTree (ec);
8708 public override void Emit(EmitContext ec)
8713 protected override Expression DoResolve (ResolveContext ec)
8716 // We are born fully resolved
8724 // This class is used to represent the address of an array, used
8725 // only by the Fixed statement, this generates "&a [0]" construct
8726 // for fixed (char *pa = a)
8728 public class ArrayPtr : FixedBufferPtr {
8729 TypeSpec array_type;
8731 public ArrayPtr (Expression array, TypeSpec array_type, Location l):
8732 base (array, array_type, l)
8734 this.array_type = array_type;
8737 public override void Emit (EmitContext ec)
8742 ec.Emit (OpCodes.Ldelema, array_type);
8747 // Encapsulates a conversion rules required for array indexes
8749 public class ArrayIndexCast : TypeCast
8751 public ArrayIndexCast (Expression expr)
8752 : base (expr, TypeManager.int32_type)
8754 if (expr.Type == TypeManager.int32_type)
8755 throw new ArgumentException ("unnecessary array index conversion");
8758 public override Expression CreateExpressionTree (ResolveContext ec)
8760 using (ec.Set (ResolveContext.Options.CheckedScope)) {
8761 return base.CreateExpressionTree (ec);
8765 public override void Emit (EmitContext ec)
8769 var expr_type = child.Type;
8771 if (expr_type == TypeManager.uint32_type)
8772 ec.Emit (OpCodes.Conv_U);
8773 else if (expr_type == TypeManager.int64_type)
8774 ec.Emit (OpCodes.Conv_Ovf_I);
8775 else if (expr_type == TypeManager.uint64_type)
8776 ec.Emit (OpCodes.Conv_Ovf_I_Un);
8778 throw new InternalErrorException ("Cannot emit cast to unknown array element type", type);
8783 // Implements the `stackalloc' keyword
8785 public class StackAlloc : Expression {
8790 public StackAlloc (Expression type, Expression count, Location l)
8797 public override Expression CreateExpressionTree (ResolveContext ec)
8799 throw new NotSupportedException ("ET");
8802 protected override Expression DoResolve (ResolveContext ec)
8804 count = count.Resolve (ec);
8808 if (count.Type != TypeManager.uint32_type){
8809 count = Convert.ImplicitConversionRequired (ec, count, TypeManager.int32_type, loc);
8814 Constant c = count as Constant;
8815 if (c != null && c.IsNegative) {
8816 ec.Report.Error (247, loc, "Cannot use a negative size with stackalloc");
8819 if (ec.HasAny (ResolveContext.Options.CatchScope | ResolveContext.Options.FinallyScope)) {
8820 ec.Report.Error (255, loc, "Cannot use stackalloc in finally or catch");
8823 TypeExpr texpr = t.ResolveAsTypeTerminal (ec, false);
8829 if (!TypeManager.VerifyUnmanaged (ec.Compiler, otype, loc))
8832 type = PointerContainer.MakeType (otype);
8833 eclass = ExprClass.Value;
8838 public override void Emit (EmitContext ec)
8840 int size = GetTypeSize (otype);
8845 ec.Emit (OpCodes.Sizeof, otype);
8849 ec.Emit (OpCodes.Mul_Ovf_Un);
8850 ec.Emit (OpCodes.Localloc);
8853 protected override void CloneTo (CloneContext clonectx, Expression t)
8855 StackAlloc target = (StackAlloc) t;
8856 target.count = count.Clone (clonectx);
8857 target.t = t.Clone (clonectx);
8862 // An object initializer expression
8864 public class ElementInitializer : Assign
8866 public readonly string Name;
8868 public ElementInitializer (string name, Expression initializer, Location loc)
8869 : base (null, initializer, loc)
8874 protected override void CloneTo (CloneContext clonectx, Expression t)
8876 ElementInitializer target = (ElementInitializer) t;
8877 target.source = source.Clone (clonectx);
8880 public override Expression CreateExpressionTree (ResolveContext ec)
8882 Arguments args = new Arguments (2);
8883 FieldExpr fe = target as FieldExpr;
8885 args.Add (new Argument (fe.CreateTypeOfExpression ()));
8887 args.Add (new Argument (((PropertyExpr)target).CreateSetterTypeOfExpression ()));
8889 args.Add (new Argument (source.CreateExpressionTree (ec)));
8890 return CreateExpressionFactoryCall (ec,
8891 source is CollectionOrObjectInitializers ? "ListBind" : "Bind",
8895 protected override Expression DoResolve (ResolveContext ec)
8898 return EmptyExpressionStatement.Instance;
8900 MemberExpr me = MemberLookupFinal (ec, ec.CurrentInitializerVariable.Type, ec.CurrentInitializerVariable.Type,
8901 Name, 0, MemberKind.Field | MemberKind.Property, BindingRestriction.AccessibleOnly | BindingRestriction.InstanceOnly, loc) as MemberExpr;
8907 me.InstanceExpression = ec.CurrentInitializerVariable;
8909 if (source is CollectionOrObjectInitializers) {
8910 Expression previous = ec.CurrentInitializerVariable;
8911 ec.CurrentInitializerVariable = target;
8912 source = source.Resolve (ec);
8913 ec.CurrentInitializerVariable = previous;
8917 eclass = source.eclass;
8922 Expression expr = base.DoResolve (ec);
8927 // Ignore field initializers with default value
8929 Constant c = source as Constant;
8930 if (c != null && c.IsDefaultInitializer (type) && target.eclass == ExprClass.Variable)
8931 return EmptyExpressionStatement.Instance.Resolve (ec);
8936 protected override MemberExpr Error_MemberLookupFailed (ResolveContext ec, TypeSpec type, IList<MemberSpec> members)
8938 var member = members.First ();
8939 if (member.Kind != MemberKind.Property && member.Kind != MemberKind.Field)
8940 ec.Report.Error (1913, loc, "Member `{0}' cannot be initialized. An object " +
8941 "initializer may only be used for fields, or properties", TypeManager.GetFullNameSignature (member));
8943 ec.Report.Error (1914, loc, " Static field or property `{0}' cannot be assigned in an object initializer",
8944 TypeManager.GetFullNameSignature (member));
8949 public override void EmitStatement (EmitContext ec)
8951 if (source is CollectionOrObjectInitializers)
8954 base.EmitStatement (ec);
8959 // A collection initializer expression
8961 class CollectionElementInitializer : Invocation
8963 public class ElementInitializerArgument : Argument
8965 public ElementInitializerArgument (Expression e)
8971 sealed class AddMemberAccess : MemberAccess
8973 public AddMemberAccess (Expression expr, Location loc)
8974 : base (expr, "Add", loc)
8978 protected override void Error_TypeDoesNotContainDefinition (ResolveContext ec, TypeSpec type, string name)
8980 if (TypeManager.HasElementType (type))
8983 base.Error_TypeDoesNotContainDefinition (ec, type, name);
8987 public CollectionElementInitializer (Expression argument)
8988 : base (null, new Arguments (1))
8990 base.arguments.Add (new ElementInitializerArgument (argument));
8991 this.loc = argument.Location;
8994 public CollectionElementInitializer (List<Expression> arguments, Location loc)
8995 : base (null, new Arguments (arguments.Count))
8997 foreach (Expression e in arguments)
8998 base.arguments.Add (new ElementInitializerArgument (e));
9003 public override Expression CreateExpressionTree (ResolveContext ec)
9005 Arguments args = new Arguments (2);
9006 args.Add (new Argument (mg.CreateExpressionTree (ec)));
9008 var expr_initializers = new ArrayInitializer (arguments.Count, loc);
9009 foreach (Argument a in arguments)
9010 expr_initializers.Add (a.CreateExpressionTree (ec));
9012 args.Add (new Argument (new ArrayCreation (
9013 CreateExpressionTypeExpression (ec, loc), "[]", expr_initializers, loc)));
9014 return CreateExpressionFactoryCall (ec, "ElementInit", args);
9017 protected override void CloneTo (CloneContext clonectx, Expression t)
9019 CollectionElementInitializer target = (CollectionElementInitializer) t;
9020 if (arguments != null)
9021 target.arguments = arguments.Clone (clonectx);
9024 protected override Expression DoResolve (ResolveContext ec)
9026 base.expr = new AddMemberAccess (ec.CurrentInitializerVariable, loc);
9028 return base.DoResolve (ec);
9033 // A block of object or collection initializers
9035 public class CollectionOrObjectInitializers : ExpressionStatement
9037 IList<Expression> initializers;
9038 bool is_collection_initialization;
9040 public static readonly CollectionOrObjectInitializers Empty =
9041 new CollectionOrObjectInitializers (Array.AsReadOnly (new Expression [0]), Location.Null);
9043 public CollectionOrObjectInitializers (IList<Expression> initializers, Location loc)
9045 this.initializers = initializers;
9049 public bool IsEmpty {
9051 return initializers.Count == 0;
9055 public bool IsCollectionInitializer {
9057 return is_collection_initialization;
9061 protected override void CloneTo (CloneContext clonectx, Expression target)
9063 CollectionOrObjectInitializers t = (CollectionOrObjectInitializers) target;
9065 t.initializers = new List<Expression> (initializers.Count);
9066 foreach (var e in initializers)
9067 t.initializers.Add (e.Clone (clonectx));
9070 public override Expression CreateExpressionTree (ResolveContext ec)
9072 var expr_initializers = new ArrayInitializer (initializers.Count, loc);
9073 foreach (Expression e in initializers) {
9074 Expression expr = e.CreateExpressionTree (ec);
9076 expr_initializers.Add (expr);
9079 return new ImplicitlyTypedArrayCreation ("[]", expr_initializers, loc);
9082 protected override Expression DoResolve (ResolveContext ec)
9084 List<string> element_names = null;
9085 for (int i = 0; i < initializers.Count; ++i) {
9086 Expression initializer = (Expression) initializers [i];
9087 ElementInitializer element_initializer = initializer as ElementInitializer;
9090 if (element_initializer != null) {
9091 element_names = new List<string> (initializers.Count);
9092 element_names.Add (element_initializer.Name);
9093 } else if (initializer is CompletingExpression){
9094 initializer.Resolve (ec);
9095 throw new InternalErrorException ("This line should never be reached");
9097 if (!ec.CurrentInitializerVariable.Type.ImplementsInterface (TypeManager.ienumerable_type)) {
9098 ec.Report.Error (1922, loc, "A field or property `{0}' cannot be initialized with a collection " +
9099 "object initializer because type `{1}' does not implement `{2}' interface",
9100 ec.CurrentInitializerVariable.GetSignatureForError (),
9101 TypeManager.CSharpName (ec.CurrentInitializerVariable.Type),
9102 TypeManager.CSharpName (TypeManager.ienumerable_type));
9105 is_collection_initialization = true;
9108 if (is_collection_initialization != (element_initializer == null)) {
9109 ec.Report.Error (747, initializer.Location, "Inconsistent `{0}' member declaration",
9110 is_collection_initialization ? "collection initializer" : "object initializer");
9114 if (!is_collection_initialization) {
9115 if (element_names.Contains (element_initializer.Name)) {
9116 ec.Report.Error (1912, element_initializer.Location,
9117 "An object initializer includes more than one member `{0}' initialization",
9118 element_initializer.Name);
9120 element_names.Add (element_initializer.Name);
9125 Expression e = initializer.Resolve (ec);
9126 if (e == EmptyExpressionStatement.Instance)
9127 initializers.RemoveAt (i--);
9129 initializers [i] = e;
9132 type = ec.CurrentInitializerVariable.Type;
9133 if (is_collection_initialization) {
9134 if (TypeManager.HasElementType (type)) {
9135 ec.Report.Error (1925, loc, "Cannot initialize object of type `{0}' with a collection initializer",
9136 TypeManager.CSharpName (type));
9140 eclass = ExprClass.Variable;
9144 public override void Emit (EmitContext ec)
9149 public override void EmitStatement (EmitContext ec)
9151 foreach (ExpressionStatement e in initializers)
9152 e.EmitStatement (ec);
9157 // New expression with element/object initializers
9159 public class NewInitialize : New
9162 // This class serves as a proxy for variable initializer target instances.
9163 // A real variable is assigned later when we resolve left side of an
9166 sealed class InitializerTargetExpression : Expression, IMemoryLocation
9168 NewInitialize new_instance;
9170 public InitializerTargetExpression (NewInitialize newInstance)
9172 this.type = newInstance.type;
9173 this.loc = newInstance.loc;
9174 this.eclass = newInstance.eclass;
9175 this.new_instance = newInstance;
9178 public override Expression CreateExpressionTree (ResolveContext ec)
9180 // Should not be reached
9181 throw new NotSupportedException ("ET");
9184 protected override Expression DoResolve (ResolveContext ec)
9189 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
9194 public override void Emit (EmitContext ec)
9196 Expression e = (Expression) new_instance.instance;
9200 #region IMemoryLocation Members
9202 public void AddressOf (EmitContext ec, AddressOp mode)
9204 new_instance.instance.AddressOf (ec, mode);
9210 CollectionOrObjectInitializers initializers;
9211 IMemoryLocation instance;
9213 public NewInitialize (Expression requested_type, Arguments arguments, CollectionOrObjectInitializers initializers, Location l)
9214 : base (requested_type, arguments, l)
9216 this.initializers = initializers;
9219 protected override IMemoryLocation EmitAddressOf (EmitContext ec, AddressOp Mode)
9221 instance = base.EmitAddressOf (ec, Mode);
9223 if (!initializers.IsEmpty)
9224 initializers.Emit (ec);
9229 protected override void CloneTo (CloneContext clonectx, Expression t)
9231 base.CloneTo (clonectx, t);
9233 NewInitialize target = (NewInitialize) t;
9234 target.initializers = (CollectionOrObjectInitializers) initializers.Clone (clonectx);
9237 public override Expression CreateExpressionTree (ResolveContext ec)
9239 Arguments args = new Arguments (2);
9240 args.Add (new Argument (base.CreateExpressionTree (ec)));
9241 if (!initializers.IsEmpty)
9242 args.Add (new Argument (initializers.CreateExpressionTree (ec)));
9244 return CreateExpressionFactoryCall (ec,
9245 initializers.IsCollectionInitializer ? "ListInit" : "MemberInit",
9249 protected override Expression DoResolve (ResolveContext ec)
9251 Expression e = base.DoResolve (ec);
9255 Expression previous = ec.CurrentInitializerVariable;
9256 ec.CurrentInitializerVariable = new InitializerTargetExpression (this);
9257 initializers.Resolve (ec);
9258 ec.CurrentInitializerVariable = previous;
9262 public override bool Emit (EmitContext ec, IMemoryLocation target)
9264 bool left_on_stack = base.Emit (ec, target);
9266 if (initializers.IsEmpty)
9267 return left_on_stack;
9269 LocalTemporary temp = target as LocalTemporary;
9271 if (!left_on_stack) {
9272 VariableReference vr = target as VariableReference;
9274 // FIXME: This still does not work correctly for pre-set variables
9275 if (vr != null && vr.IsRef)
9276 target.AddressOf (ec, AddressOp.Load);
9278 ((Expression) target).Emit (ec);
9279 left_on_stack = true;
9282 temp = new LocalTemporary (type);
9289 initializers.Emit (ec);
9291 if (left_on_stack) {
9296 return left_on_stack;
9299 public override bool HasInitializer {
9301 return !initializers.IsEmpty;
9306 public class NewAnonymousType : New
9308 static readonly AnonymousTypeParameter[] EmptyParameters = new AnonymousTypeParameter[0];
9310 List<AnonymousTypeParameter> parameters;
9311 readonly TypeContainer parent;
9312 AnonymousTypeClass anonymous_type;
9314 public NewAnonymousType (List<AnonymousTypeParameter> parameters, TypeContainer parent, Location loc)
9315 : base (null, null, loc)
9317 this.parameters = parameters;
9318 this.parent = parent;
9321 protected override void CloneTo (CloneContext clonectx, Expression target)
9323 if (parameters == null)
9326 NewAnonymousType t = (NewAnonymousType) target;
9327 t.parameters = new List<AnonymousTypeParameter> (parameters.Count);
9328 foreach (AnonymousTypeParameter atp in parameters)
9329 t.parameters.Add ((AnonymousTypeParameter) atp.Clone (clonectx));
9332 AnonymousTypeClass CreateAnonymousType (ResolveContext ec, IList<AnonymousTypeParameter> parameters)
9334 AnonymousTypeClass type = parent.Module.Compiled.GetAnonymousType (parameters);
9338 type = AnonymousTypeClass.Create (ec.Compiler, parent, parameters, loc);
9344 type.ResolveTypeParameters ();
9347 if (ec.Report.Errors == 0)
9350 parent.Module.Compiled.AddAnonymousType (type);
9354 public override Expression CreateExpressionTree (ResolveContext ec)
9356 if (parameters == null)
9357 return base.CreateExpressionTree (ec);
9359 var init = new ArrayInitializer (parameters.Count, loc);
9360 foreach (Property p in anonymous_type.Properties)
9361 init.Add (new TypeOfMethod (MemberCache.GetMember (type, p.Get.Spec), loc));
9363 var ctor_args = new ArrayInitializer (Arguments.Count, loc);
9364 foreach (Argument a in Arguments)
9365 ctor_args.Add (a.CreateExpressionTree (ec));
9367 Arguments args = new Arguments (3);
9368 args.Add (new Argument (method.CreateExpressionTree (ec)));
9369 args.Add (new Argument (new ArrayCreation (TypeManager.expression_type_expr, "[]", ctor_args, loc)));
9370 args.Add (new Argument (new ImplicitlyTypedArrayCreation ("[]", init, loc)));
9372 return CreateExpressionFactoryCall (ec, "New", args);
9375 protected override Expression DoResolve (ResolveContext ec)
9377 if (ec.HasSet (ResolveContext.Options.ConstantScope)) {
9378 ec.Report.Error (836, loc, "Anonymous types cannot be used in this expression");
9382 if (parameters == null) {
9383 anonymous_type = CreateAnonymousType (ec, EmptyParameters);
9384 RequestedType = new TypeExpression (anonymous_type.Definition, loc);
9385 return base.DoResolve (ec);
9389 Arguments = new Arguments (parameters.Count);
9390 TypeExpression [] t_args = new TypeExpression [parameters.Count];
9391 for (int i = 0; i < parameters.Count; ++i) {
9392 Expression e = ((AnonymousTypeParameter) parameters [i]).Resolve (ec);
9398 Arguments.Add (new Argument (e));
9399 t_args [i] = new TypeExpression (e.Type, e.Location);
9405 anonymous_type = CreateAnonymousType (ec, parameters);
9406 if (anonymous_type == null)
9409 RequestedType = new GenericTypeExpr (anonymous_type.Definition, new TypeArguments (t_args), loc);
9410 return base.DoResolve (ec);
9414 public class AnonymousTypeParameter : ShimExpression
9416 public readonly string Name;
9418 public AnonymousTypeParameter (Expression initializer, string name, Location loc)
9419 : base (initializer)
9425 public AnonymousTypeParameter (Parameter parameter)
9426 : base (new SimpleName (parameter.Name, parameter.Location))
9428 this.Name = parameter.Name;
9429 this.loc = parameter.Location;
9432 public override bool Equals (object o)
9434 AnonymousTypeParameter other = o as AnonymousTypeParameter;
9435 return other != null && Name == other.Name;
9438 public override int GetHashCode ()
9440 return Name.GetHashCode ();
9443 protected override Expression DoResolve (ResolveContext ec)
9445 Expression e = expr.Resolve (ec);
9449 if (e.eclass == ExprClass.MethodGroup) {
9450 Error_InvalidInitializer (ec, e.ExprClassName);
9455 if (type == TypeManager.void_type || type == TypeManager.null_type ||
9456 type == InternalType.AnonymousMethod || type.IsPointer) {
9457 Error_InvalidInitializer (ec, e.GetSignatureForError ());
9464 protected virtual void Error_InvalidInitializer (ResolveContext ec, string initializer)
9466 ec.Report.Error (828, loc, "An anonymous type property `{0}' cannot be initialized with `{1}'",