2 // expression.cs: Expression representation for the IL tree.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Marek Safar (marek.safar@seznam.cz)
8 // (C) 2001, 2002, 2003 Ximian, Inc.
9 // (C) 2003, 2004 Novell, Inc.
13 namespace Mono.CSharp {
15 using System.Collections;
16 using System.Reflection;
17 using System.Reflection.Emit;
21 // This is an user operator expression, automatically created during
24 public class UserOperatorCall : Expression {
25 public delegate Expression ExpressionTreeExpression (EmitContext ec, MethodGroupExpr mg);
27 readonly ArrayList arguments;
28 readonly MethodGroupExpr mg;
29 readonly ExpressionTreeExpression expr_tree;
31 public UserOperatorCall (MethodGroupExpr mg, ArrayList args, ExpressionTreeExpression expr_tree, Location loc)
34 this.arguments = args;
35 this.expr_tree = expr_tree;
37 type = ((MethodInfo)mg).ReturnType;
38 eclass = ExprClass.Value;
42 public override Expression CreateExpressionTree (EmitContext ec)
44 if (expr_tree != null)
45 return expr_tree (ec, mg);
47 ArrayList args = new ArrayList (arguments.Count + 1);
48 args.Add (new Argument (new NullConstant (loc).CreateExpressionTree (ec)));
49 args.Add (new Argument (mg.CreateExpressionTree (ec)));
50 foreach (Argument a in arguments) {
51 args.Add (new Argument (a.Expr.CreateExpressionTree (ec)));
54 return CreateExpressionFactoryCall ("Call", args);
57 public override Expression DoResolve (EmitContext ec)
60 // We are born fully resolved
65 public override void Emit (EmitContext ec)
67 mg.EmitCall (ec, arguments);
70 [Obsolete ("It may not be compatible with expression trees")]
71 static public UserOperatorCall MakeSimpleCall (EmitContext ec, MethodGroupExpr mg,
72 Expression e, Location loc)
76 args = new ArrayList (1);
77 Argument a = new Argument (e, Argument.AType.Expression);
79 // We need to resolve the arguments before sending them in !
80 if (!a.Resolve (ec, loc))
84 mg = mg.OverloadResolve (ec, ref args, false, loc);
89 return new UserOperatorCall (mg, args, null, loc);
92 [Obsolete ("It may not be compatible with expression trees")]
93 public MethodInfo Method {
94 get { return (MethodInfo)mg; }
98 public class ParenthesizedExpression : Expression
100 public Expression Expr;
102 public ParenthesizedExpression (Expression expr)
107 public override Expression DoResolve (EmitContext ec)
109 Expr = Expr.Resolve (ec);
113 public override void Emit (EmitContext ec)
115 throw new Exception ("Should not happen");
118 public override Location Location
121 return Expr.Location;
125 protected override void CloneTo (CloneContext clonectx, Expression t)
127 ParenthesizedExpression target = (ParenthesizedExpression) t;
129 target.Expr = Expr.Clone (clonectx);
134 // Unary implements unary expressions.
136 public class Unary : Expression {
137 public enum Operator : byte {
138 UnaryPlus, UnaryNegation, LogicalNot, OnesComplement,
139 Indirection, AddressOf, TOP
142 public readonly Operator Oper;
143 public Expression Expr;
145 public Unary (Operator op, Expression expr, Location loc)
153 /// Returns a stringified representation of the Operator
155 static public string OperName (Operator oper)
158 case Operator.UnaryPlus:
160 case Operator.UnaryNegation:
162 case Operator.LogicalNot:
164 case Operator.OnesComplement:
166 case Operator.AddressOf:
168 case Operator.Indirection:
172 return oper.ToString ();
175 public static readonly string [] oper_names;
179 oper_names = new string [(int)Operator.TOP];
181 oper_names [(int) Operator.UnaryPlus] = "op_UnaryPlus";
182 oper_names [(int) Operator.UnaryNegation] = "op_UnaryNegation";
183 oper_names [(int) Operator.LogicalNot] = "op_LogicalNot";
184 oper_names [(int) Operator.OnesComplement] = "op_OnesComplement";
185 oper_names [(int) Operator.Indirection] = "op_Indirection";
186 oper_names [(int) Operator.AddressOf] = "op_AddressOf";
189 public static void Error_OperatorCannotBeApplied (Location loc, string oper, Type t)
191 Error_OperatorCannotBeApplied (loc, oper, TypeManager.CSharpName (t));
194 public static void Error_OperatorCannotBeApplied (Location loc, string oper, string type)
196 Report.Error (23, loc, "The `{0}' operator cannot be applied to operand of type `{1}'",
200 void Error23 (Type t)
202 Error_OperatorCannotBeApplied (loc, OperName (Oper), t);
206 // This routine will attempt to simplify the unary expression when the
207 // argument is a constant.
209 Constant TryReduceConstant (EmitContext ec, Constant e)
211 Type expr_type = e.Type;
214 case Operator.UnaryPlus:
215 // Unary numeric promotions
216 if (expr_type == TypeManager.byte_type)
217 return new IntConstant (((ByteConstant)e).Value, e.Location);
218 if (expr_type == TypeManager.sbyte_type)
219 return new IntConstant (((SByteConstant)e).Value, e.Location);
220 if (expr_type == TypeManager.short_type)
221 return new IntConstant (((ShortConstant)e).Value, e.Location);
222 if (expr_type == TypeManager.ushort_type)
223 return new IntConstant (((UShortConstant)e).Value, e.Location);
224 if (expr_type == TypeManager.char_type)
225 return new IntConstant (((CharConstant)e).Value, e.Location);
227 // Predefined operators
228 if (expr_type == TypeManager.int32_type || expr_type == TypeManager.uint32_type ||
229 expr_type == TypeManager.int64_type || expr_type == TypeManager.uint64_type ||
230 expr_type == TypeManager.float_type || expr_type == TypeManager.double_type ||
231 expr_type == TypeManager.decimal_type)
238 case Operator.UnaryNegation:
239 // Unary numeric promotions
240 if (expr_type == TypeManager.byte_type)
241 return new IntConstant (-((ByteConstant)e).Value, e.Location);
242 if (expr_type == TypeManager.sbyte_type)
243 return new IntConstant (-((SByteConstant)e).Value, e.Location);
244 if (expr_type == TypeManager.short_type)
245 return new IntConstant (-((ShortConstant)e).Value, e.Location);
246 if (expr_type == TypeManager.ushort_type)
247 return new IntConstant (-((UShortConstant)e).Value, e.Location);
248 if (expr_type == TypeManager.char_type)
249 return new IntConstant (-((CharConstant)e).Value, e.Location);
251 // Predefined operators
252 if (expr_type == TypeManager.int32_type) {
253 int value = ((IntConstant)e).Value;
254 if (value == int.MinValue) {
255 if (ec.ConstantCheckState) {
256 ConstantFold.Error_CompileTimeOverflow (loc);
261 return new IntConstant (-value, e.Location);
263 if (expr_type == TypeManager.int64_type) {
264 long value = ((LongConstant)e).Value;
265 if (value == long.MinValue) {
266 if (ec.ConstantCheckState) {
267 ConstantFold.Error_CompileTimeOverflow (loc);
272 return new LongConstant (-value, e.Location);
275 if (expr_type == TypeManager.uint32_type) {
276 UIntLiteral uil = e as UIntLiteral;
278 if (uil.Value == 2147483648)
279 return new IntLiteral (int.MinValue, e.Location);
280 return new LongLiteral (-uil.Value, e.Location);
282 return new LongConstant (-((UIntConstant)e).Value, e.Location);
285 if (expr_type == TypeManager.uint64_type) {
286 ULongLiteral ull = e as ULongLiteral;
287 if (ull != null && ull.Value == 9223372036854775808)
288 return new LongLiteral (long.MinValue, e.Location);
292 if (expr_type == TypeManager.float_type) {
293 FloatLiteral fl = e as FloatLiteral;
294 // For better error reporting
296 fl.Value = -fl.Value;
299 return new FloatConstant (-((FloatConstant)e).Value, e.Location);
301 if (expr_type == TypeManager.double_type) {
302 DoubleLiteral dl = e as DoubleLiteral;
303 // For better error reporting
305 dl.Value = -dl.Value;
309 return new DoubleConstant (-((DoubleConstant)e).Value, e.Location);
311 if (expr_type == TypeManager.decimal_type)
312 return new DecimalConstant (-((DecimalConstant)e).Value, e.Location);
316 case Operator.LogicalNot:
317 if (expr_type != TypeManager.bool_type)
320 BoolConstant b = (BoolConstant) e;
321 return new BoolConstant (!(b.Value), b.Location);
323 case Operator.OnesComplement:
324 // Unary numeric promotions
325 if (expr_type == TypeManager.byte_type)
326 return new IntConstant (~((ByteConstant)e).Value, e.Location);
327 if (expr_type == TypeManager.sbyte_type)
328 return new IntConstant (~((SByteConstant)e).Value, e.Location);
329 if (expr_type == TypeManager.short_type)
330 return new IntConstant (~((ShortConstant)e).Value, e.Location);
331 if (expr_type == TypeManager.ushort_type)
332 return new IntConstant (~((UShortConstant)e).Value, e.Location);
333 if (expr_type == TypeManager.char_type)
334 return new IntConstant (~((CharConstant)e).Value, e.Location);
336 // Predefined operators
337 if (expr_type == TypeManager.int32_type)
338 return new IntConstant (~((IntConstant)e).Value, e.Location);
339 if (expr_type == TypeManager.uint32_type)
340 return new UIntConstant (~((UIntConstant)e).Value, e.Location);
341 if (expr_type == TypeManager.int64_type)
342 return new LongConstant (~((LongConstant)e).Value, e.Location);
343 if (expr_type == TypeManager.uint64_type){
344 return new ULongConstant (~((ULongConstant)e).Value, e.Location);
346 if (e is EnumConstant) {
347 e = TryReduceConstant (ec, ((EnumConstant)e).Child);
349 e = new EnumConstant (e, expr_type);
354 case Operator.AddressOf:
357 case Operator.Indirection:
360 throw new Exception ("Can not constant fold: " + Oper.ToString());
363 Expression ResolveOperator (EmitContext ec)
366 // Step 1: Default operations on CLI native types.
369 // Attempt to use a constant folding operation.
370 Constant cexpr = Expr as Constant;
372 cexpr = TryReduceConstant (ec, cexpr);
379 // Step 2: Perform Operator Overload location
381 Type expr_type = Expr.Type;
382 string op_name = oper_names [(int) Oper];
384 MethodGroupExpr user_op = MemberLookup (ec.ContainerType, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc) as MethodGroupExpr;
385 if (user_op != null) {
386 ArrayList args = new ArrayList (1);
387 args.Add (new Argument (Expr));
388 user_op = user_op.OverloadResolve (ec, ref args, false, loc);
390 if (user_op == null) {
395 return new UserOperatorCall (user_op, args, CreateExpressionTree, loc);
399 case Operator.LogicalNot:
400 if (expr_type != TypeManager.bool_type) {
401 Expr = ResolveBoolean (ec, Expr, loc);
408 type = TypeManager.bool_type;
411 case Operator.OnesComplement:
412 // Unary numeric promotions
413 if (expr_type == TypeManager.byte_type || expr_type == TypeManager.sbyte_type ||
414 expr_type == TypeManager.short_type || expr_type == TypeManager.ushort_type ||
415 expr_type == TypeManager.char_type)
417 type = TypeManager.int32_type;
418 return EmptyCast.Create (this, type);
421 // Predefined operators
422 if (expr_type == TypeManager.int32_type || expr_type == TypeManager.uint32_type ||
423 expr_type == TypeManager.int64_type || expr_type == TypeManager.uint64_type ||
424 TypeManager.IsEnumType (expr_type))
430 type = TypeManager.int32_type;
431 Expr = Convert.ImplicitUserConversion(ec, Expr, type, loc);
438 case Operator.AddressOf:
444 if (!TypeManager.VerifyUnManaged (Expr.Type, loc)){
448 IVariable variable = Expr as IVariable;
449 bool is_fixed = variable != null && variable.VerifyFixed ();
451 if (!ec.InFixedInitializer && !is_fixed) {
452 Error (212, "You can only take the address of unfixed expression inside " +
453 "of a fixed statement initializer");
457 if (ec.InFixedInitializer && is_fixed) {
458 Error (213, "You cannot use the fixed statement to take the address of an already fixed expression");
462 LocalVariableReference lr = Expr as LocalVariableReference;
464 if (lr.local_info.IsCaptured){
465 AnonymousMethod.Error_AddressOfCapturedVar (lr.Name, loc);
468 lr.local_info.AddressTaken = true;
469 lr.local_info.Used = true;
472 ParameterReference pr = Expr as ParameterReference;
473 if ((pr != null) && pr.Parameter.IsCaptured) {
474 AnonymousMethod.Error_AddressOfCapturedVar (pr.Name, loc);
478 // According to the specs, a variable is considered definitely assigned if you take
480 if ((variable != null) && (variable.VariableInfo != null)){
481 variable.VariableInfo.SetAssigned (ec);
484 type = TypeManager.GetPointerType (Expr.Type);
487 case Operator.Indirection:
493 if (!expr_type.IsPointer){
494 Error (193, "The * or -> operator must be applied to a pointer");
499 // We create an Indirection expression, because
500 // it can implement the IMemoryLocation.
502 return new Indirection (Expr, loc);
504 case Operator.UnaryPlus:
505 // Unary numeric promotions
506 if (expr_type == TypeManager.byte_type || expr_type == TypeManager.sbyte_type ||
507 expr_type == TypeManager.short_type || expr_type == TypeManager.ushort_type ||
508 expr_type == TypeManager.char_type)
510 return EmptyCast.Create (Expr, TypeManager.int32_type);
513 // Predefined operators
514 if (expr_type == TypeManager.int32_type || expr_type == TypeManager.uint32_type ||
515 expr_type == TypeManager.int64_type || expr_type == TypeManager.uint64_type ||
516 expr_type == TypeManager.float_type || expr_type == TypeManager.double_type ||
517 expr_type == TypeManager.decimal_type)
522 Expr = Convert.ImplicitUserConversion(ec, Expr, TypeManager.int32_type, loc);
524 // Because we can completely ignore unary +
531 case Operator.UnaryNegation:
533 // transform - - expr into expr
535 Unary u = Expr as Unary;
536 if (u != null && u.Oper == Operator.UnaryNegation) {
540 // Unary numeric promotions
541 if (expr_type == TypeManager.byte_type || expr_type == TypeManager.sbyte_type ||
542 expr_type == TypeManager.short_type || expr_type == TypeManager.ushort_type ||
543 expr_type == TypeManager.char_type)
545 type = TypeManager.int32_type;
546 return EmptyCast.Create (this, type);
550 // Predefined operators
552 if (expr_type == TypeManager.uint32_type) {
553 type = TypeManager.int64_type;
554 Expr = Convert.ImplicitNumericConversion (Expr, type);
558 if (expr_type == TypeManager.int32_type || expr_type == TypeManager.int64_type ||
559 expr_type == TypeManager.float_type || expr_type == TypeManager.double_type ||
560 expr_type == TypeManager.decimal_type)
569 type = TypeManager.int32_type;
570 Expr = Convert.ImplicitUserConversion(ec, Expr, type, loc);
578 Error (187, "No such operator '" + OperName (Oper) + "' defined for type '" +
579 TypeManager.CSharpName (expr_type) + "'");
583 public override Expression CreateExpressionTree (EmitContext ec)
585 return CreateExpressionTree (ec, null);
588 Expression CreateExpressionTree (EmitContext ec, MethodGroupExpr user_op)
592 case Operator.UnaryNegation:
593 method_name = "Negate";
597 throw new InternalErrorException ("Unknown unary operator " + Oper.ToString ());
600 ArrayList args = new ArrayList (2);
601 args.Add (new Argument (Expr.CreateExpressionTree (ec)));
603 args.Add (new Argument (user_op.CreateExpressionTree (ec)));
604 return CreateExpressionFactoryCall (method_name, args);
607 public override Expression DoResolve (EmitContext ec)
609 if (Oper == Operator.AddressOf) {
610 Expr = Expr.DoResolveLValue (ec, new EmptyExpression ());
612 if (Expr == null || Expr.eclass != ExprClass.Variable){
613 Error (211, "Cannot take the address of the given expression");
618 Expr = Expr.Resolve (ec);
624 if (TypeManager.IsNullableValueType (Expr.Type))
625 return new Nullable.LiftedUnaryOperator (Oper, Expr, loc).Resolve (ec);
628 eclass = ExprClass.Value;
629 return ResolveOperator (ec);
632 public override Expression DoResolveLValue (EmitContext ec, Expression right)
634 if (Oper == Operator.Indirection)
635 return DoResolve (ec);
640 public override void Emit (EmitContext ec)
642 ILGenerator ig = ec.ig;
645 case Operator.UnaryPlus:
646 throw new Exception ("This should be caught by Resolve");
648 case Operator.UnaryNegation:
649 if (ec.CheckState && type != TypeManager.float_type && type != TypeManager.double_type) {
650 ig.Emit (OpCodes.Ldc_I4_0);
651 if (type == TypeManager.int64_type)
652 ig.Emit (OpCodes.Conv_U8);
654 ig.Emit (OpCodes.Sub_Ovf);
657 ig.Emit (OpCodes.Neg);
662 case Operator.LogicalNot:
664 ig.Emit (OpCodes.Ldc_I4_0);
665 ig.Emit (OpCodes.Ceq);
668 case Operator.OnesComplement:
670 ig.Emit (OpCodes.Not);
673 case Operator.AddressOf:
674 ((IMemoryLocation)Expr).AddressOf (ec, AddressOp.LoadStore);
678 throw new Exception ("This should not happen: Operator = "
683 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
685 if (Oper == Operator.LogicalNot)
686 Expr.EmitBranchable (ec, target, !on_true);
688 base.EmitBranchable (ec, target, on_true);
691 public override string ToString ()
693 return "Unary (" + Oper + ", " + Expr + ")";
696 protected override void CloneTo (CloneContext clonectx, Expression t)
698 Unary target = (Unary) t;
700 target.Expr = Expr.Clone (clonectx);
705 // Unary operators are turned into Indirection expressions
706 // after semantic analysis (this is so we can take the address
707 // of an indirection).
709 public class Indirection : Expression, IMemoryLocation, IAssignMethod, IVariable {
711 LocalTemporary temporary;
714 public Indirection (Expression expr, Location l)
717 type = TypeManager.HasElementType (expr.Type) ? TypeManager.GetElementType (expr.Type) : expr.Type;
718 eclass = ExprClass.Variable;
722 public override void Emit (EmitContext ec)
727 LoadFromPtr (ec.ig, Type);
730 public void Emit (EmitContext ec, bool leave_copy)
734 ec.ig.Emit (OpCodes.Dup);
735 temporary = new LocalTemporary (expr.Type);
736 temporary.Store (ec);
740 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
742 prepared = prepare_for_load;
746 if (prepare_for_load)
747 ec.ig.Emit (OpCodes.Dup);
751 ec.ig.Emit (OpCodes.Dup);
752 temporary = new LocalTemporary (expr.Type);
753 temporary.Store (ec);
756 StoreFromPtr (ec.ig, type);
758 if (temporary != null) {
760 temporary.Release (ec);
764 public void AddressOf (EmitContext ec, AddressOp Mode)
769 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
771 return DoResolve (ec);
774 public override Expression DoResolve (EmitContext ec)
777 // Born fully resolved
782 public override string ToString ()
784 return "*(" + expr + ")";
787 #region IVariable Members
789 public VariableInfo VariableInfo {
793 public bool VerifyFixed ()
795 // A pointer-indirection is always fixed.
803 /// Unary Mutator expressions (pre and post ++ and --)
807 /// UnaryMutator implements ++ and -- expressions. It derives from
808 /// ExpressionStatement becuase the pre/post increment/decrement
809 /// operators can be used in a statement context.
811 /// FIXME: Idea, we could split this up in two classes, one simpler
812 /// for the common case, and one with the extra fields for more complex
813 /// classes (indexers require temporary access; overloaded require method)
816 public class UnaryMutator : ExpressionStatement {
818 public enum Mode : byte {
825 PreDecrement = IsDecrement,
826 PostIncrement = IsPost,
827 PostDecrement = IsPost | IsDecrement
831 bool is_expr = false;
832 bool recurse = false;
837 // This is expensive for the simplest case.
839 UserOperatorCall method;
841 public UnaryMutator (Mode m, Expression e, Location l)
848 static string OperName (Mode mode)
850 return (mode == Mode.PreIncrement || mode == Mode.PostIncrement) ?
855 /// Returns whether an object of type `t' can be incremented
856 /// or decremented with add/sub (ie, basically whether we can
857 /// use pre-post incr-decr operations on it, but it is not a
858 /// System.Decimal, which we require operator overloading to catch)
860 static bool IsIncrementableNumber (Type t)
862 return (t == TypeManager.sbyte_type) ||
863 (t == TypeManager.byte_type) ||
864 (t == TypeManager.short_type) ||
865 (t == TypeManager.ushort_type) ||
866 (t == TypeManager.int32_type) ||
867 (t == TypeManager.uint32_type) ||
868 (t == TypeManager.int64_type) ||
869 (t == TypeManager.uint64_type) ||
870 (t == TypeManager.char_type) ||
871 (t.IsSubclassOf (TypeManager.enum_type)) ||
872 (t == TypeManager.float_type) ||
873 (t == TypeManager.double_type) ||
874 (t.IsPointer && t != TypeManager.void_ptr_type);
877 Expression ResolveOperator (EmitContext ec)
879 Type expr_type = expr.Type;
882 // Step 1: Perform Operator Overload location
887 if (mode == Mode.PreIncrement || mode == Mode.PostIncrement)
888 op_name = "op_Increment";
890 op_name = "op_Decrement";
892 mg = MemberLookup (ec.ContainerType, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
895 method = UserOperatorCall.MakeSimpleCall (
896 ec, (MethodGroupExpr) mg, expr, loc);
899 } else if (!IsIncrementableNumber (expr_type)) {
900 Error (187, "No such operator '" + OperName (mode) + "' defined for type '" +
901 TypeManager.CSharpName (expr_type) + "'");
906 // The operand of the prefix/postfix increment decrement operators
907 // should be an expression that is classified as a variable,
908 // a property access or an indexer access
911 if (expr.eclass == ExprClass.Variable){
912 LocalVariableReference var = expr as LocalVariableReference;
913 if ((var != null) && var.IsReadOnly) {
914 Error (1604, "cannot assign to `" + var.Name + "' because it is readonly");
917 } else if (expr.eclass == ExprClass.IndexerAccess || expr.eclass == ExprClass.PropertyAccess){
918 expr = expr.ResolveLValue (ec, this, Location);
922 Report.Error (1059, loc, "The operand of an increment or decrement operator must be a variable, property or indexer");
929 public override Expression DoResolve (EmitContext ec)
931 expr = expr.Resolve (ec);
936 eclass = ExprClass.Value;
939 if (TypeManager.IsNullableValueType (expr.Type))
940 return new Nullable.LiftedUnaryMutator (mode, expr, loc).Resolve (ec);
943 return ResolveOperator (ec);
947 // Loads the proper "1" into the stack based on the type, then it emits the
948 // opcode for the operation requested
950 void LoadOneAndEmitOp (EmitContext ec, Type t)
953 // Measure if getting the typecode and using that is more/less efficient
954 // that comparing types. t.GetTypeCode() is an internal call.
956 ILGenerator ig = ec.ig;
958 if (t == TypeManager.uint64_type || t == TypeManager.int64_type)
959 LongConstant.EmitLong (ig, 1);
960 else if (t == TypeManager.double_type)
961 ig.Emit (OpCodes.Ldc_R8, 1.0);
962 else if (t == TypeManager.float_type)
963 ig.Emit (OpCodes.Ldc_R4, 1.0F);
964 else if (t.IsPointer){
965 Type et = TypeManager.GetElementType (t);
966 int n = GetTypeSize (et);
969 ig.Emit (OpCodes.Sizeof, et);
971 IntConstant.EmitInt (ig, n);
973 ig.Emit (OpCodes.Ldc_I4_1);
976 // Now emit the operation
979 if (t == TypeManager.int32_type ||
980 t == TypeManager.int64_type){
981 if ((mode & Mode.IsDecrement) != 0)
982 ig.Emit (OpCodes.Sub_Ovf);
984 ig.Emit (OpCodes.Add_Ovf);
985 } else if (t == TypeManager.uint32_type ||
986 t == TypeManager.uint64_type){
987 if ((mode & Mode.IsDecrement) != 0)
988 ig.Emit (OpCodes.Sub_Ovf_Un);
990 ig.Emit (OpCodes.Add_Ovf_Un);
992 if ((mode & Mode.IsDecrement) != 0)
993 ig.Emit (OpCodes.Sub_Ovf);
995 ig.Emit (OpCodes.Add_Ovf);
998 if ((mode & Mode.IsDecrement) != 0)
999 ig.Emit (OpCodes.Sub);
1001 ig.Emit (OpCodes.Add);
1004 if (t == TypeManager.sbyte_type){
1006 ig.Emit (OpCodes.Conv_Ovf_I1);
1008 ig.Emit (OpCodes.Conv_I1);
1009 } else if (t == TypeManager.byte_type){
1011 ig.Emit (OpCodes.Conv_Ovf_U1);
1013 ig.Emit (OpCodes.Conv_U1);
1014 } else if (t == TypeManager.short_type){
1016 ig.Emit (OpCodes.Conv_Ovf_I2);
1018 ig.Emit (OpCodes.Conv_I2);
1019 } else if (t == TypeManager.ushort_type || t == TypeManager.char_type){
1021 ig.Emit (OpCodes.Conv_Ovf_U2);
1023 ig.Emit (OpCodes.Conv_U2);
1028 void EmitCode (EmitContext ec, bool is_expr)
1031 this.is_expr = is_expr;
1032 ((IAssignMethod) expr).EmitAssign (ec, this, is_expr && (mode == Mode.PreIncrement || mode == Mode.PreDecrement), true);
1035 public override void Emit (EmitContext ec)
1038 // We use recurse to allow ourselfs to be the source
1039 // of an assignment. This little hack prevents us from
1040 // having to allocate another expression
1043 ((IAssignMethod) expr).Emit (ec, is_expr && (mode == Mode.PostIncrement || mode == Mode.PostDecrement));
1045 LoadOneAndEmitOp (ec, expr.Type);
1047 ec.ig.Emit (OpCodes.Call, method.Method);
1052 EmitCode (ec, true);
1055 public override void EmitStatement (EmitContext ec)
1057 EmitCode (ec, false);
1060 protected override void CloneTo (CloneContext clonectx, Expression t)
1062 UnaryMutator target = (UnaryMutator) t;
1064 target.expr = expr.Clone (clonectx);
1069 /// Base class for the `Is' and `As' classes.
1073 /// FIXME: Split this in two, and we get to save the `Operator' Oper
1076 public abstract class Probe : Expression {
1077 public Expression ProbeType;
1078 protected Expression expr;
1079 protected TypeExpr probe_type_expr;
1081 public Probe (Expression expr, Expression probe_type, Location l)
1083 ProbeType = probe_type;
1088 public Expression Expr {
1094 public override Expression DoResolve (EmitContext ec)
1096 probe_type_expr = ProbeType.ResolveAsTypeTerminal (ec, false);
1097 if (probe_type_expr == null)
1100 expr = expr.Resolve (ec);
1104 if (expr.Type.IsPointer || probe_type_expr.Type.IsPointer) {
1105 Report.Error (244, loc, "The `{0}' operator cannot be applied to an operand of pointer type",
1110 if (expr.Type == TypeManager.anonymous_method_type) {
1111 Report.Error (837, loc, "The `{0}' operator cannot be applied to a lambda expression or anonymous method",
1119 protected abstract string OperatorName { get; }
1121 protected override void CloneTo (CloneContext clonectx, Expression t)
1123 Probe target = (Probe) t;
1125 target.expr = expr.Clone (clonectx);
1126 target.ProbeType = ProbeType.Clone (clonectx);
1132 /// Implementation of the `is' operator.
1134 public class Is : Probe {
1135 public Is (Expression expr, Expression probe_type, Location l)
1136 : base (expr, probe_type, l)
1140 public override void Emit (EmitContext ec)
1142 ILGenerator ig = ec.ig;
1145 ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
1146 ig.Emit (OpCodes.Ldnull);
1147 ig.Emit (OpCodes.Cgt_Un);
1150 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
1152 ILGenerator ig = ec.ig;
1155 ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
1156 ig.Emit (on_true ? OpCodes.Brtrue : OpCodes.Brfalse, target);
1159 Expression CreateConstantResult (bool result)
1162 Report.Warning (183, 1, loc, "The given expression is always of the provided (`{0}') type",
1163 TypeManager.CSharpName (probe_type_expr.Type));
1165 Report.Warning (184, 1, loc, "The given expression is never of the provided (`{0}') type",
1166 TypeManager.CSharpName (probe_type_expr.Type));
1168 return new BoolConstant (result, loc);
1171 public override Expression DoResolve (EmitContext ec)
1173 if (base.DoResolve (ec) == null)
1177 bool d_is_nullable = false;
1179 if (expr is Constant) {
1181 // If E is a method group or the null literal, of if the type of E is a reference
1182 // type or a nullable type and the value of E is null, the result is false
1185 return CreateConstantResult (false);
1186 } else if (TypeManager.IsNullableType (d) && !TypeManager.ContainsGenericParameters (d)) {
1187 d = TypeManager.GetTypeArguments (d) [0];
1188 d_is_nullable = true;
1191 type = TypeManager.bool_type;
1192 eclass = ExprClass.Value;
1193 Type t = probe_type_expr.Type;
1194 bool t_is_nullable = false;
1195 if (TypeManager.IsNullableType (t) && !TypeManager.ContainsGenericParameters (t)) {
1196 t = TypeManager.GetTypeArguments (t) [0];
1197 t_is_nullable = true;
1200 if (t.IsValueType) {
1203 // D and T are the same value types but D can be null
1205 if (d_is_nullable && !t_is_nullable)
1206 return Nullable.HasValue.Create (expr, ec);
1209 // The result is true if D and T are the same value types
1211 return CreateConstantResult (true);
1214 if (TypeManager.IsGenericParameter (d))
1215 return ResolveGenericParameter (t, d);
1218 // An unboxing conversion exists
1220 if (Convert.ExplicitReferenceConversionExists (d, t))
1223 if (TypeManager.IsGenericParameter (t))
1224 return ResolveGenericParameter (d, t);
1226 if (d.IsValueType) {
1228 if (Convert.ImplicitBoxingConversionExists (expr, t, out temp))
1229 return CreateConstantResult (true);
1231 if (TypeManager.IsGenericParameter (d))
1232 return ResolveGenericParameter (t, d);
1234 if (TypeManager.ContainsGenericParameters (d))
1237 if (Convert.ImplicitReferenceConversionExists (expr, t) ||
1238 Convert.ExplicitReferenceConversionExists (d, t)) {
1244 return CreateConstantResult (false);
1247 Expression ResolveGenericParameter (Type d, Type t)
1250 GenericConstraints constraints = TypeManager.GetTypeParameterConstraints (t);
1251 if (constraints != null) {
1252 if (constraints.IsReferenceType && d.IsValueType)
1253 return CreateConstantResult (false);
1255 if (constraints.IsValueType && !d.IsValueType)
1256 return CreateConstantResult (false);
1259 expr = new BoxedCast (expr, d);
1266 protected override string OperatorName {
1267 get { return "is"; }
1272 /// Implementation of the `as' operator.
1274 public class As : Probe {
1275 public As (Expression expr, Expression probe_type, Location l)
1276 : base (expr, probe_type, l)
1280 bool do_isinst = false;
1281 Expression resolved_type;
1283 public override void Emit (EmitContext ec)
1285 ILGenerator ig = ec.ig;
1290 ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
1293 if (TypeManager.IsNullableType (type))
1294 ig.Emit (OpCodes.Unbox_Any, type);
1298 static void Error_CannotConvertType (Type source, Type target, Location loc)
1300 Report.Error (39, loc, "Cannot convert type `{0}' to `{1}' via a built-in conversion",
1301 TypeManager.CSharpName (source),
1302 TypeManager.CSharpName (target));
1305 public override Expression DoResolve (EmitContext ec)
1307 if (resolved_type == null) {
1308 resolved_type = base.DoResolve (ec);
1310 if (resolved_type == null)
1314 type = probe_type_expr.Type;
1315 eclass = ExprClass.Value;
1316 Type etype = expr.Type;
1318 if (type.IsValueType && !TypeManager.IsNullableType (type)) {
1319 Report.Error (77, loc, "The `as' operator cannot be used with a non-nullable value type `{0}'",
1320 TypeManager.CSharpName (type));
1327 // If the type is a type parameter, ensure
1328 // that it is constrained by a class
1330 TypeParameterExpr tpe = probe_type_expr as TypeParameterExpr;
1332 GenericConstraints constraints = tpe.TypeParameter.GenericConstraints;
1335 if (constraints == null)
1338 if (!constraints.HasClassConstraint)
1339 if ((constraints.Attributes & GenericParameterAttributes.ReferenceTypeConstraint) == 0)
1343 Report.Error (413, loc,
1344 "The as operator requires that the `{0}' type parameter be constrained by a class",
1345 probe_type_expr.GetSignatureForError ());
1350 if (expr.IsNull && TypeManager.IsNullableType (type)) {
1351 Report.Warning (458, 2, loc, "The result of the expression is always `null' of type `{0}'",
1352 TypeManager.CSharpName (type));
1355 Expression e = Convert.ImplicitConversion (ec, expr, type, loc);
1362 if (Convert.ExplicitReferenceConversionExists (etype, type)){
1363 if (TypeManager.IsGenericParameter (etype))
1364 expr = new BoxedCast (expr, etype);
1370 if (TypeManager.ContainsGenericParameters (etype) ||
1371 TypeManager.ContainsGenericParameters (type)) {
1372 expr = new BoxedCast (expr, etype);
1377 Error_CannotConvertType (etype, type, loc);
1381 protected override string OperatorName {
1382 get { return "as"; }
1385 public override bool GetAttributableValue (Type value_type, out object value)
1387 return expr.GetAttributableValue (value_type, out value);
1392 /// This represents a typecast in the source language.
1394 /// FIXME: Cast expressions have an unusual set of parsing
1395 /// rules, we need to figure those out.
1397 public class Cast : Expression {
1398 Expression target_type;
1401 public Cast (Expression cast_type, Expression expr)
1402 : this (cast_type, expr, cast_type.Location)
1406 public Cast (Expression cast_type, Expression expr, Location loc)
1408 this.target_type = cast_type;
1412 if (target_type == TypeManager.system_void_expr)
1413 Error_VoidInvalidInTheContext (loc);
1416 public Expression TargetType {
1417 get { return target_type; }
1420 public Expression Expr {
1421 get { return expr; }
1424 public override Expression DoResolve (EmitContext ec)
1426 expr = expr.Resolve (ec);
1430 TypeExpr target = target_type.ResolveAsTypeTerminal (ec, false);
1436 if (type.IsAbstract && type.IsSealed) {
1437 Report.Error (716, loc, "Cannot convert to static type `{0}'", TypeManager.CSharpName (type));
1441 eclass = ExprClass.Value;
1443 Constant c = expr as Constant;
1445 c = c.TryReduce (ec, type, loc);
1450 if (type.IsPointer && !ec.InUnsafe) {
1454 expr = Convert.ExplicitConversion (ec, expr, type, loc);
1458 public override void Emit (EmitContext ec)
1460 throw new Exception ("Should not happen");
1463 protected override void CloneTo (CloneContext clonectx, Expression t)
1465 Cast target = (Cast) t;
1467 target.target_type = target_type.Clone (clonectx);
1468 target.expr = expr.Clone (clonectx);
1473 // C# 2.0 Default value expression
1475 public class DefaultValueExpression : Expression
1479 public DefaultValueExpression (Expression expr, Location loc)
1485 public override Expression DoResolve (EmitContext ec)
1487 TypeExpr texpr = expr.ResolveAsTypeTerminal (ec, false);
1493 if (type == TypeManager.void_type) {
1494 Error_VoidInvalidInTheContext (loc);
1498 if (TypeManager.IsGenericParameter (type))
1500 GenericConstraints constraints = TypeManager.GetTypeParameterConstraints(type);
1501 if (constraints != null && constraints.IsReferenceType)
1502 return new NullDefault (new NullLiteral (Location), type);
1506 Constant c = New.Constantify(type);
1508 return new NullDefault (c, type);
1510 if (!TypeManager.IsValueType (type))
1511 return new NullDefault (new NullLiteral (Location), type);
1513 eclass = ExprClass.Variable;
1517 public override void Emit (EmitContext ec)
1519 LocalTemporary temp_storage = new LocalTemporary(type);
1521 temp_storage.AddressOf(ec, AddressOp.LoadStore);
1522 ec.ig.Emit(OpCodes.Initobj, type);
1523 temp_storage.Emit(ec);
1526 protected override void CloneTo (CloneContext clonectx, Expression t)
1528 DefaultValueExpression target = (DefaultValueExpression) t;
1530 target.expr = expr.Clone (clonectx);
1535 /// Binary operators
1537 public class Binary : Expression {
1538 public enum Operator : byte {
1539 Multiply, Division, Modulus,
1540 Addition, Subtraction,
1541 LeftShift, RightShift,
1542 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
1543 Equality, Inequality,
1552 readonly Operator oper;
1553 protected Expression left, right;
1554 readonly bool is_compound;
1556 // This must be kept in sync with Operator!!!
1557 public static readonly string [] oper_names;
1561 oper_names = new string [(int) Operator.TOP];
1563 oper_names [(int) Operator.Multiply] = "op_Multiply";
1564 oper_names [(int) Operator.Division] = "op_Division";
1565 oper_names [(int) Operator.Modulus] = "op_Modulus";
1566 oper_names [(int) Operator.Addition] = "op_Addition";
1567 oper_names [(int) Operator.Subtraction] = "op_Subtraction";
1568 oper_names [(int) Operator.LeftShift] = "op_LeftShift";
1569 oper_names [(int) Operator.RightShift] = "op_RightShift";
1570 oper_names [(int) Operator.LessThan] = "op_LessThan";
1571 oper_names [(int) Operator.GreaterThan] = "op_GreaterThan";
1572 oper_names [(int) Operator.LessThanOrEqual] = "op_LessThanOrEqual";
1573 oper_names [(int) Operator.GreaterThanOrEqual] = "op_GreaterThanOrEqual";
1574 oper_names [(int) Operator.Equality] = "op_Equality";
1575 oper_names [(int) Operator.Inequality] = "op_Inequality";
1576 oper_names [(int) Operator.BitwiseAnd] = "op_BitwiseAnd";
1577 oper_names [(int) Operator.BitwiseOr] = "op_BitwiseOr";
1578 oper_names [(int) Operator.ExclusiveOr] = "op_ExclusiveOr";
1579 oper_names [(int) Operator.LogicalOr] = "op_LogicalOr";
1580 oper_names [(int) Operator.LogicalAnd] = "op_LogicalAnd";
1583 public Binary (Operator oper, Expression left, Expression right, bool isCompound)
1584 : this (oper, left, right)
1586 this.is_compound = isCompound;
1589 public Binary (Operator oper, Expression left, Expression right)
1594 this.loc = left.Location;
1597 public Operator Oper {
1604 /// Returns a stringified representation of the Operator
1606 string OperName (Operator oper)
1610 case Operator.Multiply:
1613 case Operator.Division:
1616 case Operator.Modulus:
1619 case Operator.Addition:
1622 case Operator.Subtraction:
1625 case Operator.LeftShift:
1628 case Operator.RightShift:
1631 case Operator.LessThan:
1634 case Operator.GreaterThan:
1637 case Operator.LessThanOrEqual:
1640 case Operator.GreaterThanOrEqual:
1643 case Operator.Equality:
1646 case Operator.Inequality:
1649 case Operator.BitwiseAnd:
1652 case Operator.BitwiseOr:
1655 case Operator.ExclusiveOr:
1658 case Operator.LogicalOr:
1661 case Operator.LogicalAnd:
1665 s = oper.ToString ();
1675 public override string ToString ()
1677 return "operator " + OperName (oper) + "(" + left.ToString () + ", " +
1678 right.ToString () + ")";
1681 Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
1683 if (expr.Type == target_type)
1686 return Convert.ImplicitConversion (ec, expr, target_type, loc);
1689 void Error_OperatorAmbiguous (Location loc, Operator oper, Type l, Type r)
1692 34, loc, "Operator `" + OperName (oper)
1693 + "' is ambiguous on operands of type `"
1694 + TypeManager.CSharpName (l) + "' "
1695 + "and `" + TypeManager.CSharpName (r)
1699 bool IsConvertible (EmitContext ec, Expression le, Expression re, Type t)
1701 return Convert.ImplicitConversionExists (ec, le, t) && Convert.ImplicitConversionExists (ec, re, t);
1704 bool VerifyApplicable_Predefined (EmitContext ec, Type t)
1706 if (!IsConvertible (ec, left, right, t))
1708 left = ForceConversion (ec, left, t);
1709 right = ForceConversion (ec, right, t);
1714 bool IsApplicable_String (EmitContext ec, Expression le, Expression re, Operator oper)
1716 bool l = Convert.ImplicitConversionExists (ec, le, TypeManager.string_type);
1717 bool r = Convert.ImplicitConversionExists (ec, re, TypeManager.string_type);
1719 if (oper == Operator.Equality || oper == Operator.Inequality)
1721 if (oper == Operator.Addition)
1726 bool OverloadResolve_PredefinedString (EmitContext ec, Operator oper)
1728 if (!IsApplicable_String (ec, left, right, oper))
1732 Type r = right.Type;
1733 if (OverloadResolve_PredefinedIntegral (ec) ||
1734 OverloadResolve_PredefinedFloating (ec)) {
1735 Error_OperatorAmbiguous (loc, oper, l, r);
1738 Type t = TypeManager.string_type;
1739 if (Convert.ImplicitConversionExists (ec, left, t))
1740 left = ForceConversion (ec, left, t);
1741 if (Convert.ImplicitConversionExists (ec, right, t))
1742 right = ForceConversion (ec, right, t);
1747 bool OverloadResolve_PredefinedIntegral (EmitContext ec)
1749 return VerifyApplicable_Predefined (ec, TypeManager.int32_type) ||
1750 VerifyApplicable_Predefined (ec, TypeManager.uint32_type) ||
1751 VerifyApplicable_Predefined (ec, TypeManager.int64_type) ||
1752 VerifyApplicable_Predefined (ec, TypeManager.uint64_type) ||
1756 bool OverloadResolve_PredefinedFloating (EmitContext ec)
1758 return VerifyApplicable_Predefined (ec, TypeManager.float_type) ||
1759 VerifyApplicable_Predefined (ec, TypeManager.double_type) ||
1763 static public void Error_OperatorCannotBeApplied (Location loc, string name, Type l, Type r)
1765 Error_OperatorCannotBeApplied (loc, name, TypeManager.CSharpName (l), TypeManager.CSharpName (r));
1768 public static void Error_OperatorCannotBeApplied (Location loc, string name, string left, string right)
1770 Report.Error (19, loc, "Operator `{0}' cannot be applied to operands of type `{1}' and `{2}'",
1774 protected void Error_OperatorCannotBeApplied ()
1776 Error_OperatorCannotBeApplied (Location, OperName (oper), TypeManager.CSharpName (left.Type),
1777 TypeManager.CSharpName(right.Type));
1780 static bool IsUnsigned (Type t)
1783 t = t.GetElementType ();
1785 return (t == TypeManager.uint32_type || t == TypeManager.uint64_type ||
1786 t == TypeManager.ushort_type || t == TypeManager.byte_type);
1789 Expression Make32or64 (EmitContext ec, Expression e)
1793 if (t == TypeManager.int32_type || t == TypeManager.uint32_type ||
1794 t == TypeManager.int64_type || t == TypeManager.uint64_type)
1796 Expression ee = Convert.ImplicitConversion (ec, e, TypeManager.int32_type, loc);
1799 ee = Convert.ImplicitConversion (ec, e, TypeManager.uint32_type, loc);
1802 ee = Convert.ImplicitConversion (ec, e, TypeManager.int64_type, loc);
1805 ee = Convert.ImplicitConversion (ec, e, TypeManager.uint64_type, loc);
1811 Expression CheckShiftArguments (EmitContext ec)
1813 Expression new_left = Make32or64 (ec, left);
1814 Expression new_right = ForceConversion (ec, right, TypeManager.int32_type);
1815 if (new_left == null || new_right == null) {
1816 Error_OperatorCannotBeApplied ();
1819 type = new_left.Type;
1820 int shiftmask = (type == TypeManager.int32_type || type == TypeManager.uint32_type) ? 31 : 63;
1822 right = new Binary (Binary.Operator.BitwiseAnd, new_right, new IntConstant (shiftmask, loc)).DoResolve (ec);
1827 // This is used to check if a test 'x == null' can be optimized to a reference equals,
1828 // i.e., not invoke op_Equality.
1830 static bool EqualsNullIsReferenceEquals (Type t)
1832 return t == TypeManager.object_type || t == TypeManager.string_type ||
1833 t == TypeManager.delegate_type || t.IsSubclassOf (TypeManager.delegate_type);
1836 static void Warning_UnintendedReferenceComparison (Location loc, string side, Type type)
1838 Report.Warning ((side == "left" ? 252 : 253), 2, loc,
1839 "Possible unintended reference comparison; to get a value comparison, " +
1840 "cast the {0} hand side to type `{1}'.", side, TypeManager.CSharpName (type));
1843 static void Warning_Constant_Result (Location loc, bool result, Type type)
1845 Report.Warning (472, 2, loc, "The result of comparing `{0}' against null is always `{1}'. " +
1846 "This operation is undocumented and it is temporary supported for compatibility reasons only",
1847 TypeManager.CSharpName (type), result ? "true" : "false");
1850 Expression ResolveOperator (EmitContext ec)
1853 Type r = right.Type;
1855 if (oper == Operator.Equality || oper == Operator.Inequality){
1856 if (right.Type == TypeManager.null_type){
1857 if (TypeManager.IsGenericParameter (l)){
1858 if (l.BaseType == TypeManager.value_type) {
1859 Error_OperatorCannotBeApplied ();
1863 left = new BoxedCast (left, TypeManager.object_type);
1864 Type = TypeManager.bool_type;
1869 // 7.9.9 Equality operators and null
1871 // CSC 2 has this behavior, it allows structs to be compared
1872 // with the null literal *outside* of a generics context and
1873 // inlines that as true or false.
1875 // This is, in my opinion, completely wrong.
1877 if (RootContext.Version != LanguageVersion.ISO_1 && l.IsValueType) {
1878 if (!TypeManager.IsPrimitiveType (l) && !TypeManager.IsEnumType (l)) {
1879 if (MemberLookup (ec.ContainerType, l, oper_names [(int)Operator.Equality], MemberTypes.Method, AllBindingFlags, loc) == null &&
1880 MemberLookup (ec.ContainerType, l, oper_names [(int)Operator.Inequality], MemberTypes.Method, AllBindingFlags, loc) == null) {
1881 Error_OperatorCannotBeApplied ();
1886 Warning_Constant_Result (loc, oper == Operator.Inequality, l);
1887 return new BoolConstant (oper == Operator.Inequality, loc);
1891 if (left is NullLiteral){
1892 if (TypeManager.IsGenericParameter (r)){
1893 if (r.BaseType == TypeManager.value_type) {
1894 Error_OperatorCannotBeApplied ();
1898 right = new BoxedCast (right, TypeManager.object_type);
1899 Type = TypeManager.bool_type;
1904 // 7.9.9 Equality operators and null
1906 // CSC 2 has this behavior, it allows structs to be compared
1907 // with the null literal *outside* of a generics context and
1908 // inlines that as true or false.
1910 // This is, in my opinion, completely wrong.
1912 if (RootContext.Version != LanguageVersion.ISO_1 && r.IsValueType){
1913 if (!TypeManager.IsPrimitiveType (r) && !TypeManager.IsEnumType (r)) {
1914 if (MemberLookup (ec.ContainerType, r, oper_names [(int) Operator.Equality], MemberTypes.Method, AllBindingFlags, loc) == null &&
1915 MemberLookup (ec.ContainerType, r, oper_names [(int) Operator.Inequality], MemberTypes.Method, AllBindingFlags, loc) == null) {
1916 Error_OperatorCannotBeApplied ();
1921 Warning_Constant_Result (loc, oper == Operator.Inequality, r);
1922 return new BoolConstant (oper == Operator.Inequality, loc);
1927 // Optimize out call to op_Equality in a few cases.
1929 if ((l == TypeManager.null_type && EqualsNullIsReferenceEquals (r)) ||
1930 (r == TypeManager.null_type && EqualsNullIsReferenceEquals (l))) {
1931 Type = TypeManager.bool_type;
1936 if (l == TypeManager.intptr_type && r == TypeManager.intptr_type) {
1937 Type = TypeManager.bool_type;
1943 // Delegate equality
1945 MethodGroupExpr mg = null;
1946 Type delegate_type = null;
1947 if (left.eclass == ExprClass.MethodGroup) {
1948 if (!TypeManager.IsDelegateType(r)) {
1949 Error_OperatorCannotBeApplied(Location, OperName(oper),
1950 left.ExprClassName, right.ExprClassName);
1953 mg = (MethodGroupExpr)left;
1955 } else if (right.eclass == ExprClass.MethodGroup) {
1956 if (!TypeManager.IsDelegateType(l)) {
1957 Error_OperatorCannotBeApplied(Location, OperName(oper),
1958 left.ExprClassName, right.ExprClassName);
1961 mg = (MethodGroupExpr)right;
1966 Expression e = ImplicitDelegateCreation.Create (ec, mg, delegate_type, loc);
1970 // Find operator method
1971 string op = oper_names[(int)oper];
1972 MemberInfo[] mi = TypeManager.MemberLookup(ec.ContainerType, null,
1973 TypeManager.delegate_type, MemberTypes.Method, AllBindingFlags, op, null);
1975 ArrayList args = new ArrayList(2);
1976 args.Add(new Argument(e, Argument.AType.Expression));
1977 if (delegate_type == l)
1978 args.Insert(0, new Argument(left, Argument.AType.Expression));
1980 args.Add(new Argument(right, Argument.AType.Expression));
1982 return new BinaryMethod (TypeManager.bool_type, (MethodInfo)mi [0], args);
1985 if (l == TypeManager.anonymous_method_type || r == TypeManager.anonymous_method_type) {
1986 Error_OperatorCannotBeApplied(Location, OperName(oper),
1987 left.ExprClassName, right.ExprClassName);
1994 // Do not perform operator overload resolution when both sides are
1997 MethodGroupExpr left_operators = null, right_operators = null;
1998 if (!(TypeManager.IsPrimitiveType (l) && TypeManager.IsPrimitiveType (r))) {
2000 // Step 1: Perform Operator Overload location
2002 string op = oper_names [(int) oper];
2004 MethodGroupExpr union;
2005 left_operators = MemberLookup (ec.ContainerType, l, op, MemberTypes.Method, AllBindingFlags, loc) as MethodGroupExpr;
2007 right_operators = MemberLookup (
2008 ec.ContainerType, r, op, MemberTypes.Method, AllBindingFlags, loc) as MethodGroupExpr;
2009 union = MethodGroupExpr.MakeUnionSet (left_operators, right_operators, loc);
2011 union = left_operators;
2013 if (union != null) {
2014 ArrayList args = new ArrayList (2);
2015 args.Add (new Argument (left, Argument.AType.Expression));
2016 args.Add (new Argument (right, Argument.AType.Expression));
2018 union = union.OverloadResolve (ec, ref args, true, Location.Null);
2020 if (union != null) {
2021 MethodInfo mi = (MethodInfo) union;
2022 return new BinaryMethod (mi.ReturnType, mi, args);
2028 // String concatenation
2030 // string operator + (string x, string y);
2031 // string operator + (string x, object y);
2032 // string operator + (object x, string y);
2034 if (oper == Operator.Addition && !TypeManager.IsDelegateType (l)) {
2036 // Either left or right expression is implicitly convertible to string
2038 if (OverloadResolve_PredefinedString (ec, oper)) {
2039 if (r == TypeManager.void_type || l == TypeManager.void_type) {
2040 Error_OperatorCannotBeApplied ();
2045 // Constants folding for strings and nulls
2047 if (left.Type == TypeManager.string_type && right.Type == TypeManager.string_type &&
2048 left is Constant && right is Constant) {
2049 string lvalue = (string)((Constant) left).GetValue ();
2050 string rvalue = (string)((Constant) right).GetValue ();
2051 return new StringConstant (lvalue + rvalue, left.Location);
2055 // Append to existing string concatenation
2057 if (left is StringConcat) {
2058 ((StringConcat) left).Append (ec, right);
2063 // Otherwise, start a new concat expression using converted expression
2065 return new StringConcat (ec, loc, left, right).Resolve (ec);
2069 // Transform a + ( - b) into a - b
2071 if (right is Unary){
2072 Unary right_unary = (Unary) right;
2074 if (right_unary.Oper == Unary.Operator.UnaryNegation){
2075 return new Binary (Operator.Subtraction, left, right_unary.Expr).Resolve (ec);
2080 if (oper == Operator.Equality || oper == Operator.Inequality){
2081 if (l == TypeManager.bool_type || r == TypeManager.bool_type){
2082 if (r != TypeManager.bool_type || l != TypeManager.bool_type){
2083 Error_OperatorCannotBeApplied ();
2087 type = TypeManager.bool_type;
2091 if (l.IsPointer || r.IsPointer) {
2092 if (l.IsPointer && r.IsPointer) {
2093 type = TypeManager.bool_type;
2097 if (l.IsPointer && r == TypeManager.null_type) {
2098 right = new EmptyConstantCast (NullPointer.Null, l);
2099 type = TypeManager.bool_type;
2103 if (r.IsPointer && l == TypeManager.null_type) {
2104 left = new EmptyConstantCast (NullPointer.Null, r);
2105 type = TypeManager.bool_type;
2111 if (l.IsGenericParameter && r.IsGenericParameter) {
2112 GenericConstraints l_gc, r_gc;
2114 l_gc = TypeManager.GetTypeParameterConstraints (l);
2115 r_gc = TypeManager.GetTypeParameterConstraints (r);
2117 if ((l_gc == null) || (r_gc == null) ||
2118 !(l_gc.HasReferenceTypeConstraint || l_gc.HasClassConstraint) ||
2119 !(r_gc.HasReferenceTypeConstraint || r_gc.HasClassConstraint)) {
2120 Error_OperatorCannotBeApplied ();
2128 // operator != (object a, object b)
2129 // operator == (object a, object b)
2131 // For this to be used, both arguments have to be reference-types.
2132 // Read the rationale on the spec (14.9.6)
2134 if (!(l.IsValueType || r.IsValueType)){
2135 type = TypeManager.bool_type;
2141 // Also, a standard conversion must exist from either one
2143 // NOTE: An interface is converted to the object before the
2144 // standard conversion is applied. It's not clear from the
2145 // standard but it looks like it works like that.
2148 l = TypeManager.object_type;
2150 r = TypeManager.object_type;
2152 bool left_to_right =
2153 Convert.ImplicitStandardConversionExists (left, r);
2154 bool right_to_left = !left_to_right &&
2155 Convert.ImplicitStandardConversionExists (right, l);
2157 if (!left_to_right && !right_to_left) {
2158 Error_OperatorCannotBeApplied ();
2162 if (left_to_right && left_operators != null &&
2163 Report.WarningLevel >= 2) {
2164 ArrayList args = new ArrayList (2);
2165 args.Add (new Argument (left, Argument.AType.Expression));
2166 args.Add (new Argument (left, Argument.AType.Expression));
2167 if (left_operators.OverloadResolve (ec, ref args, true, Location.Null) != null)
2168 Warning_UnintendedReferenceComparison (loc, "right", l);
2171 if (right_to_left && right_operators != null &&
2172 Report.WarningLevel >= 2) {
2173 ArrayList args = new ArrayList (2);
2174 args.Add (new Argument (right, Argument.AType.Expression));
2175 args.Add (new Argument (right, Argument.AType.Expression));
2176 if (right_operators.OverloadResolve (ec, ref args, true, Location.Null) != null)
2177 Warning_UnintendedReferenceComparison (loc, "left", r);
2181 // We are going to have to convert to an object to compare
2183 if (l != TypeManager.object_type)
2184 left = EmptyCast.Create (left, TypeManager.object_type);
2185 if (r != TypeManager.object_type)
2186 right = EmptyCast.Create (right, TypeManager.object_type);
2192 // Only perform numeric promotions on:
2193 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
2195 if (oper == Operator.Addition || oper == Operator.Subtraction) {
2196 if (TypeManager.IsDelegateType (l)){
2197 if (((right.eclass == ExprClass.MethodGroup) ||
2198 (r == TypeManager.anonymous_method_type))){
2199 if ((RootContext.Version != LanguageVersion.ISO_1)){
2200 Expression tmp = Convert.ImplicitConversionRequired (ec, right, l, loc);
2208 if (TypeManager.IsDelegateType (r) || right is NullLiteral){
2210 ArrayList args = new ArrayList (2);
2212 args = new ArrayList (2);
2213 args.Add (new Argument (left, Argument.AType.Expression));
2214 args.Add (new Argument (right, Argument.AType.Expression));
2216 if (oper == Operator.Addition)
2217 method = TypeManager.delegate_combine_delegate_delegate;
2219 method = TypeManager.delegate_remove_delegate_delegate;
2221 if (!TypeManager.IsEqual (l, r) && !(right is NullLiteral)) {
2222 Error_OperatorCannotBeApplied ();
2226 return new BinaryDelegate (l, method, args);
2231 // Pointer arithmetic:
2233 // T* operator + (T* x, int y);
2234 // T* operator + (T* x, uint y);
2235 // T* operator + (T* x, long y);
2236 // T* operator + (T* x, ulong y);
2238 // T* operator + (int y, T* x);
2239 // T* operator + (uint y, T *x);
2240 // T* operator + (long y, T *x);
2241 // T* operator + (ulong y, T *x);
2243 // T* operator - (T* x, int y);
2244 // T* operator - (T* x, uint y);
2245 // T* operator - (T* x, long y);
2246 // T* operator - (T* x, ulong y);
2248 // long operator - (T* x, T *y)
2251 if (r.IsPointer && oper == Operator.Subtraction){
2253 return new PointerArithmetic (
2254 false, left, right, TypeManager.int64_type,
2257 Expression t = Make32or64 (ec, right);
2259 return new PointerArithmetic (oper == Operator.Addition, left, t, l, loc).Resolve (ec);
2261 } else if (r.IsPointer && oper == Operator.Addition){
2262 Expression t = Make32or64 (ec, left);
2264 return new PointerArithmetic (true, right, t, r, loc).Resolve (ec);
2269 // Enumeration operators
2271 bool lie = TypeManager.IsEnumType (l);
2272 bool rie = TypeManager.IsEnumType (r);
2276 // U operator - (E e, E f)
2278 if (oper == Operator.Subtraction){
2280 type = TypeManager.EnumToUnderlying (l);
2283 Error_OperatorCannotBeApplied ();
2289 // operator + (E e, U x)
2290 // operator - (E e, U x)
2292 if (oper == Operator.Addition || oper == Operator.Subtraction){
2293 Type enum_type = lie ? l : r;
2294 Type other_type = lie ? r : l;
2295 Type underlying_type = TypeManager.EnumToUnderlying (enum_type);
2297 if (underlying_type != other_type){
2298 temp = Convert.ImplicitConversion (ec, lie ? right : left, underlying_type, loc);
2308 Error_OperatorCannotBeApplied ();
2317 temp = Convert.ImplicitConversion (ec, right, l, loc);
2321 Error_OperatorCannotBeApplied ();
2325 temp = Convert.ImplicitConversion (ec, left, r, loc);
2330 Error_OperatorCannotBeApplied ();
2335 if (oper == Operator.Equality || oper == Operator.Inequality ||
2336 oper == Operator.LessThanOrEqual || oper == Operator.LessThan ||
2337 oper == Operator.GreaterThanOrEqual || oper == Operator.GreaterThan){
2338 if (left.Type != right.Type){
2339 Error_OperatorCannotBeApplied ();
2342 type = TypeManager.bool_type;
2346 if (oper == Operator.BitwiseAnd ||
2347 oper == Operator.BitwiseOr ||
2348 oper == Operator.ExclusiveOr){
2349 if (left.Type != right.Type){
2350 Error_OperatorCannotBeApplied ();
2356 Error_OperatorCannotBeApplied ();
2360 if (oper == Operator.LeftShift || oper == Operator.RightShift)
2361 return CheckShiftArguments (ec);
2363 if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
2364 if (l == TypeManager.bool_type && r == TypeManager.bool_type) {
2365 type = TypeManager.bool_type;
2369 Expression left_operators_e = l == TypeManager.bool_type ?
2370 left : Convert.ImplicitUserConversion (ec, left, TypeManager.bool_type, loc);
2371 Expression right_operators_e = r == TypeManager.bool_type ?
2372 right : Convert.ImplicitUserConversion (ec, right, TypeManager.bool_type, loc);
2374 if (left_operators_e != null && right_operators_e != null) {
2375 left = left_operators_e;
2376 right = right_operators_e;
2377 type = TypeManager.bool_type;
2381 Expression e = new ConditionalLogicalOperator (
2382 oper == Operator.LogicalAnd, left, right, l, loc);
2383 return e.Resolve (ec);
2386 Expression orig_left = left;
2387 Expression orig_right = right;
2390 // operator & (bool x, bool y)
2391 // operator | (bool x, bool y)
2392 // operator ^ (bool x, bool y)
2394 if (oper == Operator.BitwiseAnd ||
2395 oper == Operator.BitwiseOr ||
2396 oper == Operator.ExclusiveOr) {
2397 if (OverloadResolve_PredefinedIntegral (ec)) {
2398 if (IsConvertible (ec, orig_left, orig_right, TypeManager.bool_type)) {
2399 Error_OperatorAmbiguous (loc, oper, l, r);
2403 if (oper == Operator.BitwiseOr && l != r && !(orig_right is Constant) && right is OpcodeCast &&
2404 (r == TypeManager.sbyte_type || r == TypeManager.short_type ||
2405 r == TypeManager.int32_type || r == TypeManager.int64_type)) {
2406 Report.Warning (675, 3, loc, "The operator `|' used on the sign-extended type `{0}'. Consider casting to a smaller unsigned type first",
2407 TypeManager.CSharpName (r));
2410 } else if (!VerifyApplicable_Predefined (ec, TypeManager.bool_type)) {
2411 Error_OperatorCannotBeApplied ();
2418 // Pointer comparison
2420 if (l.IsPointer && r.IsPointer){
2421 if (oper == Operator.LessThan || oper == Operator.LessThanOrEqual ||
2422 oper == Operator.GreaterThan || oper == Operator.GreaterThanOrEqual){
2423 type = TypeManager.bool_type;
2428 if (OverloadResolve_PredefinedIntegral (ec)) {
2429 if (IsApplicable_String (ec, orig_left, orig_right, oper)) {
2430 Error_OperatorAmbiguous (loc, oper, l, r);
2433 } else if (OverloadResolve_PredefinedFloating (ec)) {
2434 if (IsConvertible (ec, orig_left, orig_right, TypeManager.decimal_type) ||
2435 IsApplicable_String (ec, orig_left, orig_right, oper)) {
2436 Error_OperatorAmbiguous (loc, oper, l, r);
2439 } else if (VerifyApplicable_Predefined (ec, TypeManager.decimal_type)) {
2440 if (IsApplicable_String (ec, orig_left, orig_right, oper)) {
2441 Error_OperatorAmbiguous (loc, oper, l, r);
2444 } else if (!OverloadResolve_PredefinedString (ec, oper)) {
2445 Error_OperatorCannotBeApplied ();
2449 if (oper == Operator.Equality ||
2450 oper == Operator.Inequality ||
2451 oper == Operator.LessThanOrEqual ||
2452 oper == Operator.LessThan ||
2453 oper == Operator.GreaterThanOrEqual ||
2454 oper == Operator.GreaterThan)
2455 type = TypeManager.bool_type;
2460 if (l == TypeManager.decimal_type || l == TypeManager.string_type || r == TypeManager.string_type) {
2462 if (r == TypeManager.string_type)
2464 MethodGroupExpr ops = (MethodGroupExpr) MemberLookup (
2465 ec.ContainerType, lookup, oper_names [(int) oper],
2466 MemberTypes.Method, AllBindingFlags, loc);
2467 ArrayList args = new ArrayList (2);
2468 args.Add (new Argument (left, Argument.AType.Expression));
2469 args.Add (new Argument (right, Argument.AType.Expression));
2470 ops = ops.OverloadResolve (ec, ref args, true, Location.Null);
2471 return new BinaryMethod (type, (MethodInfo)ops, args);
2477 Constant EnumLiftUp (Constant left, Constant right)
2480 case Operator.BitwiseOr:
2481 case Operator.BitwiseAnd:
2482 case Operator.ExclusiveOr:
2483 case Operator.Equality:
2484 case Operator.Inequality:
2485 case Operator.LessThan:
2486 case Operator.LessThanOrEqual:
2487 case Operator.GreaterThan:
2488 case Operator.GreaterThanOrEqual:
2489 if (left is EnumConstant)
2492 if (left.IsZeroInteger)
2493 return new EnumConstant (left, right.Type);
2497 case Operator.Addition:
2498 case Operator.Subtraction:
2501 case Operator.Multiply:
2502 case Operator.Division:
2503 case Operator.Modulus:
2504 case Operator.LeftShift:
2505 case Operator.RightShift:
2506 if (right is EnumConstant || left is EnumConstant)
2510 Error_OperatorCannotBeApplied ();
2514 public override Expression DoResolve (EmitContext ec)
2519 if ((oper == Operator.Subtraction) && (left is ParenthesizedExpression)) {
2520 left = ((ParenthesizedExpression) left).Expr;
2521 left = left.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.Type);
2525 if (left.eclass == ExprClass.Type) {
2526 Report.Error (75, loc, "To cast a negative value, you must enclose the value in parentheses");
2530 left = left.Resolve (ec);
2535 Constant lc = left as Constant;
2536 if (lc != null && lc.Type == TypeManager.bool_type &&
2537 ((oper == Operator.LogicalAnd && (bool)lc.GetValue () == false) ||
2538 (oper == Operator.LogicalOr && (bool)lc.GetValue () == true))) {
2540 // TODO: make a sense to resolve unreachable expression as we do for statement
2541 Report.Warning (429, 4, loc, "Unreachable expression code detected");
2545 right = right.Resolve (ec);
2549 eclass = ExprClass.Value;
2550 Constant rc = right as Constant;
2552 // The conversion rules are ignored in enum context but why
2553 if (!ec.InEnumContext && lc != null && rc != null && (TypeManager.IsEnumType (left.Type) || TypeManager.IsEnumType (right.Type))) {
2554 left = lc = EnumLiftUp (lc, rc);
2558 right = rc = EnumLiftUp (rc, lc);
2563 if (oper == Operator.BitwiseAnd) {
2564 if (rc != null && rc.IsZeroInteger) {
2565 return lc is EnumConstant ?
2566 new EnumConstant (rc, lc.Type):
2570 if (lc != null && lc.IsZeroInteger) {
2571 return rc is EnumConstant ?
2572 new EnumConstant (lc, rc.Type):
2576 else if (oper == Operator.BitwiseOr) {
2577 if (lc is EnumConstant &&
2578 rc != null && rc.IsZeroInteger)
2580 if (rc is EnumConstant &&
2581 lc != null && lc.IsZeroInteger)
2583 } else if (oper == Operator.LogicalAnd) {
2584 if (rc != null && rc.IsDefaultValue && rc.Type == TypeManager.bool_type)
2586 if (lc != null && lc.IsDefaultValue && lc.Type == TypeManager.bool_type)
2590 if (rc != null && lc != null){
2591 int prev_e = Report.Errors;
2592 Expression e = ConstantFold.BinaryFold (
2593 ec, oper, lc, rc, loc);
2594 if (e != null || Report.Errors != prev_e)
2599 if ((left is NullLiteral || left.Type.IsValueType) &&
2600 (right is NullLiteral || right.Type.IsValueType) &&
2601 !(left is NullLiteral && right is NullLiteral) &&
2602 (TypeManager.IsNullableType (left.Type) || TypeManager.IsNullableType (right.Type)))
2603 return new Nullable.LiftedBinaryOperator (oper, left, right, loc).Resolve (ec);
2606 // Comparison warnings
2607 if (oper == Operator.Equality || oper == Operator.Inequality ||
2608 oper == Operator.LessThanOrEqual || oper == Operator.LessThan ||
2609 oper == Operator.GreaterThanOrEqual || oper == Operator.GreaterThan){
2610 if (left.Equals (right)) {
2611 Report.Warning (1718, 3, loc, "A comparison made to same variable. Did you mean to compare something else?");
2613 CheckUselessComparison (lc, right.Type);
2614 CheckUselessComparison (rc, left.Type);
2617 return ResolveOperator (ec);
2620 public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
2625 private void CheckUselessComparison (Constant c, Type type)
2627 if (c == null || !IsTypeIntegral (type)
2628 || c is StringConstant
2629 || c is BoolConstant
2630 || c is FloatConstant
2631 || c is DoubleConstant
2632 || c is DecimalConstant
2638 if (c is ULongConstant) {
2639 ulong uvalue = ((ULongConstant) c).Value;
2640 if (uvalue > long.MaxValue) {
2641 if (type == TypeManager.byte_type ||
2642 type == TypeManager.sbyte_type ||
2643 type == TypeManager.short_type ||
2644 type == TypeManager.ushort_type ||
2645 type == TypeManager.int32_type ||
2646 type == TypeManager.uint32_type ||
2647 type == TypeManager.int64_type ||
2648 type == TypeManager.char_type)
2649 WarnUselessComparison (type);
2652 value = (long) uvalue;
2654 else if (c is ByteConstant)
2655 value = ((ByteConstant) c).Value;
2656 else if (c is SByteConstant)
2657 value = ((SByteConstant) c).Value;
2658 else if (c is ShortConstant)
2659 value = ((ShortConstant) c).Value;
2660 else if (c is UShortConstant)
2661 value = ((UShortConstant) c).Value;
2662 else if (c is IntConstant)
2663 value = ((IntConstant) c).Value;
2664 else if (c is UIntConstant)
2665 value = ((UIntConstant) c).Value;
2666 else if (c is LongConstant)
2667 value = ((LongConstant) c).Value;
2668 else if (c is CharConstant)
2669 value = ((CharConstant)c).Value;
2674 if (IsValueOutOfRange (value, type))
2675 WarnUselessComparison (type);
2678 private bool IsValueOutOfRange (long value, Type type)
2680 if (IsTypeUnsigned (type) && value < 0)
2682 return type == TypeManager.sbyte_type && (value >= 0x80 || value < -0x80) ||
2683 type == TypeManager.byte_type && value >= 0x100 ||
2684 type == TypeManager.short_type && (value >= 0x8000 || value < -0x8000) ||
2685 type == TypeManager.ushort_type && value >= 0x10000 ||
2686 type == TypeManager.int32_type && (value >= 0x80000000 || value < -0x80000000) ||
2687 type == TypeManager.uint32_type && value >= 0x100000000;
2690 private static bool IsTypeIntegral (Type type)
2692 return type == TypeManager.uint64_type ||
2693 type == TypeManager.int64_type ||
2694 type == TypeManager.uint32_type ||
2695 type == TypeManager.int32_type ||
2696 type == TypeManager.ushort_type ||
2697 type == TypeManager.short_type ||
2698 type == TypeManager.sbyte_type ||
2699 type == TypeManager.byte_type ||
2700 type == TypeManager.char_type;
2703 private static bool IsTypeUnsigned (Type type)
2705 return type == TypeManager.uint64_type ||
2706 type == TypeManager.uint32_type ||
2707 type == TypeManager.ushort_type ||
2708 type == TypeManager.byte_type ||
2709 type == TypeManager.char_type;
2712 private void WarnUselessComparison (Type type)
2714 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}'",
2715 TypeManager.CSharpName (type));
2719 /// EmitBranchable is called from Statement.EmitBoolExpression in the
2720 /// context of a conditional bool expression. This function will return
2721 /// false if it is was possible to use EmitBranchable, or true if it was.
2723 /// The expression's code is generated, and we will generate a branch to `target'
2724 /// if the resulting expression value is equal to isTrue
2726 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
2728 ILGenerator ig = ec.ig;
2731 // This is more complicated than it looks, but its just to avoid
2732 // duplicated tests: basically, we allow ==, !=, >, <, >= and <=
2733 // but on top of that we want for == and != to use a special path
2734 // if we are comparing against null
2736 if ((oper == Operator.Equality || oper == Operator.Inequality) && (left is Constant || right is Constant)) {
2737 bool my_on_true = oper == Operator.Inequality ? on_true : !on_true;
2740 // put the constant on the rhs, for simplicity
2742 if (left is Constant) {
2743 Expression swap = right;
2748 if (((Constant) right).IsZeroInteger) {
2751 ig.Emit (OpCodes.Brtrue, target);
2753 ig.Emit (OpCodes.Brfalse, target);
2756 } else if (right is BoolConstant) {
2758 if (my_on_true != ((BoolConstant) right).Value)
2759 ig.Emit (OpCodes.Brtrue, target);
2761 ig.Emit (OpCodes.Brfalse, target);
2766 } else if (oper == Operator.LogicalAnd) {
2769 Label tests_end = ig.DefineLabel ();
2771 left.EmitBranchable (ec, tests_end, false);
2772 right.EmitBranchable (ec, target, true);
2773 ig.MarkLabel (tests_end);
2776 // This optimizes code like this
2777 // if (true && i > 4)
2779 if (!(left is Constant))
2780 left.EmitBranchable (ec, target, false);
2782 if (!(right is Constant))
2783 right.EmitBranchable (ec, target, false);
2788 } else if (oper == Operator.LogicalOr){
2790 left.EmitBranchable (ec, target, true);
2791 right.EmitBranchable (ec, target, true);
2794 Label tests_end = ig.DefineLabel ();
2795 left.EmitBranchable (ec, tests_end, true);
2796 right.EmitBranchable (ec, target, false);
2797 ig.MarkLabel (tests_end);
2802 } else if (!(oper == Operator.LessThan || oper == Operator.GreaterThan ||
2803 oper == Operator.LessThanOrEqual || oper == Operator.GreaterThanOrEqual ||
2804 oper == Operator.Equality || oper == Operator.Inequality)) {
2805 base.EmitBranchable (ec, target, on_true);
2813 bool is_unsigned = IsUnsigned (t) || t == TypeManager.double_type || t == TypeManager.float_type;
2816 case Operator.Equality:
2818 ig.Emit (OpCodes.Beq, target);
2820 ig.Emit (OpCodes.Bne_Un, target);
2823 case Operator.Inequality:
2825 ig.Emit (OpCodes.Bne_Un, target);
2827 ig.Emit (OpCodes.Beq, target);
2830 case Operator.LessThan:
2833 ig.Emit (OpCodes.Blt_Un, target);
2835 ig.Emit (OpCodes.Blt, target);
2838 ig.Emit (OpCodes.Bge_Un, target);
2840 ig.Emit (OpCodes.Bge, target);
2843 case Operator.GreaterThan:
2846 ig.Emit (OpCodes.Bgt_Un, target);
2848 ig.Emit (OpCodes.Bgt, target);
2851 ig.Emit (OpCodes.Ble_Un, target);
2853 ig.Emit (OpCodes.Ble, target);
2856 case Operator.LessThanOrEqual:
2859 ig.Emit (OpCodes.Ble_Un, target);
2861 ig.Emit (OpCodes.Ble, target);
2864 ig.Emit (OpCodes.Bgt_Un, target);
2866 ig.Emit (OpCodes.Bgt, target);
2870 case Operator.GreaterThanOrEqual:
2873 ig.Emit (OpCodes.Bge_Un, target);
2875 ig.Emit (OpCodes.Bge, target);
2878 ig.Emit (OpCodes.Blt_Un, target);
2880 ig.Emit (OpCodes.Blt, target);
2883 Console.WriteLine (oper);
2884 throw new Exception ("what is THAT");
2888 public override void Emit (EmitContext ec)
2890 ILGenerator ig = ec.ig;
2895 // Handle short-circuit operators differently
2898 if (oper == Operator.LogicalAnd) {
2899 Label load_zero = ig.DefineLabel ();
2900 Label end = ig.DefineLabel ();
2902 left.EmitBranchable (ec, load_zero, false);
2904 ig.Emit (OpCodes.Br, end);
2906 ig.MarkLabel (load_zero);
2907 ig.Emit (OpCodes.Ldc_I4_0);
2910 } else if (oper == Operator.LogicalOr) {
2911 Label load_one = ig.DefineLabel ();
2912 Label end = ig.DefineLabel ();
2914 left.EmitBranchable (ec, load_one, true);
2916 ig.Emit (OpCodes.Br, end);
2918 ig.MarkLabel (load_one);
2919 ig.Emit (OpCodes.Ldc_I4_1);
2927 bool is_unsigned = IsUnsigned (left.Type);
2930 case Operator.Multiply:
2932 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2933 opcode = OpCodes.Mul_Ovf;
2934 else if (is_unsigned)
2935 opcode = OpCodes.Mul_Ovf_Un;
2937 opcode = OpCodes.Mul;
2939 opcode = OpCodes.Mul;
2943 case Operator.Division:
2945 opcode = OpCodes.Div_Un;
2947 opcode = OpCodes.Div;
2950 case Operator.Modulus:
2952 opcode = OpCodes.Rem_Un;
2954 opcode = OpCodes.Rem;
2957 case Operator.Addition:
2959 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2960 opcode = OpCodes.Add_Ovf;
2961 else if (is_unsigned)
2962 opcode = OpCodes.Add_Ovf_Un;
2964 opcode = OpCodes.Add;
2966 opcode = OpCodes.Add;
2969 case Operator.Subtraction:
2971 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2972 opcode = OpCodes.Sub_Ovf;
2973 else if (is_unsigned)
2974 opcode = OpCodes.Sub_Ovf_Un;
2976 opcode = OpCodes.Sub;
2978 opcode = OpCodes.Sub;
2981 case Operator.RightShift:
2983 opcode = OpCodes.Shr_Un;
2985 opcode = OpCodes.Shr;
2988 case Operator.LeftShift:
2989 opcode = OpCodes.Shl;
2992 case Operator.Equality:
2993 opcode = OpCodes.Ceq;
2996 case Operator.Inequality:
2997 ig.Emit (OpCodes.Ceq);
2998 ig.Emit (OpCodes.Ldc_I4_0);
3000 opcode = OpCodes.Ceq;
3003 case Operator.LessThan:
3005 opcode = OpCodes.Clt_Un;
3007 opcode = OpCodes.Clt;
3010 case Operator.GreaterThan:
3012 opcode = OpCodes.Cgt_Un;
3014 opcode = OpCodes.Cgt;
3017 case Operator.LessThanOrEqual:
3018 Type lt = left.Type;
3020 if (is_unsigned || (lt == TypeManager.double_type || lt == TypeManager.float_type))
3021 ig.Emit (OpCodes.Cgt_Un);
3023 ig.Emit (OpCodes.Cgt);
3024 ig.Emit (OpCodes.Ldc_I4_0);
3026 opcode = OpCodes.Ceq;
3029 case Operator.GreaterThanOrEqual:
3030 Type le = left.Type;
3032 if (is_unsigned || (le == TypeManager.double_type || le == TypeManager.float_type))
3033 ig.Emit (OpCodes.Clt_Un);
3035 ig.Emit (OpCodes.Clt);
3037 ig.Emit (OpCodes.Ldc_I4_0);
3039 opcode = OpCodes.Ceq;
3042 case Operator.BitwiseOr:
3043 opcode = OpCodes.Or;
3046 case Operator.BitwiseAnd:
3047 opcode = OpCodes.And;
3050 case Operator.ExclusiveOr:
3051 opcode = OpCodes.Xor;
3055 throw new Exception ("This should not happen: Operator = "
3056 + oper.ToString ());
3062 protected override void CloneTo (CloneContext clonectx, Expression t)
3064 Binary target = (Binary) t;
3066 target.left = left.Clone (clonectx);
3067 target.right = right.Clone (clonectx);
3070 public override Expression CreateExpressionTree (EmitContext ec)
3074 case Operator.Addition:
3076 method_name = "AddChecked";
3078 method_name = "Add";
3080 case Operator.BitwiseAnd:
3081 method_name = "And";
3083 case Operator.GreaterThan:
3084 method_name = "GreaterThan";
3086 case Operator.LessThan:
3087 method_name = "LessThan";
3089 case Operator.LogicalAnd:
3090 method_name = "AndAlso";
3093 case Operator.BitwiseOr:
3097 case Operator.LogicalOr:
3098 method_name = "OrElse";
3101 throw new InternalErrorException ("Unknown expression tree binary operator " + oper);
3104 ArrayList args = new ArrayList (2);
3105 args.Add (new Argument (left.CreateExpressionTree (ec)));
3106 args.Add (new Argument (right.CreateExpressionTree (ec)));
3107 return CreateExpressionFactoryCall (method_name, args);
3112 // Object created by Binary when the binary operator uses an method instead of being
3113 // a binary operation that maps to a CIL binary operation.
3115 public class BinaryMethod : Expression {
3116 public MethodBase method;
3117 public ArrayList Arguments;
3119 public BinaryMethod (Type t, MethodBase m, ArrayList args)
3124 eclass = ExprClass.Value;
3127 public override Expression DoResolve (EmitContext ec)
3132 public override void Emit (EmitContext ec)
3134 ILGenerator ig = ec.ig;
3136 Invocation.EmitArguments (ec, Arguments, false, null);
3138 if (method is MethodInfo)
3139 ig.Emit (OpCodes.Call, (MethodInfo) method);
3141 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3146 // Represents the operation a + b [+ c [+ d [+ ...]]], where a is a string
3147 // b, c, d... may be strings or objects.
3149 public class StringConcat : Expression {
3150 ArrayList arguments;
3152 public StringConcat (EmitContext ec, Location loc, Expression left, Expression right)
3155 type = TypeManager.string_type;
3156 eclass = ExprClass.Value;
3158 arguments = new ArrayList (2);
3163 public override Expression DoResolve (EmitContext ec)
3168 public void Append (EmitContext ec, Expression operand)
3173 StringConstant sc = operand as StringConstant;
3175 if (arguments.Count != 0) {
3176 Argument last_argument = (Argument) arguments [arguments.Count - 1];
3177 StringConstant last_expr_constant = last_argument.Expr as StringConstant;
3178 if (last_expr_constant != null) {
3179 last_argument.Expr = new StringConstant (
3180 last_expr_constant.Value + sc.Value, sc.Location);
3186 // Multiple (3+) concatenation are resolved as multiple StringConcat instances
3188 StringConcat concat_oper = operand as StringConcat;
3189 if (concat_oper != null) {
3190 arguments.AddRange (concat_oper.arguments);
3196 // Conversion to object
3198 if (operand.Type != TypeManager.string_type) {
3199 Expression expr = Convert.ImplicitConversion (ec, operand, TypeManager.object_type, loc);
3201 Binary.Error_OperatorCannotBeApplied (loc, "+", TypeManager.string_type, operand.Type);
3207 arguments.Add (new Argument (operand));
3210 Expression CreateConcatInvocation ()
3212 return new Invocation (
3213 new MemberAccess (new MemberAccess (new QualifiedAliasMember ("global", "System", loc), "String", loc), "Concat", loc),
3217 public override void Emit (EmitContext ec)
3219 Expression concat = CreateConcatInvocation ();
3220 concat = concat.Resolve (ec);
3227 // Object created with +/= on delegates
3229 public class BinaryDelegate : Expression {
3233 public BinaryDelegate (Type t, MethodInfo mi, ArrayList args)
3238 eclass = ExprClass.Value;
3241 public override Expression DoResolve (EmitContext ec)
3246 public override void Emit (EmitContext ec)
3248 ILGenerator ig = ec.ig;
3250 Invocation.EmitArguments (ec, args, false, null);
3252 ig.Emit (OpCodes.Call, (MethodInfo) method);
3253 ig.Emit (OpCodes.Castclass, type);
3256 public Expression Right {
3258 Argument arg = (Argument) args [1];
3263 public bool IsAddition {
3265 return method == TypeManager.delegate_combine_delegate_delegate;
3271 // User-defined conditional logical operator
3273 public class ConditionalLogicalOperator : Expression {
3274 Expression left, right;
3276 Expression op_true, op_false, op;
3277 LocalTemporary left_temp;
3279 public ConditionalLogicalOperator (bool is_and, Expression left, Expression right, Type t, Location loc)
3282 eclass = ExprClass.Value;
3286 this.is_and = is_and;
3289 protected void Error19 ()
3291 Binary.Error_OperatorCannotBeApplied (loc, is_and ? "&&" : "||", left.GetSignatureForError (), right.GetSignatureForError ());
3294 protected void Error218 ()
3296 Error (218, "The type ('" + TypeManager.CSharpName (type) + "') must contain " +
3297 "declarations of operator true and operator false");
3300 public override Expression DoResolve (EmitContext ec)
3302 MethodGroupExpr operator_group;
3304 operator_group = MethodLookup (ec.ContainerType, type, is_and ? "op_BitwiseAnd" : "op_BitwiseOr", loc) as MethodGroupExpr;
3305 if (operator_group == null) {
3310 left_temp = new LocalTemporary (type);
3312 ArrayList arguments = new ArrayList (2);
3313 arguments.Add (new Argument (left_temp, Argument.AType.Expression));
3314 arguments.Add (new Argument (right, Argument.AType.Expression));
3315 operator_group = operator_group.OverloadResolve (ec, ref arguments, false, loc);
3316 if (operator_group == null) {
3321 MethodInfo method = (MethodInfo)operator_group;
3322 if (method.ReturnType != type) {
3323 Report.Error (217, loc, "In order to be applicable as a short circuit operator a user-defined logical operator `{0}' " +
3324 "must have the same return type as the type of its 2 parameters", TypeManager.CSharpSignature (method));
3328 op = new UserOperatorCall (operator_group, arguments, null, loc);
3330 op_true = GetOperatorTrue (ec, left_temp, loc);
3331 op_false = GetOperatorFalse (ec, left_temp, loc);
3332 if ((op_true == null) || (op_false == null)) {
3340 public override void Emit (EmitContext ec)
3342 ILGenerator ig = ec.ig;
3343 Label false_target = ig.DefineLabel ();
3344 Label end_target = ig.DefineLabel ();
3347 left_temp.Store (ec);
3349 (is_and ? op_false : op_true).EmitBranchable (ec, false_target, false);
3350 left_temp.Emit (ec);
3351 ig.Emit (OpCodes.Br, end_target);
3352 ig.MarkLabel (false_target);
3354 ig.MarkLabel (end_target);
3356 // We release 'left_temp' here since 'op' may refer to it too
3357 left_temp.Release (ec);
3361 public class PointerArithmetic : Expression {
3362 Expression left, right;
3366 // We assume that `l' is always a pointer
3368 public PointerArithmetic (bool is_addition, Expression l, Expression r, Type t, Location loc)
3374 is_add = is_addition;
3377 public override Expression DoResolve (EmitContext ec)
3379 eclass = ExprClass.Variable;
3381 if (left.Type == TypeManager.void_ptr_type) {
3382 Error (242, "The operation in question is undefined on void pointers");
3389 public override void Emit (EmitContext ec)
3391 Type op_type = left.Type;
3392 ILGenerator ig = ec.ig;
3394 // It must be either array or fixed buffer
3395 Type element = TypeManager.HasElementType (op_type) ?
3396 element = TypeManager.GetElementType (op_type) :
3397 element = AttributeTester.GetFixedBuffer (((FieldExpr)left).FieldInfo).ElementType;
3399 int size = GetTypeSize (element);
3400 Type rtype = right.Type;
3402 if (rtype.IsPointer){
3404 // handle (pointer - pointer)
3408 ig.Emit (OpCodes.Sub);
3412 ig.Emit (OpCodes.Sizeof, element);
3414 IntLiteral.EmitInt (ig, size);
3415 ig.Emit (OpCodes.Div);
3417 ig.Emit (OpCodes.Conv_I8);
3420 // handle + and - on (pointer op int)
3423 ig.Emit (OpCodes.Conv_I);
3425 Constant right_const = right as Constant;
3426 if (right_const != null && size != 0) {
3427 Expression ex = ConstantFold.BinaryFold (ec, Binary.Operator.Multiply, new IntConstant (size, right.Location), right_const, loc);
3435 ig.Emit (OpCodes.Sizeof, element);
3437 IntLiteral.EmitInt (ig, size);
3438 if (rtype == TypeManager.int64_type)
3439 ig.Emit (OpCodes.Conv_I8);
3440 else if (rtype == TypeManager.uint64_type)
3441 ig.Emit (OpCodes.Conv_U8);
3442 ig.Emit (OpCodes.Mul);
3446 if (rtype == TypeManager.int64_type || rtype == TypeManager.uint64_type)
3447 ig.Emit (OpCodes.Conv_I);
3450 ig.Emit (OpCodes.Add);
3452 ig.Emit (OpCodes.Sub);
3458 /// Implements the ternary conditional operator (?:)
3460 public class Conditional : Expression {
3461 Expression expr, true_expr, false_expr;
3463 public Conditional (Expression expr, Expression true_expr, Expression false_expr)
3466 this.true_expr = true_expr;
3467 this.false_expr = false_expr;
3468 this.loc = expr.Location;
3471 public Expression Expr {
3477 public Expression TrueExpr {
3483 public Expression FalseExpr {
3489 public override Expression CreateExpressionTree (EmitContext ec)
3491 ArrayList args = new ArrayList (3);
3492 args.Add (new Argument (expr.CreateExpressionTree (ec)));
3493 args.Add (new Argument (true_expr.CreateExpressionTree (ec)));
3494 args.Add (new Argument (false_expr.CreateExpressionTree (ec)));
3495 return CreateExpressionFactoryCall ("Condition", args);
3498 public override Expression DoResolve (EmitContext ec)
3500 expr = expr.Resolve (ec);
3505 if (expr.Type != TypeManager.bool_type){
3506 expr = Expression.ResolveBoolean (
3513 Assign ass = expr as Assign;
3514 if (ass != null && ass.Source is Constant) {
3515 Report.Warning (665, 3, loc, "Assignment in conditional expression is always constant; did you mean to use == instead of = ?");
3518 true_expr = true_expr.Resolve (ec);
3519 false_expr = false_expr.Resolve (ec);
3521 if (true_expr == null || false_expr == null)
3524 eclass = ExprClass.Value;
3525 if (true_expr.Type == false_expr.Type) {
3526 type = true_expr.Type;
3527 if (type == TypeManager.null_type) {
3528 // TODO: probably will have to implement ConditionalConstant
3529 // to call method without return constant as well
3530 Report.Warning (-101, 1, loc, "Conditional expression will always return same value");
3535 Type true_type = true_expr.Type;
3536 Type false_type = false_expr.Type;
3539 // First, if an implicit conversion exists from true_expr
3540 // to false_expr, then the result type is of type false_expr.Type
3542 conv = Convert.ImplicitConversion (ec, true_expr, false_type, loc);
3545 // Check if both can convert implicitl to each other's type
3547 if (Convert.ImplicitConversion (ec, false_expr, true_type, loc) != null){
3549 "Can not compute type of conditional expression " +
3550 "as `" + TypeManager.CSharpName (true_expr.Type) +
3551 "' and `" + TypeManager.CSharpName (false_expr.Type) +
3552 "' convert implicitly to each other");
3557 } else if ((conv = Convert.ImplicitConversion(ec, false_expr, true_type,loc))!= null){
3561 Report.Error (173, loc, "Type of conditional expression cannot be determined because there is no implicit conversion between `{0}' and `{1}'",
3562 true_expr.GetSignatureForError (), false_expr.GetSignatureForError ());
3567 // Dead code optimalization
3568 if (expr is BoolConstant){
3569 BoolConstant bc = (BoolConstant) expr;
3571 Report.Warning (429, 4, bc.Value ? false_expr.Location : true_expr.Location, "Unreachable expression code detected");
3572 return bc.Value ? true_expr : false_expr;
3578 public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
3583 public override void Emit (EmitContext ec)
3585 ILGenerator ig = ec.ig;
3586 Label false_target = ig.DefineLabel ();
3587 Label end_target = ig.DefineLabel ();
3589 expr.EmitBranchable (ec, false_target, false);
3590 true_expr.Emit (ec);
3591 ig.Emit (OpCodes.Br, end_target);
3592 ig.MarkLabel (false_target);
3593 false_expr.Emit (ec);
3594 ig.MarkLabel (end_target);
3597 protected override void CloneTo (CloneContext clonectx, Expression t)
3599 Conditional target = (Conditional) t;
3601 target.expr = expr.Clone (clonectx);
3602 target.true_expr = true_expr.Clone (clonectx);
3603 target.false_expr = false_expr.Clone (clonectx);
3607 public abstract class VariableReference : Expression, IAssignMethod, IMemoryLocation {
3609 LocalTemporary temp;
3611 public abstract Variable Variable {
3615 public abstract bool IsRef {
3619 public override void Emit (EmitContext ec)
3625 // This method is used by parameters that are references, that are
3626 // being passed as references: we only want to pass the pointer (that
3627 // is already stored in the parameter, not the address of the pointer,
3628 // and not the value of the variable).
3630 public void EmitLoad (EmitContext ec)
3632 Report.Debug (64, "VARIABLE EMIT LOAD", this, Variable, type, loc);
3634 Variable.EmitInstance (ec);
3638 public void Emit (EmitContext ec, bool leave_copy)
3640 Report.Debug (64, "VARIABLE EMIT", this, Variable, type, IsRef, loc);
3646 // If we are a reference, we loaded on the stack a pointer
3647 // Now lets load the real value
3649 LoadFromPtr (ec.ig, type);
3653 ec.ig.Emit (OpCodes.Dup);
3655 if (IsRef || Variable.NeedsTemporary) {
3656 temp = new LocalTemporary (Type);
3662 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy,
3663 bool prepare_for_load)
3665 Report.Debug (64, "VARIABLE EMIT ASSIGN", this, Variable, type, IsRef,
3668 ILGenerator ig = ec.ig;
3669 prepared = prepare_for_load;
3671 Variable.EmitInstance (ec);
3672 if (prepare_for_load) {
3673 if (Variable.HasInstance)
3674 ig.Emit (OpCodes.Dup);
3682 // HACK: variable is already emitted when source is an initializer
3683 if (source is NewInitialize)
3687 ig.Emit (OpCodes.Dup);
3688 if (IsRef || Variable.NeedsTemporary) {
3689 temp = new LocalTemporary (Type);
3695 StoreFromPtr (ig, type);
3697 Variable.EmitAssign (ec);
3705 public void AddressOf (EmitContext ec, AddressOp mode)
3707 Variable.EmitInstance (ec);
3708 Variable.EmitAddressOf (ec);
3715 public class LocalVariableReference : VariableReference, IVariable {
3716 public readonly string Name;
3718 public LocalInfo local_info;
3722 public LocalVariableReference (Block block, string name, Location l)
3727 eclass = ExprClass.Variable;
3731 // Setting `is_readonly' to false will allow you to create a writable
3732 // reference to a read-only variable. This is used by foreach and using.
3734 public LocalVariableReference (Block block, string name, Location l,
3735 LocalInfo local_info, bool is_readonly)
3736 : this (block, name, l)
3738 this.local_info = local_info;
3739 this.is_readonly = is_readonly;
3742 public VariableInfo VariableInfo {
3743 get { return local_info.VariableInfo; }
3746 public override bool IsRef {
3747 get { return false; }
3750 public bool IsReadOnly {
3751 get { return is_readonly; }
3754 public bool VerifyAssigned (EmitContext ec)
3756 VariableInfo variable_info = local_info.VariableInfo;
3757 return variable_info == null || variable_info.IsAssigned (ec, loc);
3760 void ResolveLocalInfo ()
3762 if (local_info == null) {
3763 local_info = Block.GetLocalInfo (Name);
3764 type = local_info.VariableType;
3765 is_readonly = local_info.ReadOnly;
3769 protected Expression DoResolveBase (EmitContext ec)
3771 type = local_info.VariableType;
3773 Expression e = Block.GetConstantExpression (Name);
3775 return e.Resolve (ec);
3777 if (!VerifyAssigned (ec))
3781 // If we are referencing a variable from the external block
3782 // flag it for capturing
3784 if (ec.MustCaptureVariable (local_info)) {
3785 if (local_info.AddressTaken){
3786 AnonymousMethod.Error_AddressOfCapturedVar (local_info.Name, loc);
3790 if (!ec.IsInProbingMode)
3792 ScopeInfo scope = local_info.Block.CreateScopeInfo ();
3793 variable = scope.AddLocal (local_info);
3794 type = variable.Type;
3801 public override Expression DoResolve (EmitContext ec)
3803 ResolveLocalInfo ();
3804 local_info.Used = true;
3806 if (type == null && local_info.Type is VarExpr) {
3807 local_info.VariableType = TypeManager.object_type;
3808 Error_VariableIsUsedBeforeItIsDeclared (Name);
3812 return DoResolveBase (ec);
3815 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3817 ResolveLocalInfo ();
3820 if (right_side == EmptyExpression.OutAccess)
3821 local_info.Used = true;
3823 // Infer implicitly typed local variable
3825 VarExpr ve = local_info.Type as VarExpr;
3827 ve.DoResolveLValue (ec, right_side);
3828 type = local_info.VariableType = ve.Type;
3835 if (right_side == EmptyExpression.OutAccess) {
3836 code = 1657; msg = "Cannot pass `{0}' as a ref or out argument because it is a `{1}'";
3837 } else if (right_side == EmptyExpression.LValueMemberAccess) {
3838 code = 1654; msg = "Cannot assign to members of `{0}' because it is a `{1}'";
3839 } else if (right_side == EmptyExpression.LValueMemberOutAccess) {
3840 code = 1655; msg = "Cannot pass members of `{0}' as ref or out arguments because it is a `{1}'";
3842 code = 1656; msg = "Cannot assign to `{0}' because it is a `{1}'";
3844 Report.Error (code, loc, msg, Name, local_info.GetReadOnlyContext ());
3848 if (VariableInfo != null)
3849 VariableInfo.SetAssigned (ec);
3851 return DoResolveBase (ec);
3854 public bool VerifyFixed ()
3856 // A local Variable is always fixed.
3860 public override int GetHashCode ()
3862 return Name.GetHashCode ();
3865 public override bool Equals (object obj)
3867 LocalVariableReference lvr = obj as LocalVariableReference;
3871 return Name == lvr.Name && Block == lvr.Block;
3874 public override Variable Variable {
3875 get { return variable != null ? variable : local_info.Variable; }
3878 public override string ToString ()
3880 return String.Format ("{0} ({1}:{2})", GetType (), Name, loc);
3883 protected override void CloneTo (CloneContext clonectx, Expression t)
3885 LocalVariableReference target = (LocalVariableReference) t;
3887 target.Block = clonectx.LookupBlock (Block);
3888 if (local_info != null)
3889 target.local_info = clonectx.LookupVariable (local_info);
3894 /// This represents a reference to a parameter in the intermediate
3897 public class ParameterReference : VariableReference, IVariable {
3898 readonly ToplevelParameterInfo pi;
3899 readonly ToplevelBlock referenced;
3902 public bool is_ref, is_out;
3905 get { return is_out; }
3908 public override bool IsRef {
3909 get { return is_ref; }
3912 public string Name {
3913 get { return Parameter.Name; }
3916 public Parameter Parameter {
3917 get { return pi.Parameter; }
3920 public ParameterReference (ToplevelBlock referenced, ToplevelParameterInfo pi, Location loc)
3923 this.referenced = referenced;
3925 eclass = ExprClass.Variable;
3928 public VariableInfo VariableInfo {
3929 get { return pi.VariableInfo; }
3932 public override Variable Variable {
3933 get { return variable != null ? variable : Parameter.Variable; }
3936 public bool VerifyFixed ()
3938 // A parameter is fixed if it's a value parameter (i.e., no modifier like out, ref, param).
3939 return Parameter.ModFlags == Parameter.Modifier.NONE;
3942 public bool IsAssigned (EmitContext ec, Location loc)
3944 // HACK: Variables are not captured in probing mode
3945 if (ec.IsInProbingMode)
3948 if (!ec.DoFlowAnalysis || !is_out || ec.CurrentBranching.IsAssigned (VariableInfo))
3951 Report.Error (269, loc, "Use of unassigned out parameter `{0}'", Name);
3955 public bool IsFieldAssigned (EmitContext ec, string field_name, Location loc)
3957 if (!ec.DoFlowAnalysis || !is_out || ec.CurrentBranching.IsFieldAssigned (VariableInfo, field_name))
3960 Report.Error (170, loc, "Use of possibly unassigned field `{0}'", field_name);
3964 public void SetAssigned (EmitContext ec)
3966 if (is_out && ec.DoFlowAnalysis)
3967 ec.CurrentBranching.SetAssigned (VariableInfo);
3970 public void SetFieldAssigned (EmitContext ec, string field_name)
3972 if (is_out && ec.DoFlowAnalysis)
3973 ec.CurrentBranching.SetFieldAssigned (VariableInfo, field_name);
3976 protected bool DoResolveBase (EmitContext ec)
3978 Parameter par = Parameter;
3979 if (!par.Resolve (ec)) {
3983 type = par.ParameterType;
3984 Parameter.Modifier mod = par.ModFlags;
3985 is_ref = (mod & Parameter.Modifier.ISBYREF) != 0;
3986 is_out = (mod & Parameter.Modifier.OUT) == Parameter.Modifier.OUT;
3987 eclass = ExprClass.Variable;
3989 AnonymousContainer am = ec.CurrentAnonymousMethod;
3993 ToplevelBlock declared = pi.Block;
3994 if (is_ref && declared != referenced) {
3995 Report.Error (1628, Location,
3996 "Cannot use ref or out parameter `{0}' inside an " +
3997 "anonymous method block", par.Name);
4001 if (!am.IsIterator && declared == referenced)
4004 // Don't capture aruments when the probing is on
4005 if (!ec.IsInProbingMode) {
4006 ScopeInfo scope = declared.CreateScopeInfo ();
4007 variable = scope.AddParameter (par, pi.Index);
4008 type = variable.Type;
4013 public override int GetHashCode ()
4015 return Name.GetHashCode ();
4018 public override bool Equals (object obj)
4020 ParameterReference pr = obj as ParameterReference;
4024 return Name == pr.Name && referenced == pr.referenced;
4027 public override Expression CreateExpressionTree (EmitContext ec)
4029 return Parameter.ExpressionTreeVariableReference ();
4033 // Notice that for ref/out parameters, the type exposed is not the
4034 // same type exposed externally.
4037 // externally we expose "int&"
4038 // here we expose "int".
4040 // We record this in "is_ref". This means that the type system can treat
4041 // the type as it is expected, but when we generate the code, we generate
4042 // the alternate kind of code.
4044 public override Expression DoResolve (EmitContext ec)
4046 if (!DoResolveBase (ec))
4049 if (is_out && ec.DoFlowAnalysis &&
4050 (!ec.OmitStructFlowAnalysis || !VariableInfo.TypeInfo.IsStruct) && !IsAssigned (ec, loc))
4056 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4058 if (!DoResolveBase (ec))
4061 // HACK: parameters are not captured when probing is on
4062 if (!ec.IsInProbingMode)
4068 static public void EmitLdArg (ILGenerator ig, int x)
4072 case 0: ig.Emit (OpCodes.Ldarg_0); break;
4073 case 1: ig.Emit (OpCodes.Ldarg_1); break;
4074 case 2: ig.Emit (OpCodes.Ldarg_2); break;
4075 case 3: ig.Emit (OpCodes.Ldarg_3); break;
4076 default: ig.Emit (OpCodes.Ldarg_S, (byte) x); break;
4079 ig.Emit (OpCodes.Ldarg, x);
4082 public override string ToString ()
4084 return "ParameterReference[" + Name + "]";
4089 /// Used for arguments to New(), Invocation()
4091 public class Argument {
4092 public enum AType : byte {
4099 public static readonly Argument[] Empty = new Argument [0];
4101 public readonly AType ArgType;
4102 public Expression Expr;
4104 public Argument (Expression expr, AType type)
4107 this.ArgType = type;
4110 public Argument (Expression expr)
4113 this.ArgType = AType.Expression;
4118 if (ArgType == AType.Ref || ArgType == AType.Out)
4119 return TypeManager.GetReferenceType (Expr.Type);
4125 public Parameter.Modifier Modifier
4130 return Parameter.Modifier.OUT;
4133 return Parameter.Modifier.REF;
4136 return Parameter.Modifier.NONE;
4141 public string GetSignatureForError ()
4143 if (Expr.eclass == ExprClass.MethodGroup)
4144 return Expr.ExprClassName;
4146 return Expr.GetSignatureForError ();
4149 public bool ResolveMethodGroup (EmitContext ec)
4151 SimpleName sn = Expr as SimpleName;
4153 Expr = sn.GetMethodGroup ();
4155 // FIXME: csc doesn't report any error if you try to use `ref' or
4156 // `out' in a delegate creation expression.
4157 Expr = Expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
4164 public bool Resolve (EmitContext ec, Location loc)
4166 using (ec.With (EmitContext.Flags.DoFlowAnalysis, true)) {
4167 // Verify that the argument is readable
4168 if (ArgType != AType.Out)
4169 Expr = Expr.Resolve (ec);
4171 // Verify that the argument is writeable
4172 if (Expr != null && (ArgType == AType.Out || ArgType == AType.Ref))
4173 Expr = Expr.ResolveLValue (ec, EmptyExpression.OutAccess, loc);
4175 return Expr != null;
4179 public void Emit (EmitContext ec)
4181 if (ArgType != AType.Ref && ArgType != AType.Out) {
4186 AddressOp mode = AddressOp.Store;
4187 if (ArgType == AType.Ref)
4188 mode |= AddressOp.Load;
4190 IMemoryLocation ml = (IMemoryLocation) Expr;
4191 ParameterReference pr = ml as ParameterReference;
4194 // ParameterReferences might already be references, so we want
4195 // to pass just the value
4197 if (pr != null && pr.IsRef)
4200 ml.AddressOf (ec, mode);
4203 public Argument Clone (CloneContext clonectx)
4205 return new Argument (Expr.Clone (clonectx), ArgType);
4210 /// Invocation of methods or delegates.
4212 public class Invocation : ExpressionStatement {
4213 protected ArrayList Arguments;
4215 protected MethodGroupExpr mg;
4216 bool arguments_resolved;
4219 // arguments is an ArrayList, but we do not want to typecast,
4220 // as it might be null.
4222 public Invocation (Expression expr, ArrayList arguments)
4224 SimpleName sn = expr as SimpleName;
4226 this.expr = sn.GetMethodGroup ();
4230 Arguments = arguments;
4231 loc = expr.Location;
4234 public Invocation (Expression expr, ArrayList arguments, bool arguments_resolved)
4235 : this (expr, arguments)
4237 this.arguments_resolved = arguments_resolved;
4240 public static string FullMethodDesc (MethodBase mb)
4246 if (mb is MethodInfo) {
4247 sb = new StringBuilder (TypeManager.CSharpName (((MethodInfo) mb).ReturnType));
4251 sb = new StringBuilder ();
4253 sb.Append (TypeManager.CSharpSignature (mb));
4254 return sb.ToString ();
4257 public override Expression CreateExpressionTree (EmitContext ec)
4259 ArrayList args = new ArrayList (Arguments.Count + 3);
4261 args.Add (new Argument (mg.InstanceExpression.CreateExpressionTree (ec)));
4263 args.Add (new Argument (new NullConstant (loc).CreateExpressionTree (ec)));
4265 args.Add (new Argument (mg.CreateExpressionTree (ec)));
4266 foreach (Argument a in Arguments) {
4267 Expression e = a.Expr.CreateExpressionTree (ec);
4269 args.Add (new Argument (e));
4272 return CreateExpressionFactoryCall ("Call", args);
4275 public override Expression DoResolve (EmitContext ec)
4277 // Don't resolve already resolved expression
4278 if (eclass != ExprClass.Invalid)
4281 Expression expr_resolved = expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
4282 if (expr_resolved == null)
4285 mg = expr_resolved as MethodGroupExpr;
4287 Type expr_type = expr_resolved.Type;
4289 if (expr_type != null && TypeManager.IsDelegateType (expr_type)){
4290 return (new DelegateInvocation (
4291 expr_resolved, Arguments, loc)).Resolve (ec);
4294 expr_resolved.Error_UnexpectedKind (ResolveFlags.MethodGroup, loc);
4299 // Next, evaluate all the expressions in the argument list
4301 if (Arguments != null && !arguments_resolved) {
4302 for (int i = 0; i < Arguments.Count; ++i)
4304 if (!((Argument)Arguments[i]).Resolve(ec, loc))
4309 mg = DoResolveOverload (ec);
4313 MethodInfo method = (MethodInfo)mg;
4314 if (method != null) {
4315 type = TypeManager.TypeToCoreType (method.ReturnType);
4317 // TODO: this is a copy of mg.ResolveMemberAccess method
4318 Expression iexpr = mg.InstanceExpression;
4319 if (method.IsStatic) {
4320 if (iexpr == null ||
4321 iexpr is This || iexpr is EmptyExpression ||
4322 mg.IdenticalTypeName) {
4323 mg.InstanceExpression = null;
4325 MemberExpr.error176 (loc, mg.GetSignatureForError ());
4331 if (type.IsPointer){
4339 // Only base will allow this invocation to happen.
4341 if (mg.IsBase && method.IsAbstract){
4342 Error_CannotCallAbstractBase (TypeManager.CSharpSignature (method));
4346 if (Arguments == null && method.DeclaringType == TypeManager.object_type && method.Name == "Finalize") {
4348 Report.Error (250, loc, "Do not directly call your base class Finalize method. It is called automatically from your destructor");
4350 Report.Error (245, loc, "Destructors and object.Finalize cannot be called directly. Consider calling IDisposable.Dispose if available");
4354 if (IsSpecialMethodInvocation (method)) {
4358 if (mg.InstanceExpression != null)
4359 mg.InstanceExpression.CheckMarshalByRefAccess (ec);
4361 eclass = ExprClass.Value;
4365 protected virtual MethodGroupExpr DoResolveOverload (EmitContext ec)
4367 return mg.OverloadResolve (ec, ref Arguments, false, loc);
4370 bool IsSpecialMethodInvocation (MethodBase method)
4372 if (!TypeManager.IsSpecialMethod (method))
4375 Report.SymbolRelatedToPreviousError (method);
4376 Report.Error (571, loc, "`{0}': cannot explicitly call operator or accessor",
4377 TypeManager.CSharpSignature (method, true));
4383 /// Emits a list of resolved Arguments that are in the arguments
4386 /// The MethodBase argument might be null if the
4387 /// emission of the arguments is known not to contain
4388 /// a `params' field (for example in constructors or other routines
4389 /// that keep their arguments in this structure)
4391 /// if `dup_args' is true, a copy of the arguments will be left
4392 /// on the stack. If `dup_args' is true, you can specify `this_arg'
4393 /// which will be duplicated before any other args. Only EmitCall
4394 /// should be using this interface.
4396 public static void EmitArguments (EmitContext ec, ArrayList arguments, bool dup_args, LocalTemporary this_arg)
4398 if (arguments == null)
4401 int top = arguments.Count;
4402 LocalTemporary [] temps = null;
4404 if (dup_args && top != 0)
4405 temps = new LocalTemporary [top];
4407 int argument_index = 0;
4409 for (int i = 0; i < top; i++) {
4410 a = (Argument) arguments [argument_index++];
4413 ec.ig.Emit (OpCodes.Dup);
4414 (temps [i] = new LocalTemporary (a.Type)).Store (ec);
4419 if (this_arg != null)
4422 for (int i = 0; i < top; i ++) {
4423 temps [i].Emit (ec);
4424 temps [i].Release (ec);
4429 static Type[] GetVarargsTypes (MethodBase mb, ArrayList arguments)
4431 ParameterData pd = TypeManager.GetParameterData (mb);
4433 if (arguments == null)
4434 return new Type [0];
4436 Argument a = (Argument) arguments [pd.Count - 1];
4437 Arglist list = (Arglist) a.Expr;
4439 return list.ArgumentTypes;
4443 /// This checks the ConditionalAttribute on the method
4445 public static bool IsMethodExcluded (MethodBase method)
4447 if (method.IsConstructor)
4450 method = TypeManager.DropGenericMethodArguments (method);
4451 if (method.DeclaringType.Module == CodeGen.Module.Builder) {
4452 IMethodData md = TypeManager.GetMethod (method);
4454 return md.IsExcluded ();
4456 // For some methods (generated by delegate class) GetMethod returns null
4457 // because they are not included in builder_to_method table
4461 return AttributeTester.IsConditionalMethodExcluded (method);
4465 /// is_base tells whether we want to force the use of the `call'
4466 /// opcode instead of using callvirt. Call is required to call
4467 /// a specific method, while callvirt will always use the most
4468 /// recent method in the vtable.
4470 /// is_static tells whether this is an invocation on a static method
4472 /// instance_expr is an expression that represents the instance
4473 /// it must be non-null if is_static is false.
4475 /// method is the method to invoke.
4477 /// Arguments is the list of arguments to pass to the method or constructor.
4479 public static void EmitCall (EmitContext ec, bool is_base,
4480 Expression instance_expr,
4481 MethodBase method, ArrayList Arguments, Location loc)
4483 EmitCall (ec, is_base, instance_expr, method, Arguments, loc, false, false);
4486 // `dup_args' leaves an extra copy of the arguments on the stack
4487 // `omit_args' does not leave any arguments at all.
4488 // So, basically, you could make one call with `dup_args' set to true,
4489 // and then another with `omit_args' set to true, and the two calls
4490 // would have the same set of arguments. However, each argument would
4491 // only have been evaluated once.
4492 public static void EmitCall (EmitContext ec, bool is_base,
4493 Expression instance_expr,
4494 MethodBase method, ArrayList Arguments, Location loc,
4495 bool dup_args, bool omit_args)
4497 ILGenerator ig = ec.ig;
4498 bool struct_call = false;
4499 bool this_call = false;
4500 LocalTemporary this_arg = null;
4502 Type decl_type = method.DeclaringType;
4504 if (!RootContext.StdLib) {
4505 // Replace any calls to the system's System.Array type with calls to
4506 // the newly created one.
4507 if (method == TypeManager.system_int_array_get_length)
4508 method = TypeManager.int_array_get_length;
4509 else if (method == TypeManager.system_int_array_get_rank)
4510 method = TypeManager.int_array_get_rank;
4511 else if (method == TypeManager.system_object_array_clone)
4512 method = TypeManager.object_array_clone;
4513 else if (method == TypeManager.system_int_array_get_length_int)
4514 method = TypeManager.int_array_get_length_int;
4515 else if (method == TypeManager.system_int_array_get_lower_bound_int)
4516 method = TypeManager.int_array_get_lower_bound_int;
4517 else if (method == TypeManager.system_int_array_get_upper_bound_int)
4518 method = TypeManager.int_array_get_upper_bound_int;
4519 else if (method == TypeManager.system_void_array_copyto_array_int)
4520 method = TypeManager.void_array_copyto_array_int;
4523 if (!ec.IsInObsoleteScope) {
4525 // This checks ObsoleteAttribute on the method and on the declaring type
4527 ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (method);
4529 AttributeTester.Report_ObsoleteMessage (oa, TypeManager.CSharpSignature (method), loc);
4531 oa = AttributeTester.GetObsoleteAttribute (method.DeclaringType);
4533 AttributeTester.Report_ObsoleteMessage (oa, method.DeclaringType.FullName, loc);
4537 if (IsMethodExcluded (method))
4540 bool is_static = method.IsStatic;
4542 if (instance_expr == EmptyExpression.Null) {
4543 SimpleName.Error_ObjectRefRequired (ec, loc, TypeManager.CSharpSignature (method));
4547 this_call = instance_expr is This;
4548 if (decl_type.IsValueType || (!this_call && instance_expr.Type.IsValueType))
4552 // If this is ourselves, push "this"
4556 Type iexpr_type = instance_expr.Type;
4559 // Push the instance expression
4561 if (TypeManager.IsValueType (iexpr_type)) {
4563 // Special case: calls to a function declared in a
4564 // reference-type with a value-type argument need
4565 // to have their value boxed.
4566 if (decl_type.IsValueType ||
4567 TypeManager.IsGenericParameter (iexpr_type)) {
4569 // If the expression implements IMemoryLocation, then
4570 // we can optimize and use AddressOf on the
4573 // If not we have to use some temporary storage for
4575 if (instance_expr is IMemoryLocation) {
4576 ((IMemoryLocation)instance_expr).
4577 AddressOf (ec, AddressOp.LoadStore);
4579 LocalTemporary temp = new LocalTemporary (iexpr_type);
4580 instance_expr.Emit (ec);
4582 temp.AddressOf (ec, AddressOp.Load);
4585 // avoid the overhead of doing this all the time.
4587 t = TypeManager.GetReferenceType (iexpr_type);
4589 instance_expr.Emit (ec);
4590 ig.Emit (OpCodes.Box, instance_expr.Type);
4591 t = TypeManager.object_type;
4594 instance_expr.Emit (ec);
4595 t = instance_expr.Type;
4599 ig.Emit (OpCodes.Dup);
4600 if (Arguments != null && Arguments.Count != 0) {
4601 this_arg = new LocalTemporary (t);
4602 this_arg.Store (ec);
4609 EmitArguments (ec, Arguments, dup_args, this_arg);
4612 if ((instance_expr != null) && (instance_expr.Type.IsGenericParameter))
4613 ig.Emit (OpCodes.Constrained, instance_expr.Type);
4617 if (is_static || struct_call || is_base || (this_call && !method.IsVirtual))
4618 call_op = OpCodes.Call;
4620 call_op = OpCodes.Callvirt;
4622 if ((method.CallingConvention & CallingConventions.VarArgs) != 0) {
4623 Type[] varargs_types = GetVarargsTypes (method, Arguments);
4624 ig.EmitCall (call_op, (MethodInfo) method, varargs_types);
4631 // and DoFoo is not virtual, you can omit the callvirt,
4632 // because you don't need the null checking behavior.
4634 if (method is MethodInfo)
4635 ig.Emit (call_op, (MethodInfo) method);
4637 ig.Emit (call_op, (ConstructorInfo) method);
4640 public override void Emit (EmitContext ec)
4642 mg.EmitCall (ec, Arguments);
4645 public override void EmitStatement (EmitContext ec)
4650 // Pop the return value if there is one
4652 if (TypeManager.TypeToCoreType (type) != TypeManager.void_type)
4653 ec.ig.Emit (OpCodes.Pop);
4656 protected override void CloneTo (CloneContext clonectx, Expression t)
4658 Invocation target = (Invocation) t;
4660 if (Arguments != null) {
4661 target.Arguments = new ArrayList (Arguments.Count);
4662 foreach (Argument a in Arguments)
4663 target.Arguments.Add (a.Clone (clonectx));
4666 target.expr = expr.Clone (clonectx);
4670 public class InvocationOrCast : ExpressionStatement
4673 Expression argument;
4675 public InvocationOrCast (Expression expr, Expression argument)
4678 this.argument = argument;
4679 this.loc = expr.Location;
4682 public override Expression DoResolve (EmitContext ec)
4685 // First try to resolve it as a cast.
4687 TypeExpr te = expr.ResolveAsTypeTerminal (ec, true);
4688 if ((te != null) && (te.eclass == ExprClass.Type)) {
4689 Cast cast = new Cast (te, argument, loc);
4690 return cast.Resolve (ec);
4694 // This can either be a type or a delegate invocation.
4695 // Let's just resolve it and see what we'll get.
4697 expr = expr.Resolve (ec, ResolveFlags.Type | ResolveFlags.VariableOrValue);
4702 // Ok, so it's a Cast.
4704 if (expr.eclass == ExprClass.Type) {
4705 Cast cast = new Cast (new TypeExpression (expr.Type, loc), argument, loc);
4706 return cast.Resolve (ec);
4710 // It's a delegate invocation.
4712 if (!TypeManager.IsDelegateType (expr.Type)) {
4713 Error (149, "Method name expected");
4717 ArrayList args = new ArrayList ();
4718 args.Add (new Argument (argument, Argument.AType.Expression));
4719 DelegateInvocation invocation = new DelegateInvocation (expr, args, loc);
4720 return invocation.Resolve (ec);
4723 public override ExpressionStatement ResolveStatement (EmitContext ec)
4726 // First try to resolve it as a cast.
4728 TypeExpr te = expr.ResolveAsTypeTerminal (ec, true);
4729 if ((te != null) && (te.eclass == ExprClass.Type)) {
4730 Error_InvalidExpressionStatement ();
4735 // This can either be a type or a delegate invocation.
4736 // Let's just resolve it and see what we'll get.
4738 expr = expr.Resolve (ec, ResolveFlags.Type | ResolveFlags.VariableOrValue);
4739 if ((expr == null) || (expr.eclass == ExprClass.Type)) {
4740 Error_InvalidExpressionStatement ();
4745 // It's a delegate invocation.
4747 if (!TypeManager.IsDelegateType (expr.Type)) {
4748 Error (149, "Method name expected");
4752 ArrayList args = new ArrayList ();
4753 args.Add (new Argument (argument, Argument.AType.Expression));
4754 DelegateInvocation invocation = new DelegateInvocation (expr, args, loc);
4755 return invocation.ResolveStatement (ec);
4758 public override void Emit (EmitContext ec)
4760 throw new Exception ("Cannot happen");
4763 public override void EmitStatement (EmitContext ec)
4765 throw new Exception ("Cannot happen");
4768 protected override void CloneTo (CloneContext clonectx, Expression t)
4770 InvocationOrCast target = (InvocationOrCast) t;
4772 target.expr = expr.Clone (clonectx);
4773 target.argument = argument.Clone (clonectx);
4778 // This class is used to "disable" the code generation for the
4779 // temporary variable when initializing value types.
4781 class EmptyAddressOf : EmptyExpression, IMemoryLocation {
4782 public void AddressOf (EmitContext ec, AddressOp Mode)
4789 /// Implements the new expression
4791 public class New : ExpressionStatement, IMemoryLocation {
4792 ArrayList Arguments;
4795 // During bootstrap, it contains the RequestedType,
4796 // but if `type' is not null, it *might* contain a NewDelegate
4797 // (because of field multi-initialization)
4799 public Expression RequestedType;
4801 MethodGroupExpr method;
4804 // If set, the new expression is for a value_target, and
4805 // we will not leave anything on the stack.
4807 protected Expression value_target;
4808 protected bool value_target_set;
4809 bool is_type_parameter = false;
4811 public New (Expression requested_type, ArrayList arguments, Location l)
4813 RequestedType = requested_type;
4814 Arguments = arguments;
4818 public bool SetTargetVariable (Expression value)
4820 value_target = value;
4821 value_target_set = true;
4822 if (!(value_target is IMemoryLocation)){
4823 Error_UnexpectedKind (null, "variable", loc);
4830 // This function is used to disable the following code sequence for
4831 // value type initialization:
4833 // AddressOf (temporary)
4837 // Instead the provide will have provided us with the address on the
4838 // stack to store the results.
4840 static Expression MyEmptyExpression;
4842 public void DisableTemporaryValueType ()
4844 if (MyEmptyExpression == null)
4845 MyEmptyExpression = new EmptyAddressOf ();
4848 // To enable this, look into:
4849 // test-34 and test-89 and self bootstrapping.
4851 // For instance, we can avoid a copy by using `newobj'
4852 // instead of Call + Push-temp on value types.
4853 // value_target = MyEmptyExpression;
4858 /// Converts complex core type syntax like 'new int ()' to simple constant
4860 public static Constant Constantify (Type t)
4862 if (t == TypeManager.int32_type)
4863 return new IntConstant (0, Location.Null);
4864 if (t == TypeManager.uint32_type)
4865 return new UIntConstant (0, Location.Null);
4866 if (t == TypeManager.int64_type)
4867 return new LongConstant (0, Location.Null);
4868 if (t == TypeManager.uint64_type)
4869 return new ULongConstant (0, Location.Null);
4870 if (t == TypeManager.float_type)
4871 return new FloatConstant (0, Location.Null);
4872 if (t == TypeManager.double_type)
4873 return new DoubleConstant (0, Location.Null);
4874 if (t == TypeManager.short_type)
4875 return new ShortConstant (0, Location.Null);
4876 if (t == TypeManager.ushort_type)
4877 return new UShortConstant (0, Location.Null);
4878 if (t == TypeManager.sbyte_type)
4879 return new SByteConstant (0, Location.Null);
4880 if (t == TypeManager.byte_type)
4881 return new ByteConstant (0, Location.Null);
4882 if (t == TypeManager.char_type)
4883 return new CharConstant ('\0', Location.Null);
4884 if (t == TypeManager.bool_type)
4885 return new BoolConstant (false, Location.Null);
4886 if (t == TypeManager.decimal_type)
4887 return new DecimalConstant (0, Location.Null);
4888 if (TypeManager.IsEnumType (t))
4889 return new EnumConstant (Constantify (TypeManager.EnumToUnderlying (t)), t);
4895 // Checks whether the type is an interface that has the
4896 // [ComImport, CoClass] attributes and must be treated
4899 public Expression CheckComImport (EmitContext ec)
4901 if (!type.IsInterface)
4905 // Turn the call into:
4906 // (the-interface-stated) (new class-referenced-in-coclassattribute ())
4908 Type real_class = AttributeTester.GetCoClassAttribute (type);
4909 if (real_class == null)
4912 New proxy = new New (new TypeExpression (real_class, loc), Arguments, loc);
4913 Cast cast = new Cast (new TypeExpression (type, loc), proxy, loc);
4914 return cast.Resolve (ec);
4917 public override Expression DoResolve (EmitContext ec)
4920 // The New DoResolve might be called twice when initializing field
4921 // expressions (see EmitFieldInitializers, the call to
4922 // GetInitializerExpression will perform a resolve on the expression,
4923 // and later the assign will trigger another resolution
4925 // This leads to bugs (#37014)
4928 if (RequestedType is NewDelegate)
4929 return RequestedType;
4933 TypeExpr texpr = RequestedType.ResolveAsTypeTerminal (ec, false);
4939 if (type == TypeManager.void_type) {
4940 Error_VoidInvalidInTheContext (loc);
4944 if (type.IsPointer) {
4945 Report.Error (1919, loc, "Unsafe type `{0}' cannot be used in an object creation expression",
4946 TypeManager.CSharpName (type));
4950 if (Arguments == null) {
4951 Expression c = Constantify (type);
4956 if (TypeManager.IsDelegateType (type)) {
4957 RequestedType = (new NewDelegate (type, Arguments, loc)).Resolve (ec);
4958 if (RequestedType != null)
4959 if (!(RequestedType is DelegateCreation))
4960 throw new Exception ("NewDelegate.Resolve returned a non NewDelegate: " + RequestedType.GetType ());
4961 return RequestedType;
4965 if (type.IsGenericParameter) {
4966 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (type);
4968 if ((gc == null) || (!gc.HasConstructorConstraint && !gc.IsValueType)) {
4969 Error (304, String.Format (
4970 "Cannot create an instance of the " +
4971 "variable type '{0}' because it " +
4972 "doesn't have the new() constraint",
4977 if ((Arguments != null) && (Arguments.Count != 0)) {
4978 Error (417, String.Format (
4979 "`{0}': cannot provide arguments " +
4980 "when creating an instance of a " +
4981 "variable type.", type));
4985 is_type_parameter = true;
4986 eclass = ExprClass.Value;
4991 if (type.IsAbstract && type.IsSealed) {
4992 Report.SymbolRelatedToPreviousError (type);
4993 Report.Error (712, loc, "Cannot create an instance of the static class `{0}'", TypeManager.CSharpName (type));
4997 if (type.IsInterface || type.IsAbstract){
4998 if (!TypeManager.IsGenericType (type)) {
4999 RequestedType = CheckComImport (ec);
5000 if (RequestedType != null)
5001 return RequestedType;
5004 Report.SymbolRelatedToPreviousError (type);
5005 Report.Error (144, loc, "Cannot create an instance of the abstract class or interface `{0}'", TypeManager.CSharpName (type));
5009 bool is_struct = type.IsValueType;
5010 eclass = ExprClass.Value;
5013 // SRE returns a match for .ctor () on structs (the object constructor),
5014 // so we have to manually ignore it.
5016 if (is_struct && Arguments == null)
5019 // For member-lookup, treat 'new Foo (bar)' as call to 'foo.ctor (bar)', where 'foo' is of type 'Foo'.
5020 Expression ml = MemberLookupFinal (ec, type, type, ".ctor",
5021 MemberTypes.Constructor, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
5023 if (Arguments != null){
5024 foreach (Argument a in Arguments){
5025 if (!a.Resolve (ec, loc))
5033 method = ml as MethodGroupExpr;
5034 if (method == null) {
5035 ml.Error_UnexpectedKind (ec.DeclContainer, "method group", loc);
5039 method = method.OverloadResolve (ec, ref Arguments, false, loc);
5046 bool DoEmitTypeParameter (EmitContext ec)
5049 ILGenerator ig = ec.ig;
5050 // IMemoryLocation ml;
5052 MethodInfo ci = TypeManager.activator_create_instance.MakeGenericMethod (
5053 new Type [] { type });
5055 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (type);
5056 if (gc.HasReferenceTypeConstraint || gc.HasClassConstraint) {
5057 ig.Emit (OpCodes.Call, ci);
5061 // Allow DoEmit() to be called multiple times.
5062 // We need to create a new LocalTemporary each time since
5063 // you can't share LocalBuilders among ILGeneators.
5064 LocalTemporary temp = new LocalTemporary (type);
5066 Label label_activator = ig.DefineLabel ();
5067 Label label_end = ig.DefineLabel ();
5069 temp.AddressOf (ec, AddressOp.Store);
5070 ig.Emit (OpCodes.Initobj, type);
5073 ig.Emit (OpCodes.Box, type);
5074 ig.Emit (OpCodes.Brfalse, label_activator);
5076 temp.AddressOf (ec, AddressOp.Store);
5077 ig.Emit (OpCodes.Initobj, type);
5079 ig.Emit (OpCodes.Br, label_end);
5081 ig.MarkLabel (label_activator);
5083 ig.Emit (OpCodes.Call, ci);
5084 ig.MarkLabel (label_end);
5087 throw new InternalErrorException ();
5092 // This DoEmit can be invoked in two contexts:
5093 // * As a mechanism that will leave a value on the stack (new object)
5094 // * As one that wont (init struct)
5096 // You can control whether a value is required on the stack by passing
5097 // need_value_on_stack. The code *might* leave a value on the stack
5098 // so it must be popped manually
5100 // If we are dealing with a ValueType, we have a few
5101 // situations to deal with:
5103 // * The target is a ValueType, and we have been provided
5104 // the instance (this is easy, we are being assigned).
5106 // * The target of New is being passed as an argument,
5107 // to a boxing operation or a function that takes a
5110 // In this case, we need to create a temporary variable
5111 // that is the argument of New.
5113 // Returns whether a value is left on the stack
5115 bool DoEmit (EmitContext ec, bool need_value_on_stack)
5117 bool is_value_type = TypeManager.IsValueType (type);
5118 ILGenerator ig = ec.ig;
5123 // Allow DoEmit() to be called multiple times.
5124 // We need to create a new LocalTemporary each time since
5125 // you can't share LocalBuilders among ILGeneators.
5126 if (!value_target_set)
5127 value_target = new LocalTemporary (type);
5129 ml = (IMemoryLocation) value_target;
5130 ml.AddressOf (ec, AddressOp.Store);
5134 method.EmitArguments (ec, Arguments);
5138 ig.Emit (OpCodes.Initobj, type);
5140 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
5141 if (need_value_on_stack){
5142 value_target.Emit (ec);
5147 ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
5152 public override void Emit (EmitContext ec)
5154 if (is_type_parameter)
5155 DoEmitTypeParameter (ec);
5160 public override void EmitStatement (EmitContext ec)
5162 bool value_on_stack;
5164 if (is_type_parameter)
5165 value_on_stack = DoEmitTypeParameter (ec);
5167 value_on_stack = DoEmit (ec, false);
5170 ec.ig.Emit (OpCodes.Pop);
5174 public virtual bool HasInitializer {
5180 public void AddressOf (EmitContext ec, AddressOp Mode)
5182 if (is_type_parameter) {
5183 LocalTemporary temp = new LocalTemporary (type);
5184 DoEmitTypeParameter (ec);
5186 temp.AddressOf (ec, Mode);
5190 if (!type.IsValueType){
5192 // We throw an exception. So far, I believe we only need to support
5194 // foreach (int j in new StructType ())
5197 throw new Exception ("AddressOf should not be used for classes");
5200 if (!value_target_set)
5201 value_target = new LocalTemporary (type);
5202 IMemoryLocation ml = (IMemoryLocation) value_target;
5204 ml.AddressOf (ec, AddressOp.Store);
5205 if (method == null) {
5206 ec.ig.Emit (OpCodes.Initobj, type);
5208 method.EmitArguments (ec, Arguments);
5209 ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
5212 ((IMemoryLocation) value_target).AddressOf (ec, Mode);
5215 protected override void CloneTo (CloneContext clonectx, Expression t)
5217 New target = (New) t;
5219 target.RequestedType = RequestedType.Clone (clonectx);
5220 if (Arguments != null){
5221 target.Arguments = new ArrayList ();
5222 foreach (Argument a in Arguments){
5223 target.Arguments.Add (a.Clone (clonectx));
5230 /// 14.5.10.2: Represents an array creation expression.
5234 /// There are two possible scenarios here: one is an array creation
5235 /// expression that specifies the dimensions and optionally the
5236 /// initialization data and the other which does not need dimensions
5237 /// specified but where initialization data is mandatory.
5239 public class ArrayCreation : Expression {
5240 Expression requested_base_type;
5241 ArrayList initializers;
5244 // The list of Argument types.
5245 // This is used to construct the `newarray' or constructor signature
5247 protected ArrayList arguments;
5249 protected Type array_element_type;
5250 bool expect_initializers = false;
5251 int num_arguments = 0;
5252 protected int dimensions;
5253 protected readonly string rank;
5255 protected ArrayList array_data;
5259 // The number of constants in array initializers
5260 int const_initializers_count;
5261 bool only_constant_initializers;
5263 public ArrayCreation (Expression requested_base_type, ArrayList exprs, string rank, ArrayList initializers, Location l)
5265 this.requested_base_type = requested_base_type;
5266 this.initializers = initializers;
5270 arguments = new ArrayList ();
5272 foreach (Expression e in exprs) {
5273 arguments.Add (new Argument (e, Argument.AType.Expression));
5278 public ArrayCreation (Expression requested_base_type, string rank, ArrayList initializers, Location l)
5280 this.requested_base_type = requested_base_type;
5281 this.initializers = initializers;
5285 //this.rank = rank.Substring (0, rank.LastIndexOf ('['));
5287 //string tmp = rank.Substring (rank.LastIndexOf ('['));
5289 //dimensions = tmp.Length - 1;
5290 expect_initializers = true;
5293 public Expression FormArrayType (Expression base_type, int idx_count, string rank)
5295 StringBuilder sb = new StringBuilder (rank);
5298 for (int i = 1; i < idx_count; i++)
5303 return new ComposedCast (base_type, sb.ToString (), loc);
5306 void Error_IncorrectArrayInitializer ()
5308 Error (178, "Invalid rank specifier: expected `,' or `]'");
5311 protected override void Error_NegativeArrayIndex (Location loc)
5313 Report.Error (248, loc, "Cannot create an array with a negative size");
5316 bool CheckIndices (EmitContext ec, ArrayList probe, int idx, bool specified_dims)
5318 if (specified_dims) {
5319 Argument a = (Argument) arguments [idx];
5321 if (!a.Resolve (ec, loc))
5324 Constant c = a.Expr as Constant;
5326 c = c.ImplicitConversionRequired (TypeManager.int32_type, a.Expr.Location);
5330 Report.Error (150, a.Expr.Location, "A constant value is expected");
5334 int value = (int) c.GetValue ();
5336 if (value != probe.Count) {
5337 Error_IncorrectArrayInitializer ();
5341 bounds [idx] = value;
5344 int child_bounds = -1;
5345 only_constant_initializers = true;
5346 for (int i = 0; i < probe.Count; ++i) {
5347 object o = probe [i];
5348 if (o is ArrayList) {
5349 ArrayList sub_probe = o as ArrayList;
5350 int current_bounds = sub_probe.Count;
5352 if (child_bounds == -1)
5353 child_bounds = current_bounds;
5355 else if (child_bounds != current_bounds){
5356 Error_IncorrectArrayInitializer ();
5359 if (idx + 1 >= dimensions){
5360 Error (623, "Array initializers can only be used in a variable or field initializer. Try using a new expression instead");
5364 bool ret = CheckIndices (ec, sub_probe, idx + 1, specified_dims);
5368 if (child_bounds != -1){
5369 Error_IncorrectArrayInitializer ();
5373 Expression element = ResolveArrayElement (ec, (Expression) o);
5374 if (element == null)
5377 // Initializers with the default values can be ignored
5378 Constant c = element as Constant;
5380 if (c.IsDefaultInitializer (array_element_type)) {
5384 ++const_initializers_count;
5387 only_constant_initializers = false;
5390 array_data.Add (element);
5397 public override Expression CreateExpressionTree (EmitContext ec)
5399 if (dimensions != 1) {
5400 Report.Error (838, loc, "An expression tree cannot contain a multidimensional array initializer");
5404 ArrayList args = new ArrayList (array_data == null ? 1 : array_data.Count + 1);
5405 args.Add (new Argument (new TypeOf (new TypeExpression (array_element_type, loc), loc)));
5406 if (array_data != null) {
5407 foreach (Expression e in array_data)
5408 args.Add (new Argument (e.CreateExpressionTree (ec)));
5411 return CreateExpressionFactoryCall ("NewArrayInit", args);
5414 public void UpdateIndices ()
5417 for (ArrayList probe = initializers; probe != null;) {
5418 if (probe.Count > 0 && probe [0] is ArrayList) {
5419 Expression e = new IntConstant (probe.Count, Location.Null);
5420 arguments.Add (new Argument (e, Argument.AType.Expression));
5422 bounds [i++] = probe.Count;
5424 probe = (ArrayList) probe [0];
5427 Expression e = new IntConstant (probe.Count, Location.Null);
5428 arguments.Add (new Argument (e, Argument.AType.Expression));
5430 bounds [i++] = probe.Count;
5437 protected virtual Expression ResolveArrayElement (EmitContext ec, Expression element)
5439 element = element.Resolve (ec);
5440 if (element == null)
5443 return Convert.ImplicitConversionRequired (
5444 ec, element, array_element_type, loc);
5447 protected bool ResolveInitializers (EmitContext ec)
5449 if (initializers == null) {
5450 return !expect_initializers;
5454 // We use this to store all the date values in the order in which we
5455 // will need to store them in the byte blob later
5457 array_data = new ArrayList ();
5458 bounds = new System.Collections.Specialized.HybridDictionary ();
5460 if (arguments != null)
5461 return CheckIndices (ec, initializers, 0, true);
5463 arguments = new ArrayList ();
5465 if (!CheckIndices (ec, initializers, 0, false))
5474 // Resolved the type of the array
5476 bool ResolveArrayType (EmitContext ec)
5478 if (requested_base_type == null) {
5479 Report.Error (622, loc, "Can only use array initializer expressions to assign to array types. Try using a new expression instead");
5483 StringBuilder array_qualifier = new StringBuilder (rank);
5486 // `In the first form allocates an array instace of the type that results
5487 // from deleting each of the individual expression from the expression list'
5489 if (num_arguments > 0) {
5490 array_qualifier.Append ("[");
5491 for (int i = num_arguments-1; i > 0; i--)
5492 array_qualifier.Append (",");
5493 array_qualifier.Append ("]");
5499 TypeExpr array_type_expr;
5500 array_type_expr = new ComposedCast (requested_base_type, array_qualifier.ToString (), loc);
5501 array_type_expr = array_type_expr.ResolveAsTypeTerminal (ec, false);
5502 if (array_type_expr == null)
5505 type = array_type_expr.Type;
5506 array_element_type = TypeManager.GetElementType (type);
5507 dimensions = type.GetArrayRank ();
5512 public override Expression DoResolve (EmitContext ec)
5517 if (!ResolveArrayType (ec))
5520 if ((array_element_type.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute) {
5521 Report.Error (719, loc, "`{0}': array elements cannot be of static type",
5522 TypeManager.CSharpName (array_element_type));
5526 // First step is to validate the initializers and fill
5527 // in any missing bits
5529 if (!ResolveInitializers (ec))
5532 if (arguments.Count != dimensions) {
5533 Error_IncorrectArrayInitializer ();
5536 foreach (Argument a in arguments){
5537 if (!a.Resolve (ec, loc))
5540 a.Expr = ConvertExpressionToArrayIndex (ec, a.Expr);
5543 eclass = ExprClass.Value;
5547 MethodInfo GetArrayMethod (int arguments)
5549 ModuleBuilder mb = CodeGen.Module.Builder;
5551 Type[] arg_types = new Type[arguments];
5552 for (int i = 0; i < arguments; i++)
5553 arg_types[i] = TypeManager.int32_type;
5555 MethodInfo mi = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
5559 Report.Error (-6, "New invocation: Can not find a constructor for " +
5560 "this argument list");
5567 byte [] MakeByteBlob ()
5572 int count = array_data.Count;
5574 if (array_element_type.IsEnum)
5575 array_element_type = TypeManager.EnumToUnderlying (array_element_type);
5577 factor = GetTypeSize (array_element_type);
5579 throw new Exception ("unrecognized type in MakeByteBlob: " + array_element_type);
5581 data = new byte [(count * factor + 3) & ~3];
5584 for (int i = 0; i < count; ++i) {
5585 object v = array_data [i];
5587 if (v is EnumConstant)
5588 v = ((EnumConstant) v).Child;
5590 if (v is Constant && !(v is StringConstant))
5591 v = ((Constant) v).GetValue ();
5597 if (array_element_type == TypeManager.int64_type){
5598 if (!(v is Expression)){
5599 long val = (long) v;
5601 for (int j = 0; j < factor; ++j) {
5602 data [idx + j] = (byte) (val & 0xFF);
5606 } else if (array_element_type == TypeManager.uint64_type){
5607 if (!(v is Expression)){
5608 ulong val = (ulong) v;
5610 for (int j = 0; j < factor; ++j) {
5611 data [idx + j] = (byte) (val & 0xFF);
5615 } else if (array_element_type == TypeManager.float_type) {
5616 if (!(v is Expression)){
5617 element = BitConverter.GetBytes ((float) v);
5619 for (int j = 0; j < factor; ++j)
5620 data [idx + j] = element [j];
5621 if (!BitConverter.IsLittleEndian)
5622 System.Array.Reverse (data, idx, 4);
5624 } else if (array_element_type == TypeManager.double_type) {
5625 if (!(v is Expression)){
5626 element = BitConverter.GetBytes ((double) v);
5628 for (int j = 0; j < factor; ++j)
5629 data [idx + j] = element [j];
5631 // FIXME: Handle the ARM float format.
5632 if (!BitConverter.IsLittleEndian)
5633 System.Array.Reverse (data, idx, 8);
5635 } else if (array_element_type == TypeManager.char_type){
5636 if (!(v is Expression)){
5637 int val = (int) ((char) v);
5639 data [idx] = (byte) (val & 0xff);
5640 data [idx+1] = (byte) (val >> 8);
5642 } else if (array_element_type == TypeManager.short_type){
5643 if (!(v is Expression)){
5644 int val = (int) ((short) v);
5646 data [idx] = (byte) (val & 0xff);
5647 data [idx+1] = (byte) (val >> 8);
5649 } else if (array_element_type == TypeManager.ushort_type){
5650 if (!(v is Expression)){
5651 int val = (int) ((ushort) v);
5653 data [idx] = (byte) (val & 0xff);
5654 data [idx+1] = (byte) (val >> 8);
5656 } else if (array_element_type == TypeManager.int32_type) {
5657 if (!(v is Expression)){
5660 data [idx] = (byte) (val & 0xff);
5661 data [idx+1] = (byte) ((val >> 8) & 0xff);
5662 data [idx+2] = (byte) ((val >> 16) & 0xff);
5663 data [idx+3] = (byte) (val >> 24);
5665 } else if (array_element_type == TypeManager.uint32_type) {
5666 if (!(v is Expression)){
5667 uint val = (uint) v;
5669 data [idx] = (byte) (val & 0xff);
5670 data [idx+1] = (byte) ((val >> 8) & 0xff);
5671 data [idx+2] = (byte) ((val >> 16) & 0xff);
5672 data [idx+3] = (byte) (val >> 24);
5674 } else if (array_element_type == TypeManager.sbyte_type) {
5675 if (!(v is Expression)){
5676 sbyte val = (sbyte) v;
5677 data [idx] = (byte) val;
5679 } else if (array_element_type == TypeManager.byte_type) {
5680 if (!(v is Expression)){
5681 byte val = (byte) v;
5682 data [idx] = (byte) val;
5684 } else if (array_element_type == TypeManager.bool_type) {
5685 if (!(v is Expression)){
5686 bool val = (bool) v;
5687 data [idx] = (byte) (val ? 1 : 0);
5689 } else if (array_element_type == TypeManager.decimal_type){
5690 if (!(v is Expression)){
5691 int [] bits = Decimal.GetBits ((decimal) v);
5694 // FIXME: For some reason, this doesn't work on the MS runtime.
5695 int [] nbits = new int [4];
5696 nbits [0] = bits [3];
5697 nbits [1] = bits [2];
5698 nbits [2] = bits [0];
5699 nbits [3] = bits [1];
5701 for (int j = 0; j < 4; j++){
5702 data [p++] = (byte) (nbits [j] & 0xff);
5703 data [p++] = (byte) ((nbits [j] >> 8) & 0xff);
5704 data [p++] = (byte) ((nbits [j] >> 16) & 0xff);
5705 data [p++] = (byte) (nbits [j] >> 24);
5709 throw new Exception ("Unrecognized type in MakeByteBlob: " + array_element_type);
5718 // Emits the initializers for the array
5720 void EmitStaticInitializers (EmitContext ec)
5723 // First, the static data
5726 ILGenerator ig = ec.ig;
5728 byte [] data = MakeByteBlob ();
5730 fb = RootContext.MakeStaticData (data);
5732 ig.Emit (OpCodes.Dup);
5733 ig.Emit (OpCodes.Ldtoken, fb);
5734 ig.Emit (OpCodes.Call,
5735 TypeManager.void_initializearray_array_fieldhandle);
5739 // Emits pieces of the array that can not be computed at compile
5740 // time (variables and string locations).
5742 // This always expect the top value on the stack to be the array
5744 void EmitDynamicInitializers (EmitContext ec, bool emitConstants)
5746 ILGenerator ig = ec.ig;
5747 int dims = bounds.Count;
5748 int [] current_pos = new int [dims];
5750 MethodInfo set = null;
5753 Type [] args = new Type [dims + 1];
5755 for (int j = 0; j < dims; j++)
5756 args [j] = TypeManager.int32_type;
5757 args [dims] = array_element_type;
5759 set = CodeGen.Module.Builder.GetArrayMethod (
5761 CallingConventions.HasThis | CallingConventions.Standard,
5762 TypeManager.void_type, args);
5765 for (int i = 0; i < array_data.Count; i++){
5767 Expression e = (Expression)array_data [i];
5769 // Constant can be initialized via StaticInitializer
5770 if (e != null && !(!emitConstants && e is Constant)) {
5771 Type etype = e.Type;
5773 ig.Emit (OpCodes.Dup);
5775 for (int idx = 0; idx < dims; idx++)
5776 IntConstant.EmitInt (ig, current_pos [idx]);
5779 // If we are dealing with a struct, get the
5780 // address of it, so we can store it.
5782 if ((dims == 1) && etype.IsValueType &&
5783 (!TypeManager.IsBuiltinOrEnum (etype) ||
5784 etype == TypeManager.decimal_type)) {
5789 // Let new know that we are providing
5790 // the address where to store the results
5792 n.DisableTemporaryValueType ();
5795 ig.Emit (OpCodes.Ldelema, etype);
5801 bool is_stobj, has_type_arg;
5802 OpCode op = ArrayAccess.GetStoreOpcode (etype, out is_stobj, out has_type_arg);
5804 ig.Emit (OpCodes.Stobj, etype);
5805 else if (has_type_arg)
5806 ig.Emit (op, etype);
5810 ig.Emit (OpCodes.Call, set);
5817 for (int j = dims - 1; j >= 0; j--){
5819 if (current_pos [j] < (int) bounds [j])
5821 current_pos [j] = 0;
5826 public override void Emit (EmitContext ec)
5828 ILGenerator ig = ec.ig;
5830 foreach (Argument a in arguments)
5833 if (arguments.Count == 1)
5834 ig.Emit (OpCodes.Newarr, array_element_type);
5836 ig.Emit (OpCodes.Newobj, GetArrayMethod (arguments.Count));
5839 if (initializers == null)
5842 // Emit static initializer for arrays which have contain more than 4 items and
5843 // the static initializer will initialize at least 25% of array values.
5844 // NOTE: const_initializers_count does not contain default constant values.
5845 if (const_initializers_count >= 4 && const_initializers_count * 4 > (array_data.Count) &&
5846 TypeManager.IsPrimitiveType (array_element_type)) {
5847 EmitStaticInitializers (ec);
5849 if (!only_constant_initializers)
5850 EmitDynamicInitializers (ec, false);
5852 EmitDynamicInitializers (ec, true);
5856 public override bool GetAttributableValue (Type value_type, out object value)
5858 if (arguments.Count != 1) {
5859 // Report.Error (-211, Location, "attribute can not encode multi-dimensional arrays");
5860 return base.GetAttributableValue (null, out value);
5863 if (array_data == null) {
5864 Constant c = (Constant)((Argument)arguments [0]).Expr;
5865 if (c.IsDefaultValue) {
5866 value = Array.CreateInstance (array_element_type, 0);
5869 // Report.Error (-212, Location, "array should be initialized when passing it to an attribute");
5870 return base.GetAttributableValue (null, out value);
5873 Array ret = Array.CreateInstance (array_element_type, array_data.Count);
5874 object element_value;
5875 for (int i = 0; i < ret.Length; ++i)
5877 Expression e = (Expression)array_data [i];
5879 // Is null when an initializer is optimized (value == predefined value)
5883 if (!e.GetAttributableValue (array_element_type, out element_value)) {
5887 ret.SetValue (element_value, i);
5893 protected override void CloneTo (CloneContext clonectx, Expression t)
5895 ArrayCreation target = (ArrayCreation) t;
5897 if (requested_base_type != null)
5898 target.requested_base_type = requested_base_type.Clone (clonectx);
5900 if (arguments != null){
5901 target.arguments = new ArrayList (arguments.Count);
5902 foreach (Argument a in arguments)
5903 target.arguments.Add (a.Clone (clonectx));
5906 if (initializers != null){
5907 target.initializers = new ArrayList (initializers.Count);
5908 foreach (Expression initializer in initializers)
5909 target.initializers.Add (initializer.Clone (clonectx));
5915 // Represents an implicitly typed array epxression
5917 public class ImplicitlyTypedArrayCreation : ArrayCreation
5919 public ImplicitlyTypedArrayCreation (string rank, ArrayList initializers, Location loc)
5920 : base (null, rank, initializers, loc)
5922 if (RootContext.Version <= LanguageVersion.ISO_2)
5923 Report.FeatureIsNotAvailable (loc, "implicitly typed arrays");
5925 if (rank.Length > 2) {
5926 while (rank [++dimensions] == ',');
5932 public override Expression DoResolve (EmitContext ec)
5937 if (!ResolveInitializers (ec))
5940 if (array_element_type == null || array_element_type == TypeManager.null_type ||
5941 array_element_type == TypeManager.void_type || array_element_type == TypeManager.anonymous_method_type ||
5942 arguments.Count != dimensions) {
5943 Report.Error (826, loc, "The type of an implicitly typed array cannot be inferred from the initializer. Try specifying array type explicitly");
5948 // At this point we found common base type for all initializer elements
5949 // but we have to be sure that all static initializer elements are of
5952 UnifyInitializerElement (ec);
5954 type = TypeManager.GetConstructedType (array_element_type, rank);
5955 eclass = ExprClass.Value;
5960 // Converts static initializer only
5962 void UnifyInitializerElement (EmitContext ec)
5964 for (int i = 0; i < array_data.Count; ++i) {
5965 Expression e = (Expression)array_data[i];
5967 array_data [i] = Convert.ImplicitConversionStandard (ec, e, array_element_type, Location.Null);
5971 protected override Expression ResolveArrayElement (EmitContext ec, Expression element)
5973 element = element.Resolve (ec);
5974 if (element == null)
5977 if (array_element_type == null) {
5978 array_element_type = element.Type;
5982 if (Convert.ImplicitStandardConversionExists (element, array_element_type)) {
5986 if (Convert.ImplicitStandardConversionExists (new TypeExpression (array_element_type, loc), element.Type)) {
5987 array_element_type = element.Type;
5991 element.Error_ValueCannotBeConverted (ec, element.Location, array_element_type, false);
5996 public sealed class CompilerGeneratedThis : This
5998 public static This Instance = new CompilerGeneratedThis ();
6000 private CompilerGeneratedThis ()
6001 : base (Location.Null)
6005 public override Expression DoResolve (EmitContext ec)
6007 eclass = ExprClass.Variable;
6008 type = ec.ContainerType;
6009 variable = new SimpleThis (type);
6015 /// Represents the `this' construct
6018 public class This : VariableReference, IVariable
6021 VariableInfo variable_info;
6022 protected Variable variable;
6025 public This (Block block, Location loc)
6031 public This (Location loc)
6036 public VariableInfo VariableInfo {
6037 get { return variable_info; }
6040 public bool VerifyFixed ()
6042 return !TypeManager.IsValueType (Type);
6045 public override bool IsRef {
6046 get { return is_struct; }
6049 public override Variable Variable {
6050 get { return variable; }
6053 public bool ResolveBase (EmitContext ec)
6055 eclass = ExprClass.Variable;
6057 if (ec.TypeContainer.CurrentType != null)
6058 type = ec.TypeContainer.CurrentType;
6060 type = ec.ContainerType;
6062 is_struct = ec.TypeContainer is Struct;
6065 Error (26, "Keyword `this' is not valid in a static property, " +
6066 "static method, or static field initializer");
6070 if (block != null) {
6071 if (block.Toplevel.ThisVariable != null)
6072 variable_info = block.Toplevel.ThisVariable.VariableInfo;
6074 AnonymousContainer am = ec.CurrentAnonymousMethod;
6075 if (is_struct && (am != null) && !am.IsIterator) {
6076 Report.Error (1673, loc, "Anonymous methods inside structs " +
6077 "cannot access instance members of `this'. " +
6078 "Consider copying `this' to a local variable " +
6079 "outside the anonymous method and using the " +
6083 RootScopeInfo host = block.Toplevel.RootScope;
6084 if ((host != null) && !ec.IsConstructor &&
6085 (!is_struct || host.IsIterator)) {
6086 variable = host.CaptureThis ();
6087 type = variable.Type;
6092 if (variable == null)
6093 variable = new SimpleThis (type);
6099 // Called from Invocation to check if the invocation is correct
6101 public override void CheckMarshalByRefAccess (EmitContext ec)
6103 if ((variable_info != null) && !(type.IsValueType && ec.OmitStructFlowAnalysis) &&
6104 !variable_info.IsAssigned (ec)) {
6105 Error (188, "The `this' object cannot be used before all of its " +
6106 "fields are assigned to");
6107 variable_info.SetAssigned (ec);
6111 public override Expression CreateExpressionTree (EmitContext ec)
6113 ArrayList args = new ArrayList (2);
6114 args.Add (new Argument (this));
6115 args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
6116 return CreateExpressionFactoryCall ("Constant", args);
6119 public override Expression DoResolve (EmitContext ec)
6121 if (!ResolveBase (ec))
6125 if (ec.IsInFieldInitializer) {
6126 Error (27, "Keyword `this' is not available in the current context");
6133 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
6135 if (!ResolveBase (ec))
6138 if (variable_info != null)
6139 variable_info.SetAssigned (ec);
6141 if (ec.TypeContainer is Class){
6142 Error (1604, "Cannot assign to 'this' because it is read-only");
6148 public override int GetHashCode()
6150 return block.GetHashCode ();
6153 public override bool Equals (object obj)
6155 This t = obj as This;
6159 return block == t.block;
6162 protected class SimpleThis : Variable
6166 public SimpleThis (Type type)
6171 public override Type Type {
6172 get { return type; }
6175 public override bool HasInstance {
6176 get { return false; }
6179 public override bool NeedsTemporary {
6180 get { return false; }
6183 public override void EmitInstance (EmitContext ec)
6188 public override void Emit (EmitContext ec)
6190 ec.ig.Emit (OpCodes.Ldarg_0);
6193 public override void EmitAssign (EmitContext ec)
6195 throw new InvalidOperationException ();
6198 public override void EmitAddressOf (EmitContext ec)
6200 ec.ig.Emit (OpCodes.Ldarg_0);
6204 protected override void CloneTo (CloneContext clonectx, Expression t)
6206 This target = (This) t;
6208 target.block = clonectx.LookupBlock (block);
6213 /// Represents the `__arglist' construct
6215 public class ArglistAccess : Expression
6217 public ArglistAccess (Location loc)
6222 public override Expression DoResolve (EmitContext ec)
6224 eclass = ExprClass.Variable;
6225 type = TypeManager.runtime_argument_handle_type;
6227 if (ec.IsInFieldInitializer || !ec.CurrentBlock.Toplevel.HasVarargs)
6229 Error (190, "The __arglist construct is valid only within " +
6230 "a variable argument method");
6237 public override void Emit (EmitContext ec)
6239 ec.ig.Emit (OpCodes.Arglist);
6242 protected override void CloneTo (CloneContext clonectx, Expression target)
6249 /// Represents the `__arglist (....)' construct
6251 public class Arglist : Expression
6253 Argument[] Arguments;
6255 public Arglist (Location loc)
6256 : this (Argument.Empty, loc)
6260 public Arglist (Argument[] args, Location l)
6266 public Type[] ArgumentTypes {
6268 Type[] retval = new Type [Arguments.Length];
6269 for (int i = 0; i < Arguments.Length; i++)
6270 retval [i] = Arguments [i].Type;
6275 public override Expression CreateExpressionTree (EmitContext ec)
6277 Report.Error (1952, loc, "An expression tree cannot contain a method with variable arguments");
6281 public override Expression DoResolve (EmitContext ec)
6283 eclass = ExprClass.Variable;
6284 type = TypeManager.runtime_argument_handle_type;
6286 foreach (Argument arg in Arguments) {
6287 if (!arg.Resolve (ec, loc))
6294 public override void Emit (EmitContext ec)
6296 foreach (Argument arg in Arguments)
6300 protected override void CloneTo (CloneContext clonectx, Expression t)
6302 Arglist target = (Arglist) t;
6304 target.Arguments = new Argument [Arguments.Length];
6305 for (int i = 0; i < Arguments.Length; i++)
6306 target.Arguments [i] = Arguments [i].Clone (clonectx);
6311 // This produces the value that renders an instance, used by the iterators code
6313 public class ProxyInstance : Expression, IMemoryLocation {
6314 public override Expression DoResolve (EmitContext ec)
6316 eclass = ExprClass.Variable;
6317 type = ec.ContainerType;
6321 public override void Emit (EmitContext ec)
6323 ec.ig.Emit (OpCodes.Ldarg_0);
6327 public void AddressOf (EmitContext ec, AddressOp mode)
6329 ec.ig.Emit (OpCodes.Ldarg_0);
6334 /// Implements the typeof operator
6336 public class TypeOf : Expression {
6337 Expression QueriedType;
6338 protected Type typearg;
6340 public TypeOf (Expression queried_type, Location l)
6342 QueriedType = queried_type;
6346 public override Expression DoResolve (EmitContext ec)
6348 TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec, false);
6352 typearg = texpr.Type;
6354 if (typearg == TypeManager.void_type) {
6355 Error (673, "System.Void cannot be used from C#. Use typeof (void) to get the void type object");
6359 if (typearg.IsPointer && !ec.InUnsafe){
6364 type = TypeManager.type_type;
6365 // Even though what is returned is a type object, it's treated as a value by the compiler.
6366 // In particular, 'typeof (Foo).X' is something totally different from 'Foo.X'.
6367 eclass = ExprClass.Value;
6371 public override void Emit (EmitContext ec)
6373 ec.ig.Emit (OpCodes.Ldtoken, typearg);
6374 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
6377 public override bool GetAttributableValue (Type value_type, out object value)
6379 if (TypeManager.ContainsGenericParameters (typearg) &&
6380 !TypeManager.IsGenericTypeDefinition (typearg)) {
6381 Report.SymbolRelatedToPreviousError (typearg);
6382 Report.Error (416, loc, "`{0}': an attribute argument cannot use type parameters",
6383 TypeManager.CSharpName (typearg));
6388 if (value_type == TypeManager.object_type) {
6389 value = (object)typearg;
6396 public Type TypeArgument
6404 protected override void CloneTo (CloneContext clonectx, Expression t)
6406 TypeOf target = (TypeOf) t;
6408 target.QueriedType = QueriedType.Clone (clonectx);
6413 /// Implements the `typeof (void)' operator
6415 public class TypeOfVoid : TypeOf {
6416 public TypeOfVoid (Location l) : base (null, l)
6421 public override Expression DoResolve (EmitContext ec)
6423 type = TypeManager.type_type;
6424 typearg = TypeManager.void_type;
6425 // See description in TypeOf.
6426 eclass = ExprClass.Value;
6431 internal class TypeOfMethod : Expression
6433 readonly MethodGroupExpr method;
6434 static MethodInfo get_type_from_handle;
6436 static TypeOfMethod ()
6438 get_type_from_handle = typeof (MethodBase).GetMethod ("GetMethodFromHandle",
6439 new Type [] { TypeManager.runtime_method_handle_type });
6442 public TypeOfMethod (MethodGroupExpr method)
6444 this.method = method;
6445 loc = method.Location;
6448 public override Expression DoResolve (EmitContext ec)
6450 type = typeof (MethodBase);
6451 eclass = ExprClass.Value;
6455 public override void Emit (EmitContext ec)
6457 ec.ig.Emit (OpCodes.Ldtoken, (MethodInfo)method);
6458 ec.ig.Emit (OpCodes.Call, get_type_from_handle);
6463 /// Implements the sizeof expression
6465 public class SizeOf : Expression {
6466 readonly Expression QueriedType;
6469 public SizeOf (Expression queried_type, Location l)
6471 this.QueriedType = queried_type;
6475 public override Expression DoResolve (EmitContext ec)
6477 TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec, false);
6482 if (texpr is TypeParameterExpr){
6483 ((TypeParameterExpr)texpr).Error_CannotUseAsUnmanagedType (loc);
6488 type_queried = texpr.Type;
6489 if (type_queried.IsEnum)
6490 type_queried = TypeManager.EnumToUnderlying (type_queried);
6492 if (type_queried == TypeManager.void_type) {
6493 Expression.Error_VoidInvalidInTheContext (loc);
6497 int size_of = GetTypeSize (type_queried);
6499 return new IntConstant (size_of, loc);
6503 Report.Error (233, loc, "`{0}' does not have a predefined size, therefore sizeof can only be used in an unsafe context (consider using System.Runtime.InteropServices.Marshal.SizeOf)",
6504 TypeManager.CSharpName (type_queried));
6508 if (!TypeManager.VerifyUnManaged (type_queried, loc)){
6512 type = TypeManager.int32_type;
6513 eclass = ExprClass.Value;
6517 public override void Emit (EmitContext ec)
6519 int size = GetTypeSize (type_queried);
6522 ec.ig.Emit (OpCodes.Sizeof, type_queried);
6524 IntConstant.EmitInt (ec.ig, size);
6527 protected override void CloneTo (CloneContext clonectx, Expression t)
6533 /// Implements the qualified-alias-member (::) expression.
6535 public class QualifiedAliasMember : Expression
6537 string alias, identifier;
6539 public QualifiedAliasMember (string alias, string identifier, Location l)
6542 this.identifier = identifier;
6546 public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
6548 if (alias == "global")
6549 return new MemberAccess (RootNamespace.Global, identifier, loc).ResolveAsTypeStep (ec, silent);
6551 int errors = Report.Errors;
6552 FullNamedExpression fne = ec.DeclContainer.NamespaceEntry.LookupAlias (alias);
6554 if (errors == Report.Errors)
6555 Report.Error (432, loc, "Alias `{0}' not found", alias);
6558 if (fne.eclass != ExprClass.Namespace) {
6560 Report.Error (431, loc, "`{0}' cannot be used with '::' since it denotes a type", alias);
6563 return new MemberAccess (fne, identifier).ResolveAsTypeStep (ec, silent);
6566 public override Expression DoResolve (EmitContext ec)
6568 FullNamedExpression fne;
6569 if (alias == "global") {
6570 fne = RootNamespace.Global;
6572 int errors = Report.Errors;
6573 fne = ec.DeclContainer.NamespaceEntry.LookupAlias (alias);
6575 if (errors == Report.Errors)
6576 Report.Error (432, loc, "Alias `{0}' not found", alias);
6581 Expression retval = new MemberAccess (fne, identifier).DoResolve (ec);
6585 if (!(retval is FullNamedExpression)) {
6586 Report.Error (687, loc, "The expression `{0}::{1}' did not resolve to a namespace or a type", alias, identifier);
6590 // We defer this check till the end to match the behaviour of CSC
6591 if (fne.eclass != ExprClass.Namespace) {
6592 Report.Error (431, loc, "`{0}' cannot be used with '::' since it denotes a type", alias);
6598 public override void Emit (EmitContext ec)
6600 throw new InternalErrorException ("QualifiedAliasMember found in resolved tree");
6604 public override string ToString ()
6606 return alias + "::" + identifier;
6609 public override string GetSignatureForError ()
6614 protected override void CloneTo (CloneContext clonectx, Expression t)
6621 /// Implements the member access expression
6623 public class MemberAccess : Expression {
6624 public readonly string Identifier;
6626 readonly TypeArguments args;
6628 public MemberAccess (Expression expr, string id)
6629 : this (expr, id, expr.Location)
6633 public MemberAccess (Expression expr, string identifier, Location loc)
6636 Identifier = identifier;
6640 public MemberAccess (Expression expr, string identifier, TypeArguments args, Location loc)
6641 : this (expr, identifier, loc)
6646 protected string LookupIdentifier {
6647 get { return MemberName.MakeName (Identifier, args); }
6650 // TODO: this method has very poor performace for Enum fields and
6651 // probably for other constants as well
6652 Expression DoResolve (EmitContext ec, Expression right_side)
6655 throw new Exception ();
6658 // Resolve the expression with flow analysis turned off, we'll do the definite
6659 // assignment checks later. This is because we don't know yet what the expression
6660 // will resolve to - it may resolve to a FieldExpr and in this case we must do the
6661 // definite assignment check on the actual field and not on the whole struct.
6664 SimpleName original = expr as SimpleName;
6665 Expression expr_resolved = expr.Resolve (ec,
6666 ResolveFlags.VariableOrValue | ResolveFlags.Type |
6667 ResolveFlags.Intermediate | ResolveFlags.DisableStructFlowAnalysis);
6669 if (expr_resolved == null)
6672 if (expr_resolved is Namespace) {
6673 Namespace ns = (Namespace) expr_resolved;
6674 FullNamedExpression retval = ns.Lookup (ec.DeclContainer, LookupIdentifier, loc);
6676 if ((retval != null) && (args != null))
6677 retval = new ConstructedType (retval, args, loc).ResolveAsTypeStep (ec, false);
6681 ns.Error_NamespaceDoesNotExist (ec.DeclContainer, loc, Identifier);
6685 Type expr_type = expr_resolved.Type;
6686 if (expr_type.IsPointer || expr_type == TypeManager.void_type || expr_resolved is NullLiteral){
6687 Unary.Error_OperatorCannotBeApplied (loc, ".", expr_type);
6690 if (expr_type == TypeManager.anonymous_method_type){
6691 Unary.Error_OperatorCannotBeApplied (loc, ".", "anonymous method");
6695 Constant c = expr_resolved as Constant;
6696 if (c != null && c.GetValue () == null) {
6697 Report.Warning (1720, 1, loc, "Expression will always cause a `{0}'",
6698 "System.NullReferenceException");
6702 if (!args.Resolve (ec))
6706 Expression member_lookup;
6707 member_lookup = MemberLookup (
6708 ec.ContainerType, expr_type, expr_type, Identifier, loc);
6710 if ((member_lookup == null) && (args != null)) {
6711 member_lookup = MemberLookup (
6712 ec.ContainerType, expr_type, expr_type, LookupIdentifier, loc);
6715 if (member_lookup == null) {
6716 ExprClass expr_eclass = expr_resolved.eclass;
6719 // Extension methods are not allowed on all expression types
6721 if (expr_eclass == ExprClass.Value || expr_eclass == ExprClass.Variable ||
6722 expr_eclass == ExprClass.IndexerAccess || expr_eclass == ExprClass.PropertyAccess ||
6723 expr_eclass == ExprClass.EventAccess) {
6724 ExtensionMethodGroupExpr ex_method_lookup = ec.TypeContainer.LookupExtensionMethod (expr_type, Identifier);
6725 if (ex_method_lookup != null) {
6726 ex_method_lookup.ExtensionExpression = expr_resolved;
6729 ex_method_lookup.SetTypeArguments (args);
6732 return ex_method_lookup.DoResolve (ec);
6736 expr = expr_resolved;
6737 Error_MemberLookupFailed (
6738 ec.ContainerType, expr_type, expr_type, Identifier, null,
6739 AllMemberTypes, AllBindingFlags);
6743 TypeExpr texpr = member_lookup as TypeExpr;
6744 if (texpr != null) {
6745 if (!(expr_resolved is TypeExpr) &&
6746 (original == null || !original.IdenticalNameAndTypeName (ec, expr_resolved, loc))) {
6747 Report.Error (572, loc, "`{0}': cannot reference a type through an expression; try `{1}' instead",
6748 Identifier, member_lookup.GetSignatureForError ());
6752 if (!texpr.CheckAccessLevel (ec.DeclContainer)) {
6753 Report.SymbolRelatedToPreviousError (member_lookup.Type);
6754 ErrorIsInaccesible (loc, TypeManager.CSharpName (member_lookup.Type));
6759 ConstructedType ct = expr_resolved as ConstructedType;
6762 // When looking up a nested type in a generic instance
6763 // via reflection, we always get a generic type definition
6764 // and not a generic instance - so we have to do this here.
6766 // See gtest-172-lib.cs and gtest-172.cs for an example.
6768 ct = new ConstructedType (
6769 member_lookup.Type, ct.TypeArguments, loc);
6771 return ct.ResolveAsTypeStep (ec, false);
6774 return member_lookup;
6777 MemberExpr me = (MemberExpr) member_lookup;
6778 me = me.ResolveMemberAccess (ec, expr_resolved, loc, original);
6783 me.SetTypeArguments (args);
6786 if (original != null && !TypeManager.IsValueType (expr_type)) {
6787 if (me.IsInstance) {
6788 LocalVariableReference var = expr_resolved as LocalVariableReference;
6789 if (var != null && !var.VerifyAssigned (ec))
6794 // The following DoResolve/DoResolveLValue will do the definite assignment
6797 if (right_side != null)
6798 return me.DoResolveLValue (ec, right_side);
6800 return me.DoResolve (ec);
6803 public override Expression DoResolve (EmitContext ec)
6805 return DoResolve (ec, null);
6808 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
6810 return DoResolve (ec, right_side);
6813 public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
6815 return ResolveNamespaceOrType (ec, silent);
6818 public FullNamedExpression ResolveNamespaceOrType (IResolveContext rc, bool silent)
6820 FullNamedExpression new_expr = expr.ResolveAsTypeStep (rc, silent);
6822 if (new_expr == null)
6825 if (new_expr is Namespace) {
6826 Namespace ns = (Namespace) new_expr;
6827 FullNamedExpression retval = ns.Lookup (rc.DeclContainer, LookupIdentifier, loc);
6829 if ((retval != null) && (args != null))
6830 retval = new ConstructedType (retval, args, loc).ResolveAsTypeStep (rc, false);
6832 if (!silent && retval == null)
6833 ns.Error_NamespaceDoesNotExist (rc.DeclContainer, loc, LookupIdentifier);
6837 TypeExpr tnew_expr = new_expr.ResolveAsTypeTerminal (rc, false);
6838 if (tnew_expr == null)
6841 Type expr_type = tnew_expr.Type;
6843 if (expr_type.IsPointer){
6844 Error (23, "The `.' operator can not be applied to pointer operands (" +
6845 TypeManager.CSharpName (expr_type) + ")");
6849 Expression member_lookup = MemberLookup (
6850 rc.DeclContainer.TypeBuilder, expr_type, expr_type, LookupIdentifier,
6851 MemberTypes.NestedType, BindingFlags.Public | BindingFlags.NonPublic, loc);
6852 if (member_lookup == null) {
6856 member_lookup = MemberLookup (
6857 rc.DeclContainer.TypeBuilder, expr_type, expr_type, SimpleName.RemoveGenericArity (LookupIdentifier),
6858 MemberTypes.NestedType, BindingFlags.Public | BindingFlags.NonPublic, loc);
6860 if (member_lookup != null) {
6861 tnew_expr = member_lookup.ResolveAsTypeTerminal (rc, false);
6862 if (tnew_expr == null)
6865 Namespace.Error_TypeArgumentsCannotBeUsed (tnew_expr.Type, loc);
6869 member_lookup = MemberLookup (
6870 rc.DeclContainer.TypeBuilder, expr_type, expr_type, LookupIdentifier,
6871 MemberTypes.All, BindingFlags.Public | BindingFlags.NonPublic, loc);
6873 if (member_lookup == null) {
6874 Report.Error (426, loc, "The nested type `{0}' does not exist in the type `{1}'",
6875 Identifier, new_expr.GetSignatureForError ());
6877 // TODO: Report.SymbolRelatedToPreviousError
6878 member_lookup.Error_UnexpectedKind (null, "type", loc);
6883 TypeExpr texpr = member_lookup.ResolveAsTypeTerminal (rc, false);
6888 TypeArguments the_args = args;
6889 Type declaring_type = texpr.Type.DeclaringType;
6890 if (TypeManager.HasGenericArguments (declaring_type)) {
6891 while (!TypeManager.IsEqual (TypeManager.DropGenericTypeArguments (expr_type), declaring_type)) {
6892 expr_type = expr_type.BaseType;
6895 TypeArguments new_args = new TypeArguments (loc);
6896 foreach (Type decl in TypeManager.GetTypeArguments (expr_type))
6897 new_args.Add (new TypeExpression (decl, loc));
6900 new_args.Add (args);
6902 the_args = new_args;
6905 if (the_args != null) {
6906 ConstructedType ctype = new ConstructedType (texpr.Type, the_args, loc);
6907 return ctype.ResolveAsTypeStep (rc, false);
6914 public override void Emit (EmitContext ec)
6916 throw new Exception ("Should not happen");
6919 protected override void Error_TypeDoesNotContainDefinition (Type type, string name)
6921 if (RootContext.Version > LanguageVersion.ISO_2 &&
6922 ((expr.eclass & (ExprClass.Value | ExprClass.Variable)) != 0)) {
6923 Report.Error (1061, loc, "Type `{0}' does not contain a definition for `{1}' and no " +
6924 "extension method `{1}' of type `{0}' could be found " +
6925 "(are you missing a using directive or an assembly reference?)",
6926 TypeManager.CSharpName (type), name);
6930 base.Error_TypeDoesNotContainDefinition (type, name);
6933 public override string ToString ()
6935 return expr + "." + MemberName.MakeName (Identifier, args);
6938 public override string GetSignatureForError ()
6940 return expr.GetSignatureForError () + "." + Identifier;
6943 protected override void CloneTo (CloneContext clonectx, Expression t)
6945 MemberAccess target = (MemberAccess) t;
6947 target.expr = expr.Clone (clonectx);
6952 /// Implements checked expressions
6954 public class CheckedExpr : Expression {
6956 public Expression Expr;
6958 public CheckedExpr (Expression e, Location l)
6964 public override Expression DoResolve (EmitContext ec)
6966 using (ec.With (EmitContext.Flags.AllCheckStateFlags, true))
6967 Expr = Expr.Resolve (ec);
6972 if (Expr is Constant)
6975 eclass = Expr.eclass;
6980 public override void Emit (EmitContext ec)
6982 using (ec.With (EmitContext.Flags.AllCheckStateFlags, true))
6986 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
6988 using (ec.With (EmitContext.Flags.AllCheckStateFlags, true))
6989 Expr.EmitBranchable (ec, target, on_true);
6992 protected override void CloneTo (CloneContext clonectx, Expression t)
6994 CheckedExpr target = (CheckedExpr) t;
6996 target.Expr = Expr.Clone (clonectx);
7001 /// Implements the unchecked expression
7003 public class UnCheckedExpr : Expression {
7005 public Expression Expr;
7007 public UnCheckedExpr (Expression e, Location l)
7013 public override Expression DoResolve (EmitContext ec)
7015 using (ec.With (EmitContext.Flags.AllCheckStateFlags, false))
7016 Expr = Expr.Resolve (ec);
7021 if (Expr is Constant)
7024 eclass = Expr.eclass;
7029 public override void Emit (EmitContext ec)
7031 using (ec.With (EmitContext.Flags.AllCheckStateFlags, false))
7035 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
7037 using (ec.With (EmitContext.Flags.AllCheckStateFlags, false))
7038 Expr.EmitBranchable (ec, target, on_true);
7041 protected override void CloneTo (CloneContext clonectx, Expression t)
7043 UnCheckedExpr target = (UnCheckedExpr) t;
7045 target.Expr = Expr.Clone (clonectx);
7050 /// An Element Access expression.
7052 /// During semantic analysis these are transformed into
7053 /// IndexerAccess, ArrayAccess or a PointerArithmetic.
7055 public class ElementAccess : Expression {
7056 public ArrayList Arguments;
7057 public Expression Expr;
7059 public ElementAccess (Expression e, ArrayList e_list)
7068 Arguments = new ArrayList ();
7069 foreach (Expression tmp in e_list)
7070 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
7074 bool CommonResolve (EmitContext ec)
7076 Expr = Expr.Resolve (ec);
7078 if (Arguments == null)
7081 foreach (Argument a in Arguments){
7082 if (!a.Resolve (ec, loc))
7086 return Expr != null;
7089 public override Expression CreateExpressionTree (EmitContext ec)
7091 ArrayList args = new ArrayList (Arguments.Count + 1);
7092 args.Add (new Argument (Expr.CreateExpressionTree (ec)));
7093 foreach (Argument a in Arguments)
7094 args.Add (new Argument (a.Expr.CreateExpressionTree (ec)));
7096 return CreateExpressionFactoryCall ("ArrayIndex", args);
7099 Expression MakePointerAccess (EmitContext ec, Type t)
7101 if (t == TypeManager.void_ptr_type){
7102 Error (242, "The array index operation is not valid on void pointers");
7105 if (Arguments.Count != 1){
7106 Error (196, "A pointer must be indexed by only one value");
7111 p = new PointerArithmetic (true, Expr, ((Argument)Arguments [0]).Expr, t, loc).Resolve (ec);
7114 return new Indirection (p, loc).Resolve (ec);
7117 public override Expression DoResolve (EmitContext ec)
7119 if (!CommonResolve (ec))
7123 // We perform some simple tests, and then to "split" the emit and store
7124 // code we create an instance of a different class, and return that.
7126 // I am experimenting with this pattern.
7130 if (t == TypeManager.array_type){
7131 Report.Error (21, loc, "Cannot apply indexing with [] to an expression of type `System.Array'");
7136 return (new ArrayAccess (this, loc)).Resolve (ec);
7138 return MakePointerAccess (ec, t);
7140 FieldExpr fe = Expr as FieldExpr;
7142 IFixedBuffer ff = AttributeTester.GetFixedBuffer (fe.FieldInfo);
7144 return MakePointerAccess (ec, ff.ElementType);
7147 return (new IndexerAccess (this, loc)).Resolve (ec);
7150 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
7152 if (!CommonResolve (ec))
7157 return (new ArrayAccess (this, loc)).DoResolveLValue (ec, right_side);
7160 return MakePointerAccess (ec, type);
7162 if (Expr.eclass != ExprClass.Variable && type.IsValueType)
7163 Error_CannotModifyIntermediateExpressionValue (ec);
7165 return (new IndexerAccess (this, loc)).DoResolveLValue (ec, right_side);
7168 public override void Emit (EmitContext ec)
7170 throw new Exception ("Should never be reached");
7173 public override string GetSignatureForError ()
7175 return Expr.GetSignatureForError ();
7178 protected override void CloneTo (CloneContext clonectx, Expression t)
7180 ElementAccess target = (ElementAccess) t;
7182 target.Expr = Expr.Clone (clonectx);
7183 target.Arguments = new ArrayList (Arguments.Count);
7184 foreach (Argument a in Arguments)
7185 target.Arguments.Add (a.Clone (clonectx));
7190 /// Implements array access
7192 public class ArrayAccess : Expression, IAssignMethod, IMemoryLocation {
7194 // Points to our "data" repository
7198 LocalTemporary temp;
7199 LocalTemporary prepared_value;
7203 public ArrayAccess (ElementAccess ea_data, Location l)
7206 eclass = ExprClass.Variable;
7210 public override Expression CreateExpressionTree (EmitContext ec)
7212 return ea.CreateExpressionTree (ec);
7215 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
7217 return DoResolve (ec);
7220 public override Expression DoResolve (EmitContext ec)
7223 ExprClass eclass = ea.Expr.eclass;
7225 // As long as the type is valid
7226 if (!(eclass == ExprClass.Variable || eclass == ExprClass.PropertyAccess ||
7227 eclass == ExprClass.Value)) {
7228 ea.Expr.Error_UnexpectedKind ("variable or value");
7233 Type t = ea.Expr.Type;
7234 if (t.GetArrayRank () != ea.Arguments.Count) {
7235 Report.Error (22, ea.Location, "Wrong number of indexes `{0}' inside [], expected `{1}'",
7236 ea.Arguments.Count.ToString (), t.GetArrayRank ().ToString ());
7240 type = TypeManager.GetElementType (t);
7241 if (type.IsPointer && !ec.InUnsafe) {
7242 UnsafeError (ea.Location);
7246 foreach (Argument a in ea.Arguments) {
7247 a.Expr = ConvertExpressionToArrayIndex (ec, a.Expr);
7250 eclass = ExprClass.Variable;
7256 /// Emits the right opcode to load an object of Type `t'
7257 /// from an array of T
7259 void EmitLoadOpcode (ILGenerator ig, Type type, int rank)
7262 MethodInfo get = FetchGetMethod ();
7263 ig.Emit (OpCodes.Call, get);
7267 if (type == TypeManager.byte_type || type == TypeManager.bool_type)
7268 ig.Emit (OpCodes.Ldelem_U1);
7269 else if (type == TypeManager.sbyte_type)
7270 ig.Emit (OpCodes.Ldelem_I1);
7271 else if (type == TypeManager.short_type)
7272 ig.Emit (OpCodes.Ldelem_I2);
7273 else if (type == TypeManager.ushort_type || type == TypeManager.char_type)
7274 ig.Emit (OpCodes.Ldelem_U2);
7275 else if (type == TypeManager.int32_type)
7276 ig.Emit (OpCodes.Ldelem_I4);
7277 else if (type == TypeManager.uint32_type)
7278 ig.Emit (OpCodes.Ldelem_U4);
7279 else if (type == TypeManager.uint64_type)
7280 ig.Emit (OpCodes.Ldelem_I8);
7281 else if (type == TypeManager.int64_type)
7282 ig.Emit (OpCodes.Ldelem_I8);
7283 else if (type == TypeManager.float_type)
7284 ig.Emit (OpCodes.Ldelem_R4);
7285 else if (type == TypeManager.double_type)
7286 ig.Emit (OpCodes.Ldelem_R8);
7287 else if (type == TypeManager.intptr_type)
7288 ig.Emit (OpCodes.Ldelem_I);
7289 else if (TypeManager.IsEnumType (type)){
7290 EmitLoadOpcode (ig, TypeManager.EnumToUnderlying (type), rank);
7291 } else if (type.IsValueType){
7292 ig.Emit (OpCodes.Ldelema, type);
7293 ig.Emit (OpCodes.Ldobj, type);
7295 } else if (type.IsGenericParameter) {
7296 ig.Emit (OpCodes.Ldelem, type);
7298 } else if (type.IsPointer)
7299 ig.Emit (OpCodes.Ldelem_I);
7301 ig.Emit (OpCodes.Ldelem_Ref);
7304 protected override void Error_NegativeArrayIndex (Location loc)
7306 Report.Warning (251, 2, loc, "Indexing an array with a negative index (array indices always start at zero)");
7310 /// Returns the right opcode to store an object of Type `t'
7311 /// from an array of T.
7313 static public OpCode GetStoreOpcode (Type t, out bool is_stobj, out bool has_type_arg)
7315 //Console.WriteLine (new System.Diagnostics.StackTrace ());
7316 has_type_arg = false; is_stobj = false;
7317 t = TypeManager.TypeToCoreType (t);
7318 if (TypeManager.IsEnumType (t))
7319 t = TypeManager.EnumToUnderlying (t);
7320 if (t == TypeManager.byte_type || t == TypeManager.sbyte_type ||
7321 t == TypeManager.bool_type)
7322 return OpCodes.Stelem_I1;
7323 else if (t == TypeManager.short_type || t == TypeManager.ushort_type ||
7324 t == TypeManager.char_type)
7325 return OpCodes.Stelem_I2;
7326 else if (t == TypeManager.int32_type || t == TypeManager.uint32_type)
7327 return OpCodes.Stelem_I4;
7328 else if (t == TypeManager.int64_type || t == TypeManager.uint64_type)
7329 return OpCodes.Stelem_I8;
7330 else if (t == TypeManager.float_type)
7331 return OpCodes.Stelem_R4;
7332 else if (t == TypeManager.double_type)
7333 return OpCodes.Stelem_R8;
7334 else if (t == TypeManager.intptr_type) {
7335 has_type_arg = true;
7337 return OpCodes.Stobj;
7338 } else if (t.IsValueType) {
7339 has_type_arg = true;
7341 return OpCodes.Stobj;
7343 } else if (t.IsGenericParameter) {
7344 has_type_arg = true;
7345 return OpCodes.Stelem;
7348 } else if (t.IsPointer)
7349 return OpCodes.Stelem_I;
7351 return OpCodes.Stelem_Ref;
7354 MethodInfo FetchGetMethod ()
7356 ModuleBuilder mb = CodeGen.Module.Builder;
7357 int arg_count = ea.Arguments.Count;
7358 Type [] args = new Type [arg_count];
7361 for (int i = 0; i < arg_count; i++){
7362 //args [i++] = a.Type;
7363 args [i] = TypeManager.int32_type;
7366 get = mb.GetArrayMethod (
7367 ea.Expr.Type, "Get",
7368 CallingConventions.HasThis |
7369 CallingConventions.Standard,
7375 MethodInfo FetchAddressMethod ()
7377 ModuleBuilder mb = CodeGen.Module.Builder;
7378 int arg_count = ea.Arguments.Count;
7379 Type [] args = new Type [arg_count];
7383 ret_type = TypeManager.GetReferenceType (type);
7385 for (int i = 0; i < arg_count; i++){
7386 //args [i++] = a.Type;
7387 args [i] = TypeManager.int32_type;
7390 address = mb.GetArrayMethod (
7391 ea.Expr.Type, "Address",
7392 CallingConventions.HasThis |
7393 CallingConventions.Standard,
7400 // Load the array arguments into the stack.
7402 // If we have been requested to cache the values (cached_locations array
7403 // initialized), then load the arguments the first time and store them
7404 // in locals. otherwise load from local variables.
7406 // prepare_for_load is used in compound assignments to cache original index
7407 // values ( label[idx++] += s )
7409 LocalTemporary [] LoadArrayAndArguments (EmitContext ec, bool prepare_for_load)
7413 LocalTemporary[] indexes = null;
7414 if (prepare_for_load) {
7415 ec.ig.Emit (OpCodes.Dup);
7416 indexes = new LocalTemporary [ea.Arguments.Count];
7419 for (int i = 0; i < ea.Arguments.Count; ++i) {
7420 ((Argument)ea.Arguments [i]).Emit (ec);
7421 if (!prepare_for_load)
7424 // Keep original array index value on the stack
7425 ec.ig.Emit (OpCodes.Dup);
7427 indexes [i] = new LocalTemporary (TypeManager.intptr_type);
7428 indexes [i].Store (ec);
7434 public void Emit (EmitContext ec, bool leave_copy)
7436 int rank = ea.Expr.Type.GetArrayRank ();
7437 ILGenerator ig = ec.ig;
7439 if (prepared_value != null) {
7440 prepared_value.Emit (ec);
7441 } else if (prepared) {
7442 LoadFromPtr (ig, this.type);
7444 LoadArrayAndArguments (ec, false);
7445 EmitLoadOpcode (ig, type, rank);
7449 ig.Emit (OpCodes.Dup);
7450 temp = new LocalTemporary (this.type);
7455 public override void Emit (EmitContext ec)
7460 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
7462 int rank = ea.Expr.Type.GetArrayRank ();
7463 ILGenerator ig = ec.ig;
7464 Type t = source.Type;
7465 prepared = prepare_for_load && !(source is StringConcat);
7468 AddressOf (ec, AddressOp.LoadStore);
7469 ec.ig.Emit (OpCodes.Dup);
7471 LocalTemporary[] original_indexes_values = LoadArrayAndArguments (ec,
7472 prepare_for_load && (source is StringConcat));
7474 if (original_indexes_values != null) {
7475 prepared_value = new LocalTemporary (type);
7476 EmitLoadOpcode (ig, type, rank);
7477 prepared_value.Store (ec);
7478 foreach (LocalTemporary lt in original_indexes_values) {
7486 bool is_stobj, has_type_arg;
7487 OpCode op = GetStoreOpcode (t, out is_stobj, out has_type_arg);
7491 // The stobj opcode used by value types will need
7492 // an address on the stack, not really an array/array
7496 ig.Emit (OpCodes.Ldelema, t);
7501 ec.ig.Emit (OpCodes.Dup);
7502 temp = new LocalTemporary (this.type);
7507 StoreFromPtr (ig, t);
7509 ig.Emit (OpCodes.Stobj, t);
7510 else if (has_type_arg)
7517 ec.ig.Emit (OpCodes.Dup);
7518 temp = new LocalTemporary (this.type);
7523 StoreFromPtr (ig, t);
7525 int arg_count = ea.Arguments.Count;
7526 Type [] args = new Type [arg_count + 1];
7527 for (int i = 0; i < arg_count; i++) {
7528 //args [i++] = a.Type;
7529 args [i] = TypeManager.int32_type;
7531 args [arg_count] = type;
7533 MethodInfo set = CodeGen.Module.Builder.GetArrayMethod (
7534 ea.Expr.Type, "Set",
7535 CallingConventions.HasThis |
7536 CallingConventions.Standard,
7537 TypeManager.void_type, args);
7539 ig.Emit (OpCodes.Call, set);
7549 public void AddressOf (EmitContext ec, AddressOp mode)
7551 int rank = ea.Expr.Type.GetArrayRank ();
7552 ILGenerator ig = ec.ig;
7554 LoadArrayAndArguments (ec, false);
7557 ig.Emit (OpCodes.Ldelema, type);
7559 MethodInfo address = FetchAddressMethod ();
7560 ig.Emit (OpCodes.Call, address);
7564 public void EmitGetLength (EmitContext ec, int dim)
7566 int rank = ea.Expr.Type.GetArrayRank ();
7567 ILGenerator ig = ec.ig;
7571 ig.Emit (OpCodes.Ldlen);
7572 ig.Emit (OpCodes.Conv_I4);
7574 IntLiteral.EmitInt (ig, dim);
7575 ig.Emit (OpCodes.Callvirt, TypeManager.int_getlength_int);
7581 /// Expressions that represent an indexer call.
7583 public class IndexerAccess : Expression, IAssignMethod
7585 class IndexerMethodGroupExpr : MethodGroupExpr
7587 public IndexerMethodGroupExpr (Indexers indexers, Location loc)
7590 Methods = (MethodBase []) indexers.Methods.ToArray (typeof (MethodBase));
7593 public override string Name {
7599 protected override int GetApplicableParametersCount (MethodBase method, ParameterData parameters)
7602 // Here is the trick, decrease number of arguments by 1 when only
7603 // available property method is setter. This makes overload resolution
7604 // work correctly for indexers.
7607 if (method.Name [0] == 'g')
7608 return parameters.Count;
7610 return parameters.Count - 1;
7616 // Contains either property getter or setter
7617 public ArrayList Methods;
7618 public ArrayList Properties;
7624 void Append (Type caller_type, MemberInfo [] mi)
7629 foreach (PropertyInfo property in mi) {
7630 MethodInfo accessor = property.GetGetMethod (true);
7631 if (accessor == null)
7632 accessor = property.GetSetMethod (true);
7634 if (Methods == null) {
7635 Methods = new ArrayList ();
7636 Properties = new ArrayList ();
7639 Methods.Add (accessor);
7640 Properties.Add (property);
7644 static MemberInfo [] GetIndexersForTypeOrInterface (Type caller_type, Type lookup_type)
7646 string p_name = TypeManager.IndexerPropertyName (lookup_type);
7648 return TypeManager.MemberLookup (
7649 caller_type, caller_type, lookup_type, MemberTypes.Property,
7650 BindingFlags.Public | BindingFlags.Instance |
7651 BindingFlags.DeclaredOnly, p_name, null);
7654 public static Indexers GetIndexersForType (Type caller_type, Type lookup_type)
7656 Indexers ix = new Indexers ();
7659 if (lookup_type.IsGenericParameter) {
7660 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (lookup_type);
7664 if (gc.HasClassConstraint)
7665 ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, gc.ClassConstraint));
7667 Type[] ifaces = gc.InterfaceConstraints;
7668 foreach (Type itype in ifaces)
7669 ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, itype));
7675 Type copy = lookup_type;
7676 while (copy != TypeManager.object_type && copy != null){
7677 ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, copy));
7678 copy = copy.BaseType;
7681 if (lookup_type.IsInterface) {
7682 Type [] ifaces = TypeManager.GetInterfaces (lookup_type);
7683 if (ifaces != null) {
7684 foreach (Type itype in ifaces)
7685 ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, itype));
7700 // Points to our "data" repository
7702 MethodInfo get, set;
7703 bool is_base_indexer;
7705 LocalTemporary temp;
7706 LocalTemporary prepared_value;
7707 Expression set_expr;
7709 protected Type indexer_type;
7710 protected Type current_type;
7711 protected Expression instance_expr;
7712 protected ArrayList arguments;
7714 public IndexerAccess (ElementAccess ea, Location loc)
7715 : this (ea.Expr, false, loc)
7717 this.arguments = ea.Arguments;
7720 protected IndexerAccess (Expression instance_expr, bool is_base_indexer,
7723 this.instance_expr = instance_expr;
7724 this.is_base_indexer = is_base_indexer;
7725 this.eclass = ExprClass.Value;
7729 static string GetAccessorName (AccessorType at)
7731 if (at == AccessorType.Set)
7734 if (at == AccessorType.Get)
7737 throw new NotImplementedException (at.ToString ());
7740 protected virtual bool CommonResolve (EmitContext ec)
7742 indexer_type = instance_expr.Type;
7743 current_type = ec.ContainerType;
7748 public override Expression DoResolve (EmitContext ec)
7750 return ResolveAccessor (ec, AccessorType.Get);
7753 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
7755 if (right_side == EmptyExpression.OutAccess) {
7756 Report.Error (206, loc, "A property or indexer `{0}' may not be passed as an out or ref parameter",
7757 GetSignatureForError ());
7761 // if the indexer returns a value type, and we try to set a field in it
7762 if (right_side == EmptyExpression.LValueMemberAccess || right_side == EmptyExpression.LValueMemberOutAccess) {
7763 Error_CannotModifyIntermediateExpressionValue (ec);
7766 Expression e = ResolveAccessor (ec, AccessorType.Set);
7770 set_expr = Convert.ImplicitConversion (ec, right_side, type, loc);
7774 Expression ResolveAccessor (EmitContext ec, AccessorType accessorType)
7776 if (!CommonResolve (ec))
7779 Indexers ilist = Indexers.GetIndexersForType (current_type, indexer_type);
7780 if (ilist.Methods == null) {
7781 Report.Error (21, loc, "Cannot apply indexing with [] to an expression of type `{0}'",
7782 TypeManager.CSharpName (indexer_type));
7786 MethodGroupExpr mg = new IndexerMethodGroupExpr (ilist, loc);
7787 mg = mg.OverloadResolve (ec, ref arguments, false, loc);
7791 MethodInfo mi = (MethodInfo) mg;
7792 PropertyInfo pi = null;
7793 for (int i = 0; i < ilist.Methods.Count; ++i) {
7794 if (ilist.Methods [i] == mi) {
7795 pi = (PropertyInfo) ilist.Properties [i];
7800 type = pi.PropertyType;
7801 if (type.IsPointer && !ec.InUnsafe)
7804 MethodInfo accessor;
7805 if (accessorType == AccessorType.Get) {
7806 accessor = get = pi.GetGetMethod (true);
7808 accessor = set = pi.GetSetMethod (true);
7809 if (accessor == null && pi.GetGetMethod (true) != null) {
7810 Report.SymbolRelatedToPreviousError (pi);
7811 Report.Error (200, loc, "The read only property or indexer `{0}' cannot be assigned to",
7812 TypeManager.GetFullNameSignature (pi));
7817 if (accessor == null) {
7818 Report.SymbolRelatedToPreviousError (pi);
7819 Report.Error (154, loc, "The property or indexer `{0}' cannot be used in this context because it lacks a `{1}' accessor",
7820 TypeManager.GetFullNameSignature (pi), GetAccessorName (accessorType));
7825 // Only base will allow this invocation to happen.
7827 if (accessor.IsAbstract && this is BaseIndexerAccess) {
7828 Error_CannotCallAbstractBase (TypeManager.GetFullNameSignature (pi));
7831 bool must_do_cs1540_check;
7832 if (!IsAccessorAccessible (ec.ContainerType, accessor, out must_do_cs1540_check)) {
7834 set = pi.GetSetMethod (true);
7836 get = pi.GetGetMethod (true);
7838 if (set != null && get != null &&
7839 (set.Attributes & MethodAttributes.MemberAccessMask) != (get.Attributes & MethodAttributes.MemberAccessMask)) {
7840 Report.SymbolRelatedToPreviousError (accessor);
7841 Report.Error (271, loc, "The property or indexer `{0}' cannot be used in this context because a `{1}' accessor is inaccessible",
7842 TypeManager.GetFullNameSignature (pi), GetAccessorName (accessorType));
7844 Report.SymbolRelatedToPreviousError (pi);
7845 ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (pi));
7849 instance_expr.CheckMarshalByRefAccess (ec);
7850 eclass = ExprClass.IndexerAccess;
7854 public void Emit (EmitContext ec, bool leave_copy)
7857 prepared_value.Emit (ec);
7859 Invocation.EmitCall (ec, is_base_indexer, instance_expr, get,
7860 arguments, loc, false, false);
7864 ec.ig.Emit (OpCodes.Dup);
7865 temp = new LocalTemporary (Type);
7871 // source is ignored, because we already have a copy of it from the
7872 // LValue resolution and we have already constructed a pre-cached
7873 // version of the arguments (ea.set_arguments);
7875 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
7877 prepared = prepare_for_load;
7878 Expression value = set_expr;
7881 Invocation.EmitCall (ec, is_base_indexer, instance_expr, get,
7882 arguments, loc, true, false);
7884 prepared_value = new LocalTemporary (type);
7885 prepared_value.Store (ec);
7887 prepared_value.Release (ec);
7890 ec.ig.Emit (OpCodes.Dup);
7891 temp = new LocalTemporary (Type);
7894 } else if (leave_copy) {
7895 temp = new LocalTemporary (Type);
7901 arguments.Add (new Argument (value, Argument.AType.Expression));
7902 Invocation.EmitCall (ec, is_base_indexer, instance_expr, set, arguments, loc, false, prepared);
7910 public override void Emit (EmitContext ec)
7915 public override string GetSignatureForError ()
7917 return TypeManager.CSharpSignature (get != null ? get : set, false);
7920 protected override void CloneTo (CloneContext clonectx, Expression t)
7922 IndexerAccess target = (IndexerAccess) t;
7924 if (arguments != null){
7925 target.arguments = new ArrayList ();
7926 foreach (Argument a in arguments)
7927 target.arguments.Add (a.Clone (clonectx));
7929 if (instance_expr != null)
7930 target.instance_expr = instance_expr.Clone (clonectx);
7935 /// The base operator for method names
7937 public class BaseAccess : Expression {
7938 public readonly string Identifier;
7941 public BaseAccess (string member, Location l)
7943 this.Identifier = member;
7947 public BaseAccess (string member, TypeArguments args, Location l)
7953 public override Expression DoResolve (EmitContext ec)
7955 Expression c = CommonResolve (ec);
7961 // MethodGroups use this opportunity to flag an error on lacking ()
7963 if (!(c is MethodGroupExpr))
7964 return c.Resolve (ec);
7968 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
7970 Expression c = CommonResolve (ec);
7976 // MethodGroups use this opportunity to flag an error on lacking ()
7978 if (! (c is MethodGroupExpr))
7979 return c.DoResolveLValue (ec, right_side);
7984 Expression CommonResolve (EmitContext ec)
7986 Expression member_lookup;
7987 Type current_type = ec.ContainerType;
7988 Type base_type = current_type.BaseType;
7991 Error (1511, "Keyword `base' is not available in a static method");
7995 if (ec.IsInFieldInitializer){
7996 Error (1512, "Keyword `base' is not available in the current context");
8000 member_lookup = MemberLookup (ec.ContainerType, null, base_type, Identifier,
8001 AllMemberTypes, AllBindingFlags, loc);
8002 if (member_lookup == null) {
8003 Error_MemberLookupFailed (ec.ContainerType, base_type, base_type, Identifier,
8004 null, AllMemberTypes, AllBindingFlags);
8011 left = new TypeExpression (base_type, loc);
8013 left = ec.GetThis (loc);
8015 MemberExpr me = (MemberExpr) member_lookup;
8016 me = me.ResolveMemberAccess (ec, left, loc, null);
8023 me.SetTypeArguments (args);
8029 public override void Emit (EmitContext ec)
8031 throw new Exception ("Should never be called");
8034 protected override void CloneTo (CloneContext clonectx, Expression t)
8036 BaseAccess target = (BaseAccess) t;
8039 target.args = args.Clone ();
8044 /// The base indexer operator
8046 public class BaseIndexerAccess : IndexerAccess {
8047 public BaseIndexerAccess (ArrayList args, Location loc)
8048 : base (null, true, loc)
8050 arguments = new ArrayList ();
8051 foreach (Expression tmp in args)
8052 arguments.Add (new Argument (tmp, Argument.AType.Expression));
8055 protected override bool CommonResolve (EmitContext ec)
8057 instance_expr = ec.GetThis (loc);
8059 current_type = ec.ContainerType.BaseType;
8060 indexer_type = current_type;
8062 foreach (Argument a in arguments){
8063 if (!a.Resolve (ec, loc))
8072 /// This class exists solely to pass the Type around and to be a dummy
8073 /// that can be passed to the conversion functions (this is used by
8074 /// foreach implementation to typecast the object return value from
8075 /// get_Current into the proper type. All code has been generated and
8076 /// we only care about the side effect conversions to be performed
8078 /// This is also now used as a placeholder where a no-action expression
8079 /// is needed (the `New' class).
8081 public class EmptyExpression : Expression {
8082 public static readonly EmptyExpression Null = new EmptyExpression ();
8084 public static readonly EmptyExpression OutAccess = new EmptyExpression ();
8085 public static readonly EmptyExpression LValueMemberAccess = new EmptyExpression ();
8086 public static readonly EmptyExpression LValueMemberOutAccess = new EmptyExpression ();
8088 static EmptyExpression temp = new EmptyExpression ();
8089 public static EmptyExpression Grab ()
8091 EmptyExpression retval = temp == null ? new EmptyExpression () : temp;
8096 public static void Release (EmptyExpression e)
8101 // TODO: should be protected
8102 public EmptyExpression ()
8104 type = TypeManager.object_type;
8105 eclass = ExprClass.Value;
8106 loc = Location.Null;
8109 public EmptyExpression (Type t)
8112 eclass = ExprClass.Value;
8113 loc = Location.Null;
8116 public override Expression DoResolve (EmitContext ec)
8121 public override void Emit (EmitContext ec)
8123 // nothing, as we only exist to not do anything.
8127 // This is just because we might want to reuse this bad boy
8128 // instead of creating gazillions of EmptyExpressions.
8129 // (CanImplicitConversion uses it)
8131 public void SetType (Type t)
8138 // Empty statement expression
8140 public sealed class EmptyExpressionStatement : ExpressionStatement
8142 public static readonly EmptyExpressionStatement Instance = new EmptyExpressionStatement ();
8144 private EmptyExpressionStatement ()
8146 type = TypeManager.object_type;
8147 eclass = ExprClass.Value;
8148 loc = Location.Null;
8151 public override void EmitStatement (EmitContext ec)
8156 public override Expression DoResolve (EmitContext ec)
8161 public override void Emit (EmitContext ec)
8167 public class UserCast : Expression {
8171 public UserCast (MethodInfo method, Expression source, Location l)
8173 this.method = method;
8174 this.source = source;
8175 type = method.ReturnType;
8176 eclass = ExprClass.Value;
8180 public Expression Source {
8186 public override Expression DoResolve (EmitContext ec)
8189 // We are born fully resolved
8194 public override void Emit (EmitContext ec)
8196 ILGenerator ig = ec.ig;
8200 if (method is MethodInfo)
8201 ig.Emit (OpCodes.Call, (MethodInfo) method);
8203 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
8209 // This class is used to "construct" the type during a typecast
8210 // operation. Since the Type.GetType class in .NET can parse
8211 // the type specification, we just use this to construct the type
8212 // one bit at a time.
8214 public class ComposedCast : TypeExpr {
8218 public ComposedCast (Expression left, string dim)
8219 : this (left, dim, left.Location)
8223 public ComposedCast (Expression left, string dim, Location l)
8230 public Expression RemoveNullable ()
8232 if (dim.EndsWith ("?")) {
8233 dim = dim.Substring (0, dim.Length - 1);
8234 if (dim.Length == 0)
8241 protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
8243 TypeExpr lexpr = left.ResolveAsTypeTerminal (ec, false);
8247 Type ltype = lexpr.Type;
8248 if ((ltype == TypeManager.void_type) && (dim != "*")) {
8249 Error_VoidInvalidInTheContext (loc);
8254 if ((dim.Length > 0) && (dim [0] == '?')) {
8255 TypeExpr nullable = new NullableType (left, loc);
8257 nullable = new ComposedCast (nullable, dim.Substring (1), loc);
8258 return nullable.ResolveAsTypeTerminal (ec, false);
8262 if (dim == "*" && !TypeManager.VerifyUnManaged (ltype, loc))
8265 if (dim != "" && dim [0] == '[' &&
8266 (ltype == TypeManager.arg_iterator_type || ltype == TypeManager.typed_reference_type)) {
8267 Report.Error (611, loc, "Array elements cannot be of type `{0}'", TypeManager.CSharpName (ltype));
8272 type = TypeManager.GetConstructedType (ltype, dim);
8277 throw new InternalErrorException ("Couldn't create computed type " + ltype + dim);
8279 if (type.IsPointer && !ec.IsInUnsafeScope){
8284 eclass = ExprClass.Type;
8288 public override string Name {
8289 get { return left + dim; }
8292 public override string FullName {
8293 get { return type.FullName; }
8296 public override string GetSignatureForError ()
8298 return left.GetSignatureForError () + dim;
8301 protected override void CloneTo (CloneContext clonectx, Expression t)
8303 ComposedCast target = (ComposedCast) t;
8305 target.left = left.Clone (clonectx);
8309 public class FixedBufferPtr : Expression {
8312 public FixedBufferPtr (Expression array, Type array_type, Location l)
8317 type = TypeManager.GetPointerType (array_type);
8318 eclass = ExprClass.Value;
8321 public override void Emit(EmitContext ec)
8326 public override Expression DoResolve (EmitContext ec)
8329 // We are born fully resolved
8337 // This class is used to represent the address of an array, used
8338 // only by the Fixed statement, this generates "&a [0]" construct
8339 // for fixed (char *pa = a)
8341 public class ArrayPtr : FixedBufferPtr {
8344 public ArrayPtr (Expression array, Type array_type, Location l):
8345 base (array, array_type, l)
8347 this.array_type = array_type;
8350 public override void Emit (EmitContext ec)
8354 ILGenerator ig = ec.ig;
8355 IntLiteral.EmitInt (ig, 0);
8356 ig.Emit (OpCodes.Ldelema, array_type);
8361 // Encapsulates a conversion rules required for array indexes
8363 public class ArrayIndexCast : Expression
8367 public ArrayIndexCast (Expression expr)
8370 this.loc = expr.Location;
8373 public override Expression CreateExpressionTree (EmitContext ec)
8375 ArrayList args = new ArrayList (2);
8376 args.Add (new Argument (expr.CreateExpressionTree (ec)));
8377 args.Add (new Argument (new TypeOf (new TypeExpression (TypeManager.int32_type, loc), loc)));
8378 return CreateExpressionFactoryCall ("ConvertChecked", args);
8381 public override Expression DoResolve (EmitContext ec)
8384 eclass = expr.eclass;
8388 public override void Emit (EmitContext ec)
8392 if (type == TypeManager.int32_type)
8395 if (type == TypeManager.uint32_type)
8396 ec.ig.Emit (OpCodes.Conv_U);
8397 else if (type == TypeManager.int64_type)
8398 ec.ig.Emit (OpCodes.Conv_Ovf_I);
8399 else if (type == TypeManager.uint64_type)
8400 ec.ig.Emit (OpCodes.Conv_Ovf_I_Un);
8402 throw new InternalErrorException ("Cannot emit cast to unknown array element type", type);
8407 // Used by the fixed statement
8409 public class StringPtr : Expression {
8412 public StringPtr (LocalBuilder b, Location l)
8415 eclass = ExprClass.Value;
8416 type = TypeManager.char_ptr_type;
8420 public override Expression DoResolve (EmitContext ec)
8422 // This should never be invoked, we are born in fully
8423 // initialized state.
8428 public override void Emit (EmitContext ec)
8430 ILGenerator ig = ec.ig;
8432 ig.Emit (OpCodes.Ldloc, b);
8433 ig.Emit (OpCodes.Conv_I);
8434 ig.Emit (OpCodes.Call, TypeManager.int_get_offset_to_string_data);
8435 ig.Emit (OpCodes.Add);
8440 // Implements the `stackalloc' keyword
8442 public class StackAlloc : Expression {
8447 public StackAlloc (Expression type, Expression count, Location l)
8454 public override Expression DoResolve (EmitContext ec)
8456 count = count.Resolve (ec);
8460 if (count.Type != TypeManager.int32_type){
8461 count = Convert.ImplicitConversionRequired (ec, count, TypeManager.int32_type, loc);
8466 Constant c = count as Constant;
8467 if (c != null && c.IsNegative) {
8468 Report.Error (247, loc, "Cannot use a negative size with stackalloc");
8472 if (ec.InCatch || ec.InFinally) {
8473 Error (255, "Cannot use stackalloc in finally or catch");
8477 TypeExpr texpr = t.ResolveAsTypeTerminal (ec, false);
8483 if (!TypeManager.VerifyUnManaged (otype, loc))
8486 type = TypeManager.GetPointerType (otype);
8487 eclass = ExprClass.Value;
8492 public override void Emit (EmitContext ec)
8494 int size = GetTypeSize (otype);
8495 ILGenerator ig = ec.ig;
8498 ig.Emit (OpCodes.Sizeof, otype);
8500 IntConstant.EmitInt (ig, size);
8502 ig.Emit (OpCodes.Mul);
8503 ig.Emit (OpCodes.Localloc);
8506 protected override void CloneTo (CloneContext clonectx, Expression t)
8508 StackAlloc target = (StackAlloc) t;
8509 target.count = count.Clone (clonectx);
8510 target.t = t.Clone (clonectx);
8515 // An object initializer expression
8517 public class ElementInitializer : Expression
8519 Expression initializer;
8520 public readonly string Name;
8522 public ElementInitializer (string name, Expression initializer, Location loc)
8525 this.initializer = initializer;
8529 protected override void CloneTo (CloneContext clonectx, Expression t)
8531 if (initializer == null)
8534 ElementInitializer target = (ElementInitializer) t;
8535 target.initializer = initializer.Clone (clonectx);
8538 public override Expression DoResolve (EmitContext ec)
8540 if (initializer == null)
8541 return EmptyExpressionStatement.Instance;
8543 MemberExpr element_member = MemberLookupFinal (ec, ec.CurrentInitializerVariable.Type, ec.CurrentInitializerVariable.Type,
8544 Name, MemberTypes.Field | MemberTypes.Property, BindingFlags.Public | BindingFlags.Instance, loc) as MemberExpr;
8546 if (element_member == null)
8549 element_member.InstanceExpression = ec.CurrentInitializerVariable;
8551 if (initializer is CollectionOrObjectInitializers) {
8552 Expression previous = ec.CurrentInitializerVariable;
8553 ec.CurrentInitializerVariable = element_member;
8554 initializer = initializer.Resolve (ec);
8555 ec.CurrentInitializerVariable = previous;
8559 Assign a = new Assign (element_member, initializer, loc);
8560 if (a.Resolve (ec) == null)
8564 // Ignore field initializers with default value
8566 Constant c = a.Source as Constant;
8567 if (c != null && c.IsDefaultInitializer (a.Type) && a.Target.eclass == ExprClass.Variable)
8568 return EmptyExpressionStatement.Instance;
8573 protected override Expression Error_MemberLookupFailed (MemberInfo[] members)
8575 MemberInfo member = members [0];
8576 if (member.MemberType != MemberTypes.Property && member.MemberType != MemberTypes.Field)
8577 Report.Error (1913, loc, "Member `{0}' cannot be initialized. An object " +
8578 "initializer may only be used for fields, or properties", TypeManager.GetFullNameSignature (member));
8580 Report.Error (1914, loc, " Static field or property `{0}' cannot be assigned in an object initializer",
8581 TypeManager.GetFullNameSignature (member));
8586 public override void Emit (EmitContext ec)
8588 throw new NotSupportedException ("Should not be reached");
8593 // A collection initializer expression
8595 public class CollectionElementInitializer : Expression
8597 public class ElementInitializerArgument : Argument
8599 public ElementInitializerArgument (Expression e)
8605 ArrayList arguments;
8607 public CollectionElementInitializer (Expression argument)
8609 arguments = new ArrayList (1);
8610 arguments.Add (argument);
8611 this.loc = argument.Location;
8614 public CollectionElementInitializer (ArrayList arguments, Location loc)
8616 this.arguments = arguments;
8620 protected override void CloneTo (CloneContext clonectx, Expression t)
8622 CollectionElementInitializer target = (CollectionElementInitializer) t;
8623 ArrayList t_arguments = target.arguments = new ArrayList (arguments.Count);
8624 foreach (Expression e in arguments)
8625 t_arguments.Add (e.Clone (clonectx));
8628 public override Expression DoResolve (EmitContext ec)
8630 // TODO: We should call a constructor which takes element counts argument,
8631 // for know types like List<T>, Dictionary<T, U>
8633 for (int i = 0; i < arguments.Count; ++i)
8634 arguments [i] = new ElementInitializerArgument ((Expression)arguments [i]);
8636 Expression add_method = new Invocation (
8637 new MemberAccess (ec.CurrentInitializerVariable, "Add", loc),
8640 add_method = add_method.Resolve (ec);
8645 public override void Emit (EmitContext ec)
8647 throw new NotSupportedException ("Should not be reached");
8652 // A block of object or collection initializers
8654 public class CollectionOrObjectInitializers : ExpressionStatement
8656 ArrayList initializers;
8658 public static readonly CollectionOrObjectInitializers Empty =
8659 new CollectionOrObjectInitializers (new ArrayList (0), Location.Null);
8661 public CollectionOrObjectInitializers (ArrayList initializers, Location loc)
8663 this.initializers = initializers;
8667 public bool IsEmpty {
8669 return initializers.Count == 0;
8673 protected override void CloneTo (CloneContext clonectx, Expression target)
8675 CollectionOrObjectInitializers t = (CollectionOrObjectInitializers) target;
8677 t.initializers = new ArrayList (initializers.Count);
8678 foreach (Expression e in initializers)
8679 t.initializers.Add (e.Clone (clonectx));
8682 public override Expression DoResolve (EmitContext ec)
8684 bool is_elements_initialization = false;
8685 ArrayList element_names = null;
8686 for (int i = 0; i < initializers.Count; ++i) {
8687 Expression initializer = (Expression) initializers [i];
8688 ElementInitializer element_initializer = initializer as ElementInitializer;
8691 if (element_initializer != null) {
8692 is_elements_initialization = true;
8693 element_names = new ArrayList (initializers.Count);
8694 element_names.Add (element_initializer.Name);
8696 if (!TypeManager.ImplementsInterface (ec.CurrentInitializerVariable.Type,
8697 TypeManager.ienumerable_type)) {
8698 Report.Error (1922, loc, "A field or property `{0}' cannot be initialized with a collection " +
8699 "object initializer because type `{1}' does not implement `{2}' interface",
8700 ec.CurrentInitializerVariable.GetSignatureForError (),
8701 TypeManager.CSharpName (ec.CurrentInitializerVariable.Type),
8702 TypeManager.CSharpName (TypeManager.ienumerable_type));
8707 if (is_elements_initialization == (element_initializer == null)) {
8708 Report.Error (747, initializer.Location, "Inconsistent `{0}' member declaration",
8709 is_elements_initialization ? "object initializer" : "collection initializer");
8713 if (is_elements_initialization) {
8714 if (element_names.Contains (element_initializer.Name)) {
8715 Report.Error (1912, element_initializer.Location,
8716 "An object initializer includes more than one member `{0}' initialization",
8717 element_initializer.Name);
8719 element_names.Add (element_initializer.Name);
8724 Expression e = initializer.Resolve (ec);
8725 if (e == EmptyExpressionStatement.Instance)
8726 initializers.RemoveAt (i--);
8728 initializers [i] = e;
8731 type = typeof (CollectionOrObjectInitializers);
8732 eclass = ExprClass.Variable;
8736 public override void Emit (EmitContext ec)
8741 public override void EmitStatement (EmitContext ec)
8743 foreach (ExpressionStatement e in initializers)
8744 e.EmitStatement (ec);
8749 // New expression with element/object initializers
8751 public class NewInitialize : New
8754 // This class serves as a proxy for variable initializer target instances.
8755 // A real variable is assigned later when we resolve left side of an
8758 sealed class InitializerTargetExpression : Expression, IMemoryLocation
8760 NewInitialize new_instance;
8762 public InitializerTargetExpression (NewInitialize newInstance)
8764 this.type = newInstance.type;
8765 this.loc = newInstance.loc;
8766 this.eclass = newInstance.eclass;
8767 this.new_instance = newInstance;
8770 public override Expression DoResolve (EmitContext ec)
8775 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
8780 public override void Emit (EmitContext ec)
8782 new_instance.value_target.Emit (ec);
8785 #region IMemoryLocation Members
8787 public void AddressOf (EmitContext ec, AddressOp mode)
8789 ((IMemoryLocation)new_instance.value_target).AddressOf (ec, mode);
8795 CollectionOrObjectInitializers initializers;
8797 public NewInitialize (Expression requested_type, ArrayList arguments, CollectionOrObjectInitializers initializers, Location l)
8798 : base (requested_type, arguments, l)
8800 this.initializers = initializers;
8803 protected override void CloneTo (CloneContext clonectx, Expression t)
8805 base.CloneTo (clonectx, t);
8807 NewInitialize target = (NewInitialize) t;
8808 target.initializers = (CollectionOrObjectInitializers) initializers.Clone (clonectx);
8811 public override Expression DoResolve (EmitContext ec)
8813 if (eclass != ExprClass.Invalid)
8816 Expression e = base.DoResolve (ec);
8820 // Empty initializer can be optimized to simple new
8821 if (initializers.IsEmpty)
8824 Expression previous = ec.CurrentInitializerVariable;
8825 ec.CurrentInitializerVariable = new InitializerTargetExpression (this);
8826 initializers.Resolve (ec);
8827 ec.CurrentInitializerVariable = previous;
8831 public override void Emit (EmitContext ec)
8836 // If target is a value, let's use it
8838 VariableReference variable = value_target as VariableReference;
8839 if (variable != null) {
8841 StoreFromPtr (ec.ig, type);
8843 variable.Variable.EmitAssign (ec);
8845 if (value_target == null || value_target_set)
8846 value_target = new LocalTemporary (type);
8848 ((LocalTemporary) value_target).Store (ec);
8851 initializers.Emit (ec);
8853 if (variable == null)
8854 value_target.Emit (ec);
8857 public override void EmitStatement (EmitContext ec)
8859 if (initializers.IsEmpty) {
8860 base.EmitStatement (ec);
8866 if (value_target == null) {
8867 LocalTemporary variable = new LocalTemporary (type);
8868 variable.Store (ec);
8869 value_target = variable;
8872 initializers.EmitStatement (ec);
8875 public override bool HasInitializer {
8877 return !initializers.IsEmpty;
8882 public class AnonymousTypeDeclaration : Expression
8884 ArrayList parameters;
8885 readonly TypeContainer parent;
8886 static readonly ArrayList EmptyParameters = new ArrayList (0);
8888 public AnonymousTypeDeclaration (ArrayList parameters, TypeContainer parent, Location loc)
8890 this.parameters = parameters;
8891 this.parent = parent;
8895 protected override void CloneTo (CloneContext clonectx, Expression target)
8897 if (parameters == null)
8900 AnonymousTypeDeclaration t = (AnonymousTypeDeclaration) target;
8901 t.parameters = new ArrayList (parameters.Count);
8902 foreach (AnonymousTypeParameter atp in parameters)
8903 t.parameters.Add (atp.Clone (clonectx));
8906 AnonymousTypeClass CreateAnonymousType (ArrayList parameters)
8908 AnonymousTypeClass type = RootContext.ToplevelTypes.GetAnonymousType (parameters);
8912 type = AnonymousTypeClass.Create (parent, parameters, loc);
8917 type.DefineMembers ();
8921 RootContext.ToplevelTypes.AddAnonymousType (type);
8925 public override Expression DoResolve (EmitContext ec)
8927 AnonymousTypeClass anonymous_type;
8929 if (parameters == null) {
8930 anonymous_type = CreateAnonymousType (EmptyParameters);
8931 return new New (new TypeExpression (anonymous_type.TypeBuilder, loc),
8932 null, loc).Resolve (ec);
8936 ArrayList arguments = new ArrayList (parameters.Count);
8937 TypeExpression [] t_args = new TypeExpression [parameters.Count];
8938 for (int i = 0; i < parameters.Count; ++i) {
8939 Expression e = ((AnonymousTypeParameter) parameters [i]).Resolve (ec);
8945 arguments.Add (new Argument (e));
8946 t_args [i] = new TypeExpression (e.Type, e.Location);
8952 anonymous_type = CreateAnonymousType (parameters);
8953 if (anonymous_type == null)
8956 ConstructedType te = new ConstructedType (anonymous_type.TypeBuilder,
8957 new TypeArguments (loc, t_args), loc);
8959 return new New (te, arguments, loc).Resolve (ec);
8962 public override void Emit (EmitContext ec)
8964 throw new InternalErrorException ("Should not be reached");
8968 public class AnonymousTypeParameter : Expression
8970 public readonly string Name;
8971 Expression initializer;
8973 public AnonymousTypeParameter (Expression initializer, string name, Location loc)
8977 this.initializer = initializer;
8980 public AnonymousTypeParameter (Parameter parameter)
8982 this.Name = parameter.Name;
8983 this.loc = parameter.Location;
8984 this.initializer = new SimpleName (Name, loc);
8987 protected override void CloneTo (CloneContext clonectx, Expression target)
8989 AnonymousTypeParameter t = (AnonymousTypeParameter) target;
8990 t.initializer = initializer.Clone (clonectx);
8993 public override bool Equals (object o)
8995 AnonymousTypeParameter other = o as AnonymousTypeParameter;
8996 return other != null && Name == other.Name;
8999 public override int GetHashCode ()
9001 return Name.GetHashCode ();
9004 public override Expression DoResolve (EmitContext ec)
9006 Expression e = initializer.Resolve (ec);
9011 if (type == TypeManager.void_type || type == TypeManager.null_type ||
9012 type == TypeManager.anonymous_method_type || type.IsPointer) {
9013 Error_InvalidInitializer (e);
9020 protected virtual void Error_InvalidInitializer (Expression initializer)
9022 Report.Error (828, loc, "An anonymous type property `{0}' cannot be initialized with `{1}'",
9023 Name, initializer.GetSignatureForError ());
9026 public override void Emit (EmitContext ec)
9028 throw new InternalErrorException ("Should not be reached");