2 // expression.cs: Expression representation for the IL tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001, 2002, 2003 Ximian, Inc.
8 // (C) 2003, 2004 Novell, Inc.
12 namespace Mono.CSharp {
14 using System.Collections;
15 using System.Reflection;
16 using System.Reflection.Emit;
20 /// This is just a helper class, it is generated by Unary, UnaryMutator
21 /// when an overloaded method has been found. It just emits the code for a
24 public class StaticCallExpr : ExpressionStatement {
28 public StaticCallExpr (MethodInfo m, ArrayList a, Location l)
34 eclass = ExprClass.Value;
38 public override Expression DoResolve (EmitContext ec)
41 // We are born fully resolved
46 public override void Emit (EmitContext ec)
49 Invocation.EmitArguments (ec, mi, args, false, null);
51 ec.ig.Emit (OpCodes.Call, mi);
55 static public StaticCallExpr MakeSimpleCall (EmitContext ec, MethodGroupExpr mg,
56 Expression e, Location loc)
61 args = new ArrayList (1);
62 Argument a = new Argument (e, Argument.AType.Expression);
64 // We need to resolve the arguments before sending them in !
65 if (!a.Resolve (ec, loc))
69 method = Invocation.OverloadResolve (
70 ec, (MethodGroupExpr) mg, args, false, loc);
75 return new StaticCallExpr ((MethodInfo) method, args, loc);
78 public override void EmitStatement (EmitContext ec)
81 if (TypeManager.TypeToCoreType (type) != TypeManager.void_type)
82 ec.ig.Emit (OpCodes.Pop);
85 public MethodInfo Method {
90 public class ParenthesizedExpression : Expression
92 public Expression Expr;
94 public ParenthesizedExpression (Expression expr)
97 this.loc = expr.Location;
100 public override Expression DoResolve (EmitContext ec)
102 Expr = Expr.Resolve (ec);
106 public override void Emit (EmitContext ec)
108 throw new Exception ("Should not happen");
113 /// Unary expressions.
117 /// Unary implements unary expressions. It derives from
118 /// ExpressionStatement becuase the pre/post increment/decrement
119 /// operators can be used in a statement context.
121 public class Unary : Expression {
122 public enum Operator : byte {
123 UnaryPlus, UnaryNegation, LogicalNot, OnesComplement,
124 Indirection, AddressOf, TOP
127 public Operator Oper;
128 public Expression Expr;
130 public Unary (Operator op, Expression expr, Location loc)
138 /// Returns a stringified representation of the Operator
140 static public string OperName (Operator oper)
143 case Operator.UnaryPlus:
145 case Operator.UnaryNegation:
147 case Operator.LogicalNot:
149 case Operator.OnesComplement:
151 case Operator.AddressOf:
153 case Operator.Indirection:
157 return oper.ToString ();
160 public static readonly string [] oper_names;
164 oper_names = new string [(int)Operator.TOP];
166 oper_names [(int) Operator.UnaryPlus] = "op_UnaryPlus";
167 oper_names [(int) Operator.UnaryNegation] = "op_UnaryNegation";
168 oper_names [(int) Operator.LogicalNot] = "op_LogicalNot";
169 oper_names [(int) Operator.OnesComplement] = "op_OnesComplement";
170 oper_names [(int) Operator.Indirection] = "op_Indirection";
171 oper_names [(int) Operator.AddressOf] = "op_AddressOf";
174 void Error23 (Type t)
176 Report.Error (23, loc, "Operator `{0}' cannot be applied to operand of type `{1}'",
177 OperName (Oper), TypeManager.CSharpName (t));
181 /// The result has been already resolved:
183 /// FIXME: a minus constant -128 sbyte cant be turned into a
186 static Expression TryReduceNegative (Constant expr)
190 if (expr is IntConstant)
191 e = new IntConstant (-((IntConstant) expr).Value);
192 else if (expr is UIntConstant){
193 uint value = ((UIntConstant) expr).Value;
195 if (value < 2147483649)
196 return new IntConstant (-(int)value);
198 e = new LongConstant (-value);
200 else if (expr is LongConstant)
201 e = new LongConstant (-((LongConstant) expr).Value);
202 else if (expr is ULongConstant){
203 ulong value = ((ULongConstant) expr).Value;
205 if (value < 9223372036854775809)
206 return new LongConstant(-(long)value);
208 else if (expr is FloatConstant)
209 e = new FloatConstant (-((FloatConstant) expr).Value);
210 else if (expr is DoubleConstant)
211 e = new DoubleConstant (-((DoubleConstant) expr).Value);
212 else if (expr is DecimalConstant)
213 e = new DecimalConstant (-((DecimalConstant) expr).Value);
214 else if (expr is ShortConstant)
215 e = new IntConstant (-((ShortConstant) expr).Value);
216 else if (expr is UShortConstant)
217 e = new IntConstant (-((UShortConstant) expr).Value);
218 else if (expr is SByteConstant)
219 e = new IntConstant (-((SByteConstant) expr).Value);
220 else if (expr is ByteConstant)
221 e = new IntConstant (-((ByteConstant) expr).Value);
226 // This routine will attempt to simplify the unary expression when the
227 // argument is a constant. The result is returned in `result' and the
228 // function returns true or false depending on whether a reduction
229 // was performed or not
231 bool Reduce (EmitContext ec, Constant e, out Expression result)
233 Type expr_type = e.Type;
236 case Operator.UnaryPlus:
237 if (expr_type == TypeManager.bool_type){
246 case Operator.UnaryNegation:
247 result = TryReduceNegative (e);
248 return result != null;
250 case Operator.LogicalNot:
251 if (expr_type != TypeManager.bool_type) {
257 BoolConstant b = (BoolConstant) e;
258 result = new BoolConstant (!(b.Value));
261 case Operator.OnesComplement:
262 if (!((expr_type == TypeManager.int32_type) ||
263 (expr_type == TypeManager.uint32_type) ||
264 (expr_type == TypeManager.int64_type) ||
265 (expr_type == TypeManager.uint64_type) ||
266 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
269 if (Convert.ImplicitConversionExists (ec, e, TypeManager.int32_type)){
270 result = new Cast (new TypeExpression (TypeManager.int32_type, loc), e, loc);
271 result = result.Resolve (ec);
272 } else if (Convert.ImplicitConversionExists (ec, e, TypeManager.uint32_type)){
273 result = new Cast (new TypeExpression (TypeManager.uint32_type, loc), e, loc);
274 result = result.Resolve (ec);
275 } else if (Convert.ImplicitConversionExists (ec, e, TypeManager.int64_type)){
276 result = new Cast (new TypeExpression (TypeManager.int64_type, loc), e, loc);
277 result = result.Resolve (ec);
278 } else if (Convert.ImplicitConversionExists (ec, e, TypeManager.uint64_type)){
279 result = new Cast (new TypeExpression (TypeManager.uint64_type, loc), e, loc);
280 result = result.Resolve (ec);
283 if (result == null || !(result is Constant)){
289 expr_type = result.Type;
290 e = (Constant) result;
293 if (e is EnumConstant){
294 EnumConstant enum_constant = (EnumConstant) e;
297 if (Reduce (ec, enum_constant.Child, out reduced)){
298 result = new EnumConstant ((Constant) reduced, enum_constant.Type);
306 if (expr_type == TypeManager.int32_type){
307 result = new IntConstant (~ ((IntConstant) e).Value);
308 } else if (expr_type == TypeManager.uint32_type){
309 result = new UIntConstant (~ ((UIntConstant) e).Value);
310 } else if (expr_type == TypeManager.int64_type){
311 result = new LongConstant (~ ((LongConstant) e).Value);
312 } else if (expr_type == TypeManager.uint64_type){
313 result = new ULongConstant (~ ((ULongConstant) e).Value);
321 case Operator.AddressOf:
325 case Operator.Indirection:
329 throw new Exception ("Can not constant fold: " + Oper.ToString());
332 Expression ResolveOperator (EmitContext ec)
335 // Step 1: Default operations on CLI native types.
338 // Attempt to use a constant folding operation.
339 if (Expr is Constant){
342 if (Reduce (ec, (Constant) Expr, out result))
347 // Step 2: Perform Operator Overload location
349 Type expr_type = Expr.Type;
353 op_name = oper_names [(int) Oper];
355 mg = MemberLookup (ec, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
358 Expression e = StaticCallExpr.MakeSimpleCall (
359 ec, (MethodGroupExpr) mg, Expr, loc);
369 // Only perform numeric promotions on:
372 if (expr_type == null)
376 case Operator.LogicalNot:
377 if (expr_type != TypeManager.bool_type) {
378 Expr = ResolveBoolean (ec, Expr, loc);
385 type = TypeManager.bool_type;
388 case Operator.OnesComplement:
389 if (!((expr_type == TypeManager.int32_type) ||
390 (expr_type == TypeManager.uint32_type) ||
391 (expr_type == TypeManager.int64_type) ||
392 (expr_type == TypeManager.uint64_type) ||
393 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
396 e = Convert.ImplicitConversion (ec, Expr, TypeManager.int32_type, loc);
399 e = Convert.ImplicitConversion (ec, Expr, TypeManager.uint32_type, loc);
402 e = Convert.ImplicitConversion (ec, Expr, TypeManager.int64_type, loc);
405 e = Convert.ImplicitConversion (ec, Expr, TypeManager.uint64_type, loc);
418 case Operator.AddressOf:
424 if (!TypeManager.VerifyUnManaged (Expr.Type, loc)){
428 IVariable variable = Expr as IVariable;
429 bool is_fixed = variable != null && variable.VerifyFixed ();
431 if (!ec.InFixedInitializer && !is_fixed) {
432 Error (212, "You can only take the address of unfixed expression inside " +
433 "of a fixed statement initializer");
437 if (ec.InFixedInitializer && is_fixed) {
438 Error (213, "You cannot use the fixed statement to take the address of an already fixed expression");
442 LocalVariableReference lr = Expr as LocalVariableReference;
444 if (lr.local_info.IsCaptured){
445 AnonymousMethod.Error_AddressOfCapturedVar (lr.Name, loc);
448 lr.local_info.AddressTaken = true;
449 lr.local_info.Used = true;
452 // According to the specs, a variable is considered definitely assigned if you take
454 if ((variable != null) && (variable.VariableInfo != null))
455 variable.VariableInfo.SetAssigned (ec);
457 type = TypeManager.GetPointerType (Expr.Type);
460 case Operator.Indirection:
466 if (!expr_type.IsPointer){
467 Error (193, "The * or -> operator must be applied to a pointer");
472 // We create an Indirection expression, because
473 // it can implement the IMemoryLocation.
475 return new Indirection (Expr, loc);
477 case Operator.UnaryPlus:
479 // A plus in front of something is just a no-op, so return the child.
483 case Operator.UnaryNegation:
485 // Deals with -literals
486 // int operator- (int x)
487 // long operator- (long x)
488 // float operator- (float f)
489 // double operator- (double d)
490 // decimal operator- (decimal d)
492 Expression expr = null;
495 // transform - - expr into expr
498 Unary unary = (Unary) Expr;
500 if (unary.Oper == Operator.UnaryNegation)
505 // perform numeric promotions to int,
509 // The following is inneficient, because we call
510 // ImplicitConversion too many times.
512 // It is also not clear if we should convert to Float
513 // or Double initially.
515 if (expr_type == TypeManager.uint32_type){
517 // FIXME: handle exception to this rule that
518 // permits the int value -2147483648 (-2^31) to
519 // bt wrote as a decimal interger literal
521 type = TypeManager.int64_type;
522 Expr = Convert.ImplicitConversion (ec, Expr, type, loc);
526 if (expr_type == TypeManager.uint64_type){
528 // FIXME: Handle exception of `long value'
529 // -92233720368547758087 (-2^63) to be wrote as
530 // decimal integer literal.
536 if (expr_type == TypeManager.float_type){
541 expr = Convert.ImplicitConversion (ec, Expr, TypeManager.int32_type, loc);
548 expr = Convert.ImplicitConversion (ec, Expr, TypeManager.int64_type, loc);
555 expr = Convert.ImplicitConversion (ec, Expr, TypeManager.double_type, loc);
566 Error (187, "No such operator '" + OperName (Oper) + "' defined for type '" +
567 TypeManager.CSharpName (expr_type) + "'");
571 public override Expression DoResolve (EmitContext ec)
573 if (Oper == Operator.AddressOf) {
574 Expr = Expr.DoResolveLValue (ec, new EmptyExpression ());
576 if (Expr == null || Expr.eclass != ExprClass.Variable){
577 Error (211, "Cannot take the address of the given expression");
582 Expr = Expr.Resolve (ec);
587 if (TypeManager.IsNullableType (Expr.Type))
588 return new Nullable.LiftedUnaryOperator (Oper, Expr, loc).Resolve (ec);
590 eclass = ExprClass.Value;
591 return ResolveOperator (ec);
594 public override Expression DoResolveLValue (EmitContext ec, Expression right)
596 if (Oper == Operator.Indirection)
597 return DoResolve (ec);
602 public override void Emit (EmitContext ec)
604 ILGenerator ig = ec.ig;
607 case Operator.UnaryPlus:
608 throw new Exception ("This should be caught by Resolve");
610 case Operator.UnaryNegation:
612 ig.Emit (OpCodes.Ldc_I4_0);
613 if (type == TypeManager.int64_type)
614 ig.Emit (OpCodes.Conv_U8);
616 ig.Emit (OpCodes.Sub_Ovf);
619 ig.Emit (OpCodes.Neg);
624 case Operator.LogicalNot:
626 ig.Emit (OpCodes.Ldc_I4_0);
627 ig.Emit (OpCodes.Ceq);
630 case Operator.OnesComplement:
632 ig.Emit (OpCodes.Not);
635 case Operator.AddressOf:
636 ((IMemoryLocation)Expr).AddressOf (ec, AddressOp.LoadStore);
640 throw new Exception ("This should not happen: Operator = "
645 public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
647 if (Oper == Operator.LogicalNot)
648 Expr.EmitBranchable (ec, target, !onTrue);
650 base.EmitBranchable (ec, target, onTrue);
653 public override string ToString ()
655 return "Unary (" + Oper + ", " + Expr + ")";
661 // Unary operators are turned into Indirection expressions
662 // after semantic analysis (this is so we can take the address
663 // of an indirection).
665 public class Indirection : Expression, IMemoryLocation, IAssignMethod, IVariable {
667 LocalTemporary temporary;
670 public Indirection (Expression expr, Location l)
673 type = TypeManager.HasElementType (expr.Type) ? TypeManager.GetElementType (expr.Type) : expr.Type;
674 eclass = ExprClass.Variable;
678 public override void Emit (EmitContext ec)
683 LoadFromPtr (ec.ig, Type);
686 public void Emit (EmitContext ec, bool leave_copy)
690 ec.ig.Emit (OpCodes.Dup);
691 temporary = new LocalTemporary (ec, expr.Type);
692 temporary.Store (ec);
696 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
698 prepared = prepare_for_load;
702 if (prepare_for_load)
703 ec.ig.Emit (OpCodes.Dup);
707 ec.ig.Emit (OpCodes.Dup);
708 temporary = new LocalTemporary (ec, expr.Type);
709 temporary.Store (ec);
712 StoreFromPtr (ec.ig, type);
714 if (temporary != null)
718 public void AddressOf (EmitContext ec, AddressOp Mode)
723 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
725 return DoResolve (ec);
728 public override Expression DoResolve (EmitContext ec)
731 // Born fully resolved
736 public override string ToString ()
738 return "*(" + expr + ")";
741 #region IVariable Members
743 public VariableInfo VariableInfo {
749 public bool VerifyFixed ()
751 // A pointer-indirection is always fixed.
759 /// Unary Mutator expressions (pre and post ++ and --)
763 /// UnaryMutator implements ++ and -- expressions. It derives from
764 /// ExpressionStatement becuase the pre/post increment/decrement
765 /// operators can be used in a statement context.
767 /// FIXME: Idea, we could split this up in two classes, one simpler
768 /// for the common case, and one with the extra fields for more complex
769 /// classes (indexers require temporary access; overloaded require method)
772 public class UnaryMutator : ExpressionStatement {
774 public enum Mode : byte {
781 PreDecrement = IsDecrement,
782 PostIncrement = IsPost,
783 PostDecrement = IsPost | IsDecrement
787 bool is_expr = false;
788 bool recurse = false;
793 // This is expensive for the simplest case.
795 StaticCallExpr method;
797 public UnaryMutator (Mode m, Expression e, Location l)
804 static string OperName (Mode mode)
806 return (mode == Mode.PreIncrement || mode == Mode.PostIncrement) ?
811 /// Returns whether an object of type `t' can be incremented
812 /// or decremented with add/sub (ie, basically whether we can
813 /// use pre-post incr-decr operations on it, but it is not a
814 /// System.Decimal, which we require operator overloading to catch)
816 static bool IsIncrementableNumber (Type t)
818 return (t == TypeManager.sbyte_type) ||
819 (t == TypeManager.byte_type) ||
820 (t == TypeManager.short_type) ||
821 (t == TypeManager.ushort_type) ||
822 (t == TypeManager.int32_type) ||
823 (t == TypeManager.uint32_type) ||
824 (t == TypeManager.int64_type) ||
825 (t == TypeManager.uint64_type) ||
826 (t == TypeManager.char_type) ||
827 (t.IsSubclassOf (TypeManager.enum_type)) ||
828 (t == TypeManager.float_type) ||
829 (t == TypeManager.double_type) ||
830 (t.IsPointer && t != TypeManager.void_ptr_type);
833 Expression ResolveOperator (EmitContext ec)
835 Type expr_type = expr.Type;
838 // Step 1: Perform Operator Overload location
843 if (mode == Mode.PreIncrement || mode == Mode.PostIncrement)
844 op_name = "op_Increment";
846 op_name = "op_Decrement";
848 mg = MemberLookup (ec, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
851 method = StaticCallExpr.MakeSimpleCall (
852 ec, (MethodGroupExpr) mg, expr, loc);
855 } else if (!IsIncrementableNumber (expr_type)) {
856 Error (187, "No such operator '" + OperName (mode) + "' defined for type '" +
857 TypeManager.CSharpName (expr_type) + "'");
862 // The operand of the prefix/postfix increment decrement operators
863 // should be an expression that is classified as a variable,
864 // a property access or an indexer access
867 if (expr.eclass == ExprClass.Variable){
868 LocalVariableReference var = expr as LocalVariableReference;
869 if ((var != null) && var.IsReadOnly) {
870 Error (1604, "cannot assign to `" + var.Name + "' because it is readonly");
873 } else if (expr.eclass == ExprClass.IndexerAccess || expr.eclass == ExprClass.PropertyAccess){
874 expr = expr.ResolveLValue (ec, this, Location);
878 expr.Error_UnexpectedKind (ec, "variable, indexer or property access", loc);
885 public override Expression DoResolve (EmitContext ec)
887 expr = expr.Resolve (ec);
892 eclass = ExprClass.Value;
894 if (TypeManager.IsNullableType (expr.Type))
895 return new Nullable.LiftedUnaryMutator (mode, expr, loc).Resolve (ec);
897 return ResolveOperator (ec);
900 static int PtrTypeSize (Type t)
902 return GetTypeSize (TypeManager.GetElementType (t));
906 // Loads the proper "1" into the stack based on the type, then it emits the
907 // opcode for the operation requested
909 void LoadOneAndEmitOp (EmitContext ec, Type t)
912 // Measure if getting the typecode and using that is more/less efficient
913 // that comparing types. t.GetTypeCode() is an internal call.
915 ILGenerator ig = ec.ig;
917 if (t == TypeManager.uint64_type || t == TypeManager.int64_type)
918 LongConstant.EmitLong (ig, 1);
919 else if (t == TypeManager.double_type)
920 ig.Emit (OpCodes.Ldc_R8, 1.0);
921 else if (t == TypeManager.float_type)
922 ig.Emit (OpCodes.Ldc_R4, 1.0F);
923 else if (t.IsPointer){
924 int n = PtrTypeSize (t);
927 ig.Emit (OpCodes.Sizeof, t);
929 IntConstant.EmitInt (ig, n);
931 ig.Emit (OpCodes.Ldc_I4_1);
934 // Now emit the operation
937 if (t == TypeManager.int32_type ||
938 t == TypeManager.int64_type){
939 if ((mode & Mode.IsDecrement) != 0)
940 ig.Emit (OpCodes.Sub_Ovf);
942 ig.Emit (OpCodes.Add_Ovf);
943 } else if (t == TypeManager.uint32_type ||
944 t == TypeManager.uint64_type){
945 if ((mode & Mode.IsDecrement) != 0)
946 ig.Emit (OpCodes.Sub_Ovf_Un);
948 ig.Emit (OpCodes.Add_Ovf_Un);
950 if ((mode & Mode.IsDecrement) != 0)
951 ig.Emit (OpCodes.Sub_Ovf);
953 ig.Emit (OpCodes.Add_Ovf);
956 if ((mode & Mode.IsDecrement) != 0)
957 ig.Emit (OpCodes.Sub);
959 ig.Emit (OpCodes.Add);
962 if (t == TypeManager.sbyte_type){
964 ig.Emit (OpCodes.Conv_Ovf_I1);
966 ig.Emit (OpCodes.Conv_I1);
967 } else if (t == TypeManager.byte_type){
969 ig.Emit (OpCodes.Conv_Ovf_U1);
971 ig.Emit (OpCodes.Conv_U1);
972 } else if (t == TypeManager.short_type){
974 ig.Emit (OpCodes.Conv_Ovf_I2);
976 ig.Emit (OpCodes.Conv_I2);
977 } else if (t == TypeManager.ushort_type || t == TypeManager.char_type){
979 ig.Emit (OpCodes.Conv_Ovf_U2);
981 ig.Emit (OpCodes.Conv_U2);
986 void EmitCode (EmitContext ec, bool is_expr)
989 this.is_expr = is_expr;
990 ((IAssignMethod) expr).EmitAssign (ec, this, is_expr && (mode == Mode.PreIncrement || mode == Mode.PreDecrement), true);
993 public override void Emit (EmitContext ec)
996 // We use recurse to allow ourselfs to be the source
997 // of an assignment. This little hack prevents us from
998 // having to allocate another expression
1001 ((IAssignMethod) expr).Emit (ec, is_expr && (mode == Mode.PostIncrement || mode == Mode.PostDecrement));
1003 LoadOneAndEmitOp (ec, expr.Type);
1005 ec.ig.Emit (OpCodes.Call, method.Method);
1010 EmitCode (ec, true);
1013 public override void EmitStatement (EmitContext ec)
1015 EmitCode (ec, false);
1020 /// Base class for the `Is' and `As' classes.
1024 /// FIXME: Split this in two, and we get to save the `Operator' Oper
1027 public abstract class Probe : Expression {
1028 public Expression ProbeType;
1029 protected Expression expr;
1030 protected Type probe_type;
1032 public Probe (Expression expr, Expression probe_type, Location l)
1034 ProbeType = probe_type;
1039 public Expression Expr {
1045 public override Expression DoResolve (EmitContext ec)
1047 TypeExpr texpr = ProbeType.ResolveAsTypeTerminal (ec);
1050 probe_type = texpr.Type;
1052 CheckObsoleteAttribute (probe_type);
1054 expr = expr.Resolve (ec);
1058 if (expr.Type.IsPointer) {
1059 Report.Error (244, loc, "\"is\" or \"as\" are not valid on pointer types");
1067 /// Implementation of the `is' operator.
1069 public class Is : Probe {
1070 public Is (Expression expr, Expression probe_type, Location l)
1071 : base (expr, probe_type, l)
1076 AlwaysTrue, AlwaysNull, AlwaysFalse, LeaveOnStack, Probe
1081 public override void Emit (EmitContext ec)
1083 ILGenerator ig = ec.ig;
1088 case Action.AlwaysFalse:
1089 ig.Emit (OpCodes.Pop);
1090 IntConstant.EmitInt (ig, 0);
1092 case Action.AlwaysTrue:
1093 ig.Emit (OpCodes.Pop);
1094 IntConstant.EmitInt (ig, 1);
1096 case Action.LeaveOnStack:
1097 // the `e != null' rule.
1098 ig.Emit (OpCodes.Ldnull);
1099 ig.Emit (OpCodes.Ceq);
1100 ig.Emit (OpCodes.Ldc_I4_0);
1101 ig.Emit (OpCodes.Ceq);
1104 ig.Emit (OpCodes.Isinst, probe_type);
1105 ig.Emit (OpCodes.Ldnull);
1106 ig.Emit (OpCodes.Cgt_Un);
1109 throw new Exception ("never reached");
1112 public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
1114 ILGenerator ig = ec.ig;
1117 case Action.AlwaysFalse:
1119 ig.Emit (OpCodes.Br, target);
1122 case Action.AlwaysTrue:
1124 ig.Emit (OpCodes.Br, target);
1127 case Action.LeaveOnStack:
1128 // the `e != null' rule.
1130 ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
1134 ig.Emit (OpCodes.Isinst, probe_type);
1135 ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
1138 throw new Exception ("never reached");
1141 public override Expression DoResolve (EmitContext ec)
1143 Expression e = base.DoResolve (ec);
1145 if ((e == null) || (expr == null))
1148 Type etype = expr.Type;
1149 bool warning_always_matches = false;
1150 bool warning_never_matches = false;
1152 type = TypeManager.bool_type;
1153 eclass = ExprClass.Value;
1156 // First case, if at compile time, there is an implicit conversion
1157 // then e != null (objects) or true (value types)
1159 e = Convert.ImplicitConversionStandard (ec, expr, probe_type, loc);
1160 if (e != null && !(e is NullCast)){
1162 if (etype.IsValueType)
1163 action = Action.AlwaysTrue;
1165 action = Action.LeaveOnStack;
1167 warning_always_matches = true;
1168 } else if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
1169 if (etype.IsGenericParameter)
1170 expr = new BoxedCast (expr, etype);
1173 // Second case: explicit reference convresion
1175 if (expr is NullLiteral)
1176 action = Action.AlwaysFalse;
1178 action = Action.Probe;
1180 action = Action.AlwaysFalse;
1181 warning_never_matches = true;
1184 if (warning_always_matches)
1185 Report.Warning (183, 1, loc, "The given expression is always of the provided (`{0}') type", TypeManager.CSharpName (probe_type));
1186 else if (warning_never_matches){
1187 if (!(probe_type.IsInterface || expr.Type.IsInterface))
1188 Report.Warning (184, 1, loc, "The given expression is never of the provided (`{0}') type", TypeManager.CSharpName (probe_type));
1196 /// Implementation of the `as' operator.
1198 public class As : Probe {
1199 public As (Expression expr, Expression probe_type, Location l)
1200 : base (expr, probe_type, l)
1204 bool do_isinst = false;
1206 public override void Emit (EmitContext ec)
1208 ILGenerator ig = ec.ig;
1213 ig.Emit (OpCodes.Isinst, probe_type);
1216 static void Error_CannotConvertType (Type source, Type target, Location loc)
1218 Report.Error (39, loc, "Cannot convert type `{0}' to `{1}' via a built-in conversion",
1219 TypeManager.CSharpName (source),
1220 TypeManager.CSharpName (target));
1223 public override Expression DoResolve (EmitContext ec)
1225 Expression e = base.DoResolve (ec);
1231 eclass = ExprClass.Value;
1232 Type etype = expr.Type;
1234 if (TypeManager.IsValueType (probe_type)){
1235 Report.Error (77, loc, "The as operator must be used with a reference type (`" +
1236 TypeManager.CSharpName (probe_type) + "' is a value type)");
1241 e = Convert.ImplicitConversion (ec, expr, probe_type, loc);
1248 if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
1249 if (etype.IsGenericParameter)
1250 expr = new BoxedCast (expr, etype);
1256 Error_CannotConvertType (etype, probe_type, loc);
1262 /// This represents a typecast in the source language.
1264 /// FIXME: Cast expressions have an unusual set of parsing
1265 /// rules, we need to figure those out.
1267 public class Cast : Expression {
1268 Expression target_type;
1271 public Cast (Expression cast_type, Expression expr)
1272 : this (cast_type, expr, cast_type.Location)
1276 public Cast (Expression cast_type, Expression expr, Location loc)
1278 this.target_type = cast_type;
1283 public Expression TargetType {
1289 public Expression Expr {
1298 bool CheckRange (EmitContext ec, long value, Type type, long min, long max)
1300 if (!ec.ConstantCheckState)
1303 if ((value < min) || (value > max)) {
1304 Error (221, "Constant value `" + value + "' cannot be converted " +
1305 "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " +
1306 "syntax to override)");
1313 bool CheckRange (EmitContext ec, ulong value, Type type, ulong max)
1315 if (!ec.ConstantCheckState)
1319 Error (221, "Constant value `" + value + "' cannot be converted " +
1320 "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " +
1321 "syntax to override)");
1328 bool CheckUnsigned (EmitContext ec, long value, Type type)
1330 if (!ec.ConstantCheckState)
1334 Error (221, "Constant value `" + value + "' cannot be converted " +
1335 "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " +
1336 "syntax to override)");
1343 // TODO: move to constant
1345 /// Attempts to do a compile-time folding of a constant cast.
1347 Expression TryReduce (EmitContext ec, Type target_type)
1349 if (expr.Type == target_type)
1352 if (TypeManager.IsEnumType (target_type))
1353 return new EnumConstant ((Constant)expr, target_type);
1355 Expression real_expr = expr;
1356 if (real_expr is EnumConstant)
1357 real_expr = ((EnumConstant) real_expr).Child;
1359 if (real_expr is ByteConstant){
1360 byte v = ((ByteConstant) real_expr).Value;
1362 if (target_type == TypeManager.sbyte_type) {
1363 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1365 return new SByteConstant ((sbyte) v);
1367 if (target_type == TypeManager.short_type)
1368 return new ShortConstant ((short) v);
1369 if (target_type == TypeManager.ushort_type)
1370 return new UShortConstant ((ushort) v);
1371 if (target_type == TypeManager.int32_type)
1372 return new IntConstant ((int) v);
1373 if (target_type == TypeManager.uint32_type)
1374 return new UIntConstant ((uint) v);
1375 if (target_type == TypeManager.int64_type)
1376 return new LongConstant ((long) v);
1377 if (target_type == TypeManager.uint64_type)
1378 return new ULongConstant ((ulong) v);
1379 if (target_type == TypeManager.float_type)
1380 return new FloatConstant ((float) v);
1381 if (target_type == TypeManager.double_type)
1382 return new DoubleConstant ((double) v);
1383 if (target_type == TypeManager.char_type)
1384 return new CharConstant ((char) v);
1385 if (target_type == TypeManager.decimal_type)
1386 return new DecimalConstant ((decimal) v);
1388 if (real_expr is SByteConstant){
1389 sbyte v = ((SByteConstant) real_expr).Value;
1391 if (target_type == TypeManager.byte_type) {
1392 if (!CheckUnsigned (ec, v, target_type))
1394 return new ByteConstant ((byte) v);
1396 if (target_type == TypeManager.short_type)
1397 return new ShortConstant ((short) v);
1398 if (target_type == TypeManager.ushort_type) {
1399 if (!CheckUnsigned (ec, v, target_type))
1401 return new UShortConstant ((ushort) v);
1402 } if (target_type == TypeManager.int32_type)
1403 return new IntConstant ((int) v);
1404 if (target_type == TypeManager.uint32_type) {
1405 if (!CheckUnsigned (ec, v, target_type))
1407 return new UIntConstant ((uint) v);
1408 } if (target_type == TypeManager.int64_type)
1409 return new LongConstant ((long) v);
1410 if (target_type == TypeManager.uint64_type) {
1411 if (!CheckUnsigned (ec, v, target_type))
1413 return new ULongConstant ((ulong) v);
1415 if (target_type == TypeManager.float_type)
1416 return new FloatConstant ((float) v);
1417 if (target_type == TypeManager.double_type)
1418 return new DoubleConstant ((double) v);
1419 if (target_type == TypeManager.char_type) {
1420 if (!CheckUnsigned (ec, v, target_type))
1422 return new CharConstant ((char) v);
1424 if (target_type == TypeManager.decimal_type)
1425 return new DecimalConstant ((decimal) v);
1427 if (real_expr is ShortConstant){
1428 short v = ((ShortConstant) real_expr).Value;
1430 if (target_type == TypeManager.byte_type) {
1431 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1433 return new ByteConstant ((byte) v);
1435 if (target_type == TypeManager.sbyte_type) {
1436 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1438 return new SByteConstant ((sbyte) v);
1440 if (target_type == TypeManager.ushort_type) {
1441 if (!CheckUnsigned (ec, v, target_type))
1443 return new UShortConstant ((ushort) v);
1445 if (target_type == TypeManager.int32_type)
1446 return new IntConstant ((int) v);
1447 if (target_type == TypeManager.uint32_type) {
1448 if (!CheckUnsigned (ec, v, target_type))
1450 return new UIntConstant ((uint) v);
1452 if (target_type == TypeManager.int64_type)
1453 return new LongConstant ((long) v);
1454 if (target_type == TypeManager.uint64_type) {
1455 if (!CheckUnsigned (ec, v, target_type))
1457 return new ULongConstant ((ulong) v);
1459 if (target_type == TypeManager.float_type)
1460 return new FloatConstant ((float) v);
1461 if (target_type == TypeManager.double_type)
1462 return new DoubleConstant ((double) v);
1463 if (target_type == TypeManager.char_type) {
1464 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1466 return new CharConstant ((char) v);
1468 if (target_type == TypeManager.decimal_type)
1469 return new DecimalConstant ((decimal) v);
1471 if (real_expr is UShortConstant){
1472 ushort v = ((UShortConstant) real_expr).Value;
1474 if (target_type == TypeManager.byte_type) {
1475 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1477 return new ByteConstant ((byte) v);
1479 if (target_type == TypeManager.sbyte_type) {
1480 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1482 return new SByteConstant ((sbyte) v);
1484 if (target_type == TypeManager.short_type) {
1485 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1487 return new ShortConstant ((short) v);
1489 if (target_type == TypeManager.int32_type)
1490 return new IntConstant ((int) v);
1491 if (target_type == TypeManager.uint32_type)
1492 return new UIntConstant ((uint) v);
1493 if (target_type == TypeManager.int64_type)
1494 return new LongConstant ((long) v);
1495 if (target_type == TypeManager.uint64_type)
1496 return new ULongConstant ((ulong) v);
1497 if (target_type == TypeManager.float_type)
1498 return new FloatConstant ((float) v);
1499 if (target_type == TypeManager.double_type)
1500 return new DoubleConstant ((double) v);
1501 if (target_type == TypeManager.char_type) {
1502 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1504 return new CharConstant ((char) v);
1506 if (target_type == TypeManager.decimal_type)
1507 return new DecimalConstant ((decimal) v);
1509 if (real_expr is IntConstant){
1510 int v = ((IntConstant) real_expr).Value;
1512 if (target_type == TypeManager.byte_type) {
1513 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1515 return new ByteConstant ((byte) v);
1517 if (target_type == TypeManager.sbyte_type) {
1518 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1520 return new SByteConstant ((sbyte) v);
1522 if (target_type == TypeManager.short_type) {
1523 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1525 return new ShortConstant ((short) v);
1527 if (target_type == TypeManager.ushort_type) {
1528 if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue))
1530 return new UShortConstant ((ushort) v);
1532 if (target_type == TypeManager.uint32_type) {
1533 if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue))
1535 return new UIntConstant ((uint) v);
1537 if (target_type == TypeManager.int64_type)
1538 return new LongConstant ((long) v);
1539 if (target_type == TypeManager.uint64_type) {
1540 if (!CheckUnsigned (ec, v, target_type))
1542 return new ULongConstant ((ulong) v);
1544 if (target_type == TypeManager.float_type)
1545 return new FloatConstant ((float) v);
1546 if (target_type == TypeManager.double_type)
1547 return new DoubleConstant ((double) v);
1548 if (target_type == TypeManager.char_type) {
1549 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1551 return new CharConstant ((char) v);
1553 if (target_type == TypeManager.decimal_type)
1554 return new DecimalConstant ((decimal) v);
1556 if (real_expr is UIntConstant){
1557 uint v = ((UIntConstant) real_expr).Value;
1559 if (target_type == TypeManager.byte_type) {
1560 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1562 return new ByteConstant ((byte) v);
1564 if (target_type == TypeManager.sbyte_type) {
1565 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1567 return new SByteConstant ((sbyte) v);
1569 if (target_type == TypeManager.short_type) {
1570 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1572 return new ShortConstant ((short) v);
1574 if (target_type == TypeManager.ushort_type) {
1575 if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue))
1577 return new UShortConstant ((ushort) v);
1579 if (target_type == TypeManager.int32_type) {
1580 if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue))
1582 return new IntConstant ((int) v);
1584 if (target_type == TypeManager.int64_type)
1585 return new LongConstant ((long) v);
1586 if (target_type == TypeManager.uint64_type)
1587 return new ULongConstant ((ulong) v);
1588 if (target_type == TypeManager.float_type)
1589 return new FloatConstant ((float) v);
1590 if (target_type == TypeManager.double_type)
1591 return new DoubleConstant ((double) v);
1592 if (target_type == TypeManager.char_type) {
1593 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1595 return new CharConstant ((char) v);
1597 if (target_type == TypeManager.decimal_type)
1598 return new DecimalConstant ((decimal) v);
1600 if (real_expr is LongConstant){
1601 long v = ((LongConstant) real_expr).Value;
1603 if (target_type == TypeManager.byte_type) {
1604 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1606 return new ByteConstant ((byte) v);
1608 if (target_type == TypeManager.sbyte_type) {
1609 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1611 return new SByteConstant ((sbyte) v);
1613 if (target_type == TypeManager.short_type) {
1614 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1616 return new ShortConstant ((short) v);
1618 if (target_type == TypeManager.ushort_type) {
1619 if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue))
1621 return new UShortConstant ((ushort) v);
1623 if (target_type == TypeManager.int32_type) {
1624 if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue))
1626 return new IntConstant ((int) v);
1628 if (target_type == TypeManager.uint32_type) {
1629 if (!CheckRange (ec, v, target_type, UInt32.MinValue, UInt32.MaxValue))
1631 return new UIntConstant ((uint) v);
1633 if (target_type == TypeManager.uint64_type) {
1634 if (!CheckUnsigned (ec, v, target_type))
1636 return new ULongConstant ((ulong) v);
1638 if (target_type == TypeManager.float_type)
1639 return new FloatConstant ((float) v);
1640 if (target_type == TypeManager.double_type)
1641 return new DoubleConstant ((double) v);
1642 if (target_type == TypeManager.char_type) {
1643 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1645 return new CharConstant ((char) v);
1647 if (target_type == TypeManager.decimal_type)
1648 return new DecimalConstant ((decimal) v);
1650 if (real_expr is ULongConstant){
1651 ulong v = ((ULongConstant) real_expr).Value;
1653 if (target_type == TypeManager.byte_type) {
1654 if (!CheckRange (ec, v, target_type, Byte.MaxValue))
1656 return new ByteConstant ((byte) v);
1658 if (target_type == TypeManager.sbyte_type) {
1659 if (!CheckRange (ec, v, target_type, (ulong) SByte.MaxValue))
1661 return new SByteConstant ((sbyte) v);
1663 if (target_type == TypeManager.short_type) {
1664 if (!CheckRange (ec, v, target_type, (ulong) Int16.MaxValue))
1666 return new ShortConstant ((short) v);
1668 if (target_type == TypeManager.ushort_type) {
1669 if (!CheckRange (ec, v, target_type, UInt16.MaxValue))
1671 return new UShortConstant ((ushort) v);
1673 if (target_type == TypeManager.int32_type) {
1674 if (!CheckRange (ec, v, target_type, Int32.MaxValue))
1676 return new IntConstant ((int) v);
1678 if (target_type == TypeManager.uint32_type) {
1679 if (!CheckRange (ec, v, target_type, UInt32.MaxValue))
1681 return new UIntConstant ((uint) v);
1683 if (target_type == TypeManager.int64_type) {
1684 if (!CheckRange (ec, v, target_type, (ulong) Int64.MaxValue))
1686 return new LongConstant ((long) v);
1688 if (target_type == TypeManager.float_type)
1689 return new FloatConstant ((float) v);
1690 if (target_type == TypeManager.double_type)
1691 return new DoubleConstant ((double) v);
1692 if (target_type == TypeManager.char_type) {
1693 if (!CheckRange (ec, v, target_type, Char.MaxValue))
1695 return new CharConstant ((char) v);
1697 if (target_type == TypeManager.decimal_type)
1698 return new DecimalConstant ((decimal) v);
1700 if (real_expr is FloatConstant){
1701 float v = ((FloatConstant) real_expr).Value;
1703 if (target_type == TypeManager.byte_type)
1704 return new ByteConstant ((byte) v);
1705 if (target_type == TypeManager.sbyte_type)
1706 return new SByteConstant ((sbyte) v);
1707 if (target_type == TypeManager.short_type)
1708 return new ShortConstant ((short) v);
1709 if (target_type == TypeManager.ushort_type)
1710 return new UShortConstant ((ushort) v);
1711 if (target_type == TypeManager.int32_type)
1712 return new IntConstant ((int) v);
1713 if (target_type == TypeManager.uint32_type)
1714 return new UIntConstant ((uint) v);
1715 if (target_type == TypeManager.int64_type)
1716 return new LongConstant ((long) v);
1717 if (target_type == TypeManager.uint64_type)
1718 return new ULongConstant ((ulong) v);
1719 if (target_type == TypeManager.double_type)
1720 return new DoubleConstant ((double) v);
1721 if (target_type == TypeManager.char_type)
1722 return new CharConstant ((char) v);
1723 if (target_type == TypeManager.decimal_type)
1724 return new DecimalConstant ((decimal) v);
1726 if (real_expr is DoubleConstant){
1727 double v = ((DoubleConstant) real_expr).Value;
1729 if (target_type == TypeManager.byte_type){
1730 return new ByteConstant ((byte) v);
1731 } if (target_type == TypeManager.sbyte_type)
1732 return new SByteConstant ((sbyte) v);
1733 if (target_type == TypeManager.short_type)
1734 return new ShortConstant ((short) v);
1735 if (target_type == TypeManager.ushort_type)
1736 return new UShortConstant ((ushort) v);
1737 if (target_type == TypeManager.int32_type)
1738 return new IntConstant ((int) v);
1739 if (target_type == TypeManager.uint32_type)
1740 return new UIntConstant ((uint) v);
1741 if (target_type == TypeManager.int64_type)
1742 return new LongConstant ((long) v);
1743 if (target_type == TypeManager.uint64_type)
1744 return new ULongConstant ((ulong) v);
1745 if (target_type == TypeManager.float_type)
1746 return new FloatConstant ((float) v);
1747 if (target_type == TypeManager.char_type)
1748 return new CharConstant ((char) v);
1749 if (target_type == TypeManager.decimal_type)
1750 return new DecimalConstant ((decimal) v);
1753 if (real_expr is CharConstant){
1754 char v = ((CharConstant) real_expr).Value;
1756 if (target_type == TypeManager.byte_type) {
1757 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1759 return new ByteConstant ((byte) v);
1761 if (target_type == TypeManager.sbyte_type) {
1762 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1764 return new SByteConstant ((sbyte) v);
1766 if (target_type == TypeManager.short_type) {
1767 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1769 return new ShortConstant ((short) v);
1771 if (target_type == TypeManager.int32_type)
1772 return new IntConstant ((int) v);
1773 if (target_type == TypeManager.uint32_type)
1774 return new UIntConstant ((uint) v);
1775 if (target_type == TypeManager.int64_type)
1776 return new LongConstant ((long) v);
1777 if (target_type == TypeManager.uint64_type)
1778 return new ULongConstant ((ulong) v);
1779 if (target_type == TypeManager.float_type)
1780 return new FloatConstant ((float) v);
1781 if (target_type == TypeManager.double_type)
1782 return new DoubleConstant ((double) v);
1783 if (target_type == TypeManager.char_type) {
1784 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1786 return new CharConstant ((char) v);
1788 if (target_type == TypeManager.decimal_type)
1789 return new DecimalConstant ((decimal) v);
1795 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
1797 expr = expr.DoResolveLValue (ec, right_side);
1801 return ResolveRest (ec);
1804 public override Expression DoResolve (EmitContext ec)
1806 expr = expr.Resolve (ec);
1810 return ResolveRest (ec);
1813 Expression ResolveRest (EmitContext ec)
1815 TypeExpr target = target_type.ResolveAsTypeTerminal (ec);
1821 CheckObsoleteAttribute (type);
1823 if (type.IsAbstract && type.IsSealed) {
1824 Report.Error (716, loc, "Cannot convert to static type `{0}'", TypeManager.CSharpName (type));
1828 eclass = ExprClass.Value;
1830 if (expr is Constant){
1831 Expression e = TryReduce (ec, type);
1837 if (type.IsPointer && !ec.InUnsafe) {
1841 expr = Convert.ExplicitConversion (ec, expr, type, loc);
1845 public override void Emit (EmitContext ec)
1848 // This one will never happen
1850 throw new Exception ("Should not happen");
1855 /// Binary operators
1857 public class Binary : Expression {
1858 public enum Operator : byte {
1859 Multiply, Division, Modulus,
1860 Addition, Subtraction,
1861 LeftShift, RightShift,
1862 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
1863 Equality, Inequality,
1873 Expression left, right;
1875 // This must be kept in sync with Operator!!!
1876 public static readonly string [] oper_names;
1880 oper_names = new string [(int) Operator.TOP];
1882 oper_names [(int) Operator.Multiply] = "op_Multiply";
1883 oper_names [(int) Operator.Division] = "op_Division";
1884 oper_names [(int) Operator.Modulus] = "op_Modulus";
1885 oper_names [(int) Operator.Addition] = "op_Addition";
1886 oper_names [(int) Operator.Subtraction] = "op_Subtraction";
1887 oper_names [(int) Operator.LeftShift] = "op_LeftShift";
1888 oper_names [(int) Operator.RightShift] = "op_RightShift";
1889 oper_names [(int) Operator.LessThan] = "op_LessThan";
1890 oper_names [(int) Operator.GreaterThan] = "op_GreaterThan";
1891 oper_names [(int) Operator.LessThanOrEqual] = "op_LessThanOrEqual";
1892 oper_names [(int) Operator.GreaterThanOrEqual] = "op_GreaterThanOrEqual";
1893 oper_names [(int) Operator.Equality] = "op_Equality";
1894 oper_names [(int) Operator.Inequality] = "op_Inequality";
1895 oper_names [(int) Operator.BitwiseAnd] = "op_BitwiseAnd";
1896 oper_names [(int) Operator.BitwiseOr] = "op_BitwiseOr";
1897 oper_names [(int) Operator.ExclusiveOr] = "op_ExclusiveOr";
1898 oper_names [(int) Operator.LogicalOr] = "op_LogicalOr";
1899 oper_names [(int) Operator.LogicalAnd] = "op_LogicalAnd";
1902 public Binary (Operator oper, Expression left, Expression right)
1907 this.loc = left.Location;
1910 public Operator Oper {
1919 public Expression Left {
1928 public Expression Right {
1939 /// Returns a stringified representation of the Operator
1941 static string OperName (Operator oper)
1944 case Operator.Multiply:
1946 case Operator.Division:
1948 case Operator.Modulus:
1950 case Operator.Addition:
1952 case Operator.Subtraction:
1954 case Operator.LeftShift:
1956 case Operator.RightShift:
1958 case Operator.LessThan:
1960 case Operator.GreaterThan:
1962 case Operator.LessThanOrEqual:
1964 case Operator.GreaterThanOrEqual:
1966 case Operator.Equality:
1968 case Operator.Inequality:
1970 case Operator.BitwiseAnd:
1972 case Operator.BitwiseOr:
1974 case Operator.ExclusiveOr:
1976 case Operator.LogicalOr:
1978 case Operator.LogicalAnd:
1982 return oper.ToString ();
1985 public override string ToString ()
1987 return "operator " + OperName (oper) + "(" + left.ToString () + ", " +
1988 right.ToString () + ")";
1991 Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
1993 if (expr.Type == target_type)
1996 return Convert.ImplicitConversion (ec, expr, target_type, loc);
1999 public static void Error_OperatorAmbiguous (Location loc, Operator oper, Type l, Type r)
2002 34, loc, "Operator `" + OperName (oper)
2003 + "' is ambiguous on operands of type `"
2004 + TypeManager.CSharpName (l) + "' "
2005 + "and `" + TypeManager.CSharpName (r)
2009 bool IsOfType (EmitContext ec, Type l, Type r, Type t, bool check_user_conversions)
2011 if ((l == t) || (r == t))
2014 if (!check_user_conversions)
2017 if (Convert.ImplicitUserConversionExists (ec, l, t))
2019 else if (Convert.ImplicitUserConversionExists (ec, r, t))
2026 // Note that handling the case l == Decimal || r == Decimal
2027 // is taken care of by the Step 1 Operator Overload resolution.
2029 // If `check_user_conv' is true, we also check whether a user-defined conversion
2030 // exists. Note that we only need to do this if both arguments are of a user-defined
2031 // type, otherwise ConvertImplict() already finds the user-defined conversion for us,
2032 // so we don't explicitly check for performance reasons.
2034 bool DoNumericPromotions (EmitContext ec, Type l, Type r, Expression lexpr, Expression rexpr, bool check_user_conv)
2036 if (IsOfType (ec, l, r, TypeManager.double_type, check_user_conv)){
2038 // If either operand is of type double, the other operand is
2039 // conveted to type double.
2041 if (r != TypeManager.double_type)
2042 right = Convert.ImplicitConversion (ec, right, TypeManager.double_type, loc);
2043 if (l != TypeManager.double_type)
2044 left = Convert.ImplicitConversion (ec, left, TypeManager.double_type, loc);
2046 type = TypeManager.double_type;
2047 } else if (IsOfType (ec, l, r, TypeManager.float_type, check_user_conv)){
2049 // if either operand is of type float, the other operand is
2050 // converted to type float.
2052 if (r != TypeManager.double_type)
2053 right = Convert.ImplicitConversion (ec, right, TypeManager.float_type, loc);
2054 if (l != TypeManager.double_type)
2055 left = Convert.ImplicitConversion (ec, left, TypeManager.float_type, loc);
2056 type = TypeManager.float_type;
2057 } else if (IsOfType (ec, l, r, TypeManager.uint64_type, check_user_conv)){
2061 // If either operand is of type ulong, the other operand is
2062 // converted to type ulong. or an error ocurrs if the other
2063 // operand is of type sbyte, short, int or long
2065 if (l == TypeManager.uint64_type){
2066 if (r != TypeManager.uint64_type){
2067 if (right is IntConstant){
2068 IntConstant ic = (IntConstant) right;
2070 e = Convert.TryImplicitIntConversion (l, ic);
2073 } else if (right is LongConstant){
2074 long ll = ((LongConstant) right).Value;
2077 right = new ULongConstant ((ulong) ll);
2079 e = Convert.ImplicitNumericConversion (ec, right, l, loc);
2086 if (left is IntConstant){
2087 e = Convert.TryImplicitIntConversion (r, (IntConstant) left);
2090 } else if (left is LongConstant){
2091 long ll = ((LongConstant) left).Value;
2094 left = new ULongConstant ((ulong) ll);
2096 e = Convert.ImplicitNumericConversion (ec, left, r, loc);
2103 if ((other == TypeManager.sbyte_type) ||
2104 (other == TypeManager.short_type) ||
2105 (other == TypeManager.int32_type) ||
2106 (other == TypeManager.int64_type))
2107 Error_OperatorAmbiguous (loc, oper, l, r);
2109 left = ForceConversion (ec, left, TypeManager.uint64_type);
2110 right = ForceConversion (ec, right, TypeManager.uint64_type);
2112 type = TypeManager.uint64_type;
2113 } else if (IsOfType (ec, l, r, TypeManager.int64_type, check_user_conv)){
2115 // If either operand is of type long, the other operand is converted
2118 if (l != TypeManager.int64_type)
2119 left = Convert.ImplicitConversion (ec, left, TypeManager.int64_type, loc);
2120 if (r != TypeManager.int64_type)
2121 right = Convert.ImplicitConversion (ec, right, TypeManager.int64_type, loc);
2123 type = TypeManager.int64_type;
2124 } else if (IsOfType (ec, l, r, TypeManager.uint32_type, check_user_conv)){
2126 // If either operand is of type uint, and the other
2127 // operand is of type sbyte, short or int, othe operands are
2128 // converted to type long (unless we have an int constant).
2132 if (l == TypeManager.uint32_type){
2133 if (right is IntConstant){
2134 IntConstant ic = (IntConstant) right;
2138 right = new UIntConstant ((uint) val);
2145 } else if (r == TypeManager.uint32_type){
2146 if (left is IntConstant){
2147 IntConstant ic = (IntConstant) left;
2151 left = new UIntConstant ((uint) val);
2160 if ((other == TypeManager.sbyte_type) ||
2161 (other == TypeManager.short_type) ||
2162 (other == TypeManager.int32_type)){
2163 left = ForceConversion (ec, left, TypeManager.int64_type);
2164 right = ForceConversion (ec, right, TypeManager.int64_type);
2165 type = TypeManager.int64_type;
2168 // if either operand is of type uint, the other
2169 // operand is converd to type uint
2171 left = ForceConversion (ec, left, TypeManager.uint32_type);
2172 right = ForceConversion (ec, right, TypeManager.uint32_type);
2173 type = TypeManager.uint32_type;
2175 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
2176 if (l != TypeManager.decimal_type)
2177 left = Convert.ImplicitConversion (ec, left, TypeManager.decimal_type, loc);
2179 if (r != TypeManager.decimal_type)
2180 right = Convert.ImplicitConversion (ec, right, TypeManager.decimal_type, loc);
2181 type = TypeManager.decimal_type;
2183 left = ForceConversion (ec, left, TypeManager.int32_type);
2184 right = ForceConversion (ec, right, TypeManager.int32_type);
2187 Convert.ImplicitConversionExists (ec, lexpr, TypeManager.string_type) &&
2188 Convert.ImplicitConversionExists (ec, rexpr, TypeManager.string_type);
2189 if (strConv && left != null && right != null)
2190 Error_OperatorAmbiguous (loc, oper, l, r);
2192 type = TypeManager.int32_type;
2195 return (left != null) && (right != null);
2198 static public void Error_OperatorCannotBeApplied (Location loc, string name, Type l, Type r)
2200 Report.Error (19, loc, "Operator `{0}' cannot be applied to operands of type `{1}' and `{2}'",
2201 name, TypeManager.CSharpName (l), TypeManager.CSharpName (r));
2204 void Error_OperatorCannotBeApplied ()
2206 Error_OperatorCannotBeApplied (loc, OperName (oper), left.Type, right.Type);
2209 static bool is_unsigned (Type t)
2211 return (t == TypeManager.uint32_type || t == TypeManager.uint64_type ||
2212 t == TypeManager.short_type || t == TypeManager.byte_type);
2215 static bool is_user_defined (Type t)
2217 if (t.IsSubclassOf (TypeManager.value_type) &&
2218 (!TypeManager.IsBuiltinType (t) || t == TypeManager.decimal_type))
2224 Expression Make32or64 (EmitContext ec, Expression e)
2228 if (t == TypeManager.int32_type || t == TypeManager.uint32_type ||
2229 t == TypeManager.int64_type || t == TypeManager.uint64_type)
2231 Expression ee = Convert.ImplicitConversion (ec, e, TypeManager.int32_type, loc);
2234 ee = Convert.ImplicitConversion (ec, e, TypeManager.uint32_type, loc);
2237 ee = Convert.ImplicitConversion (ec, e, TypeManager.int64_type, loc);
2240 ee = Convert.ImplicitConversion (ec, e, TypeManager.uint64_type, loc);
2246 Expression CheckShiftArguments (EmitContext ec)
2250 e = ForceConversion (ec, right, TypeManager.int32_type);
2252 Error_OperatorCannotBeApplied ();
2257 if (((e = Convert.ImplicitConversion (ec, left, TypeManager.int32_type, loc)) != null) ||
2258 ((e = Convert.ImplicitConversion (ec, left, TypeManager.uint32_type, loc)) != null) ||
2259 ((e = Convert.ImplicitConversion (ec, left, TypeManager.int64_type, loc)) != null) ||
2260 ((e = Convert.ImplicitConversion (ec, left, TypeManager.uint64_type, loc)) != null)){
2264 if (type == TypeManager.int32_type || type == TypeManager.uint32_type){
2265 right = new Binary (Binary.Operator.BitwiseAnd, right, new IntLiteral (31));
2266 right = right.DoResolve (ec);
2268 right = new Binary (Binary.Operator.BitwiseAnd, right, new IntLiteral (63));
2269 right = right.DoResolve (ec);
2274 Error_OperatorCannotBeApplied ();
2279 // This is used to check if a test 'x == null' can be optimized to a reference equals,
2280 // i.e., not invoke op_Equality.
2282 static bool EqualsNullIsReferenceEquals (Type t)
2284 return t == TypeManager.object_type || t == TypeManager.string_type ||
2285 t == TypeManager.delegate_type || t.IsSubclassOf (TypeManager.delegate_type);
2288 static void Warning_UnintendedReferenceComparison (Location loc, string side, Type type)
2290 Report.Warning ((side == "left" ? 252 : 253), 2, loc,
2291 "Possible unintended reference comparison; to get a value comparison, " +
2292 "cast the {0} hand side to type `{1}'.", side, TypeManager.CSharpName (type));
2295 Expression ResolveOperator (EmitContext ec)
2298 Type r = right.Type;
2300 if (oper == Operator.Equality || oper == Operator.Inequality){
2301 if (l.IsGenericParameter && (right is NullLiteral)) {
2302 if (l.BaseType == TypeManager.value_type) {
2303 Error_OperatorCannotBeApplied ();
2307 left = new BoxedCast (left, TypeManager.object_type);
2308 Type = TypeManager.bool_type;
2312 if (r.IsGenericParameter && (left is NullLiteral)) {
2313 if (r.BaseType == TypeManager.value_type) {
2314 Error_OperatorCannotBeApplied ();
2318 right = new BoxedCast (right, TypeManager.object_type);
2319 Type = TypeManager.bool_type;
2324 // Optimize out call to op_Equality in a few cases.
2326 if ((l == TypeManager.null_type && EqualsNullIsReferenceEquals (r)) ||
2327 (r == TypeManager.null_type && EqualsNullIsReferenceEquals (l))) {
2328 Type = TypeManager.bool_type;
2334 if (l == TypeManager.intptr_type && r == TypeManager.intptr_type) {
2335 Type = TypeManager.bool_type;
2342 // Do not perform operator overload resolution when both sides are
2345 Expression left_operators = null, right_operators = null;
2346 if (!(TypeManager.IsPrimitiveType (l) && TypeManager.IsPrimitiveType (r))){
2348 // Step 1: Perform Operator Overload location
2350 string op = oper_names [(int) oper];
2352 MethodGroupExpr union;
2353 left_operators = MemberLookup (ec, l, op, MemberTypes.Method, AllBindingFlags, loc);
2355 right_operators = MemberLookup (
2356 ec, r, op, MemberTypes.Method, AllBindingFlags, loc);
2357 union = Invocation.MakeUnionSet (left_operators, right_operators, loc);
2359 union = (MethodGroupExpr) left_operators;
2361 if (union != null) {
2362 ArrayList args = new ArrayList (2);
2363 args.Add (new Argument (left, Argument.AType.Expression));
2364 args.Add (new Argument (right, Argument.AType.Expression));
2366 MethodBase method = Invocation.OverloadResolve (
2367 ec, union, args, true, Location.Null);
2369 if (method != null) {
2370 MethodInfo mi = (MethodInfo) method;
2372 return new BinaryMethod (mi.ReturnType, method, args);
2378 // Step 0: String concatenation (because overloading will get this wrong)
2380 if (oper == Operator.Addition){
2382 // If any of the arguments is a string, cast to string
2385 // Simple constant folding
2386 if (left is StringConstant && right is StringConstant)
2387 return new StringConstant (((StringConstant) left).Value + ((StringConstant) right).Value);
2389 if (l == TypeManager.string_type || r == TypeManager.string_type) {
2391 if (r == TypeManager.void_type || l == TypeManager.void_type) {
2392 Error_OperatorCannotBeApplied ();
2396 // try to fold it in on the left
2397 if (left is StringConcat) {
2400 // We have to test here for not-null, since we can be doubly-resolved
2401 // take care of not appending twice
2404 type = TypeManager.string_type;
2405 ((StringConcat) left).Append (ec, right);
2406 return left.Resolve (ec);
2412 // Otherwise, start a new concat expression
2413 return new StringConcat (ec, loc, left, right).Resolve (ec);
2417 // Transform a + ( - b) into a - b
2419 if (right is Unary){
2420 Unary right_unary = (Unary) right;
2422 if (right_unary.Oper == Unary.Operator.UnaryNegation){
2423 oper = Operator.Subtraction;
2424 right = right_unary.Expr;
2430 if (oper == Operator.Equality || oper == Operator.Inequality){
2431 if (l == TypeManager.bool_type || r == TypeManager.bool_type){
2432 if (r != TypeManager.bool_type || l != TypeManager.bool_type){
2433 Error_OperatorCannotBeApplied ();
2437 type = TypeManager.bool_type;
2441 if (l.IsPointer || r.IsPointer) {
2442 if (l.IsPointer && r.IsPointer) {
2443 type = TypeManager.bool_type;
2447 if (l.IsPointer && r == TypeManager.null_type) {
2448 right = new EmptyCast (NullPointer.Null, l);
2449 type = TypeManager.bool_type;
2453 if (r.IsPointer && l == TypeManager.null_type) {
2454 left = new EmptyCast (NullPointer.Null, r);
2455 type = TypeManager.bool_type;
2460 if (l.IsGenericParameter && r.IsGenericParameter) {
2461 GenericConstraints l_gc, r_gc;
2463 l_gc = TypeManager.GetTypeParameterConstraints (l);
2464 r_gc = TypeManager.GetTypeParameterConstraints (r);
2466 if ((l_gc == null) || (r_gc == null) ||
2467 !(l_gc.HasReferenceTypeConstraint || l_gc.HasClassConstraint) ||
2468 !(r_gc.HasReferenceTypeConstraint || r_gc.HasClassConstraint)) {
2469 Error_OperatorCannotBeApplied ();
2476 // operator != (object a, object b)
2477 // operator == (object a, object b)
2479 // For this to be used, both arguments have to be reference-types.
2480 // Read the rationale on the spec (14.9.6)
2482 if (!(l.IsValueType || r.IsValueType)){
2483 type = TypeManager.bool_type;
2489 // Also, a standard conversion must exist from either one
2491 bool left_to_right =
2492 Convert.ImplicitStandardConversionExists (ec, left, r);
2493 bool right_to_left = !left_to_right &&
2494 Convert.ImplicitStandardConversionExists (ec, right, l);
2496 if (!left_to_right && !right_to_left) {
2497 Error_OperatorCannotBeApplied ();
2501 if (left_to_right && left_operators != null &&
2502 RootContext.WarningLevel >= 2) {
2503 ArrayList args = new ArrayList (2);
2504 args.Add (new Argument (left, Argument.AType.Expression));
2505 args.Add (new Argument (left, Argument.AType.Expression));
2506 MethodBase method = Invocation.OverloadResolve (
2507 ec, (MethodGroupExpr) left_operators, args, true, Location.Null);
2509 Warning_UnintendedReferenceComparison (loc, "right", l);
2512 if (right_to_left && right_operators != null &&
2513 RootContext.WarningLevel >= 2) {
2514 ArrayList args = new ArrayList (2);
2515 args.Add (new Argument (right, Argument.AType.Expression));
2516 args.Add (new Argument (right, Argument.AType.Expression));
2517 MethodBase method = Invocation.OverloadResolve (
2518 ec, (MethodGroupExpr) right_operators, args, true, Location.Null);
2520 Warning_UnintendedReferenceComparison (loc, "left", r);
2524 // We are going to have to convert to an object to compare
2526 if (l != TypeManager.object_type)
2527 left = new EmptyCast (left, TypeManager.object_type);
2528 if (r != TypeManager.object_type)
2529 right = new EmptyCast (right, TypeManager.object_type);
2532 // FIXME: CSC here catches errors cs254 and cs252
2538 // One of them is a valuetype, but the other one is not.
2540 if (!l.IsValueType || !r.IsValueType) {
2541 Error_OperatorCannotBeApplied ();
2546 // Only perform numeric promotions on:
2547 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
2549 if (oper == Operator.Addition || oper == Operator.Subtraction) {
2550 if (TypeManager.IsDelegateType (l)){
2551 if (((right.eclass == ExprClass.MethodGroup) ||
2552 (r == TypeManager.anonymous_method_type))){
2553 if ((RootContext.Version != LanguageVersion.ISO_1)){
2554 Expression tmp = Convert.ImplicitConversionRequired (ec, right, l, loc);
2562 if (TypeManager.IsDelegateType (r)){
2564 ArrayList args = new ArrayList (2);
2566 args = new ArrayList (2);
2567 args.Add (new Argument (left, Argument.AType.Expression));
2568 args.Add (new Argument (right, Argument.AType.Expression));
2570 if (oper == Operator.Addition)
2571 method = TypeManager.delegate_combine_delegate_delegate;
2573 method = TypeManager.delegate_remove_delegate_delegate;
2575 if (!TypeManager.IsEqual (l, r)) {
2576 Error_OperatorCannotBeApplied ();
2580 return new BinaryDelegate (l, method, args);
2585 // Pointer arithmetic:
2587 // T* operator + (T* x, int y);
2588 // T* operator + (T* x, uint y);
2589 // T* operator + (T* x, long y);
2590 // T* operator + (T* x, ulong y);
2592 // T* operator + (int y, T* x);
2593 // T* operator + (uint y, T *x);
2594 // T* operator + (long y, T *x);
2595 // T* operator + (ulong y, T *x);
2597 // T* operator - (T* x, int y);
2598 // T* operator - (T* x, uint y);
2599 // T* operator - (T* x, long y);
2600 // T* operator - (T* x, ulong y);
2602 // long operator - (T* x, T *y)
2605 if (r.IsPointer && oper == Operator.Subtraction){
2607 return new PointerArithmetic (
2608 false, left, right, TypeManager.int64_type,
2611 Expression t = Make32or64 (ec, right);
2613 return new PointerArithmetic (oper == Operator.Addition, left, t, l, loc).Resolve (ec);
2615 } else if (r.IsPointer && oper == Operator.Addition){
2616 Expression t = Make32or64 (ec, left);
2618 return new PointerArithmetic (true, right, t, r, loc).Resolve (ec);
2623 // Enumeration operators
2625 bool lie = TypeManager.IsEnumType (l);
2626 bool rie = TypeManager.IsEnumType (r);
2630 // U operator - (E e, E f)
2632 if (oper == Operator.Subtraction){
2634 type = TypeManager.EnumToUnderlying (l);
2637 Error_OperatorCannotBeApplied ();
2643 // operator + (E e, U x)
2644 // operator - (E e, U x)
2646 if (oper == Operator.Addition || oper == Operator.Subtraction){
2647 Type enum_type = lie ? l : r;
2648 Type other_type = lie ? r : l;
2649 Type underlying_type = TypeManager.EnumToUnderlying (enum_type);
2651 if (underlying_type != other_type){
2652 temp = Convert.ImplicitConversion (ec, lie ? right : left, underlying_type, loc);
2662 Error_OperatorCannotBeApplied ();
2671 temp = Convert.ImplicitConversion (ec, right, l, loc);
2675 Error_OperatorCannotBeApplied ();
2679 temp = Convert.ImplicitConversion (ec, left, r, loc);
2684 Error_OperatorCannotBeApplied ();
2689 if (oper == Operator.Equality || oper == Operator.Inequality ||
2690 oper == Operator.LessThanOrEqual || oper == Operator.LessThan ||
2691 oper == Operator.GreaterThanOrEqual || oper == Operator.GreaterThan){
2692 if (left.Type != right.Type){
2693 Error_OperatorCannotBeApplied ();
2696 type = TypeManager.bool_type;
2700 if (oper == Operator.BitwiseAnd ||
2701 oper == Operator.BitwiseOr ||
2702 oper == Operator.ExclusiveOr){
2703 if (left.Type != right.Type){
2704 Error_OperatorCannotBeApplied ();
2710 Error_OperatorCannotBeApplied ();
2714 if (oper == Operator.LeftShift || oper == Operator.RightShift)
2715 return CheckShiftArguments (ec);
2717 if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
2718 if (l == TypeManager.bool_type && r == TypeManager.bool_type) {
2719 type = TypeManager.bool_type;
2724 Error_OperatorCannotBeApplied ();
2728 Expression e = new ConditionalLogicalOperator (
2729 oper == Operator.LogicalAnd, left, right, l, loc);
2730 return e.Resolve (ec);
2734 // operator & (bool x, bool y)
2735 // operator | (bool x, bool y)
2736 // operator ^ (bool x, bool y)
2738 if (l == TypeManager.bool_type && r == TypeManager.bool_type){
2739 if (oper == Operator.BitwiseAnd ||
2740 oper == Operator.BitwiseOr ||
2741 oper == Operator.ExclusiveOr){
2748 // Pointer comparison
2750 if (l.IsPointer && r.IsPointer){
2751 if (oper == Operator.LessThan || oper == Operator.LessThanOrEqual ||
2752 oper == Operator.GreaterThan || oper == Operator.GreaterThanOrEqual){
2753 type = TypeManager.bool_type;
2759 // This will leave left or right set to null if there is an error
2761 bool check_user_conv = is_user_defined (l) && is_user_defined (r);
2762 DoNumericPromotions (ec, l, r, left, right, check_user_conv);
2763 if (left == null || right == null){
2764 Error_OperatorCannotBeApplied (loc, OperName (oper), l, r);
2769 // reload our cached types if required
2774 if (oper == Operator.BitwiseAnd ||
2775 oper == Operator.BitwiseOr ||
2776 oper == Operator.ExclusiveOr){
2778 if (((l == TypeManager.int32_type) ||
2779 (l == TypeManager.uint32_type) ||
2780 (l == TypeManager.short_type) ||
2781 (l == TypeManager.ushort_type) ||
2782 (l == TypeManager.int64_type) ||
2783 (l == TypeManager.uint64_type))){
2786 Error_OperatorCannotBeApplied ();
2790 Error_OperatorCannotBeApplied ();
2795 if (oper == Operator.Equality ||
2796 oper == Operator.Inequality ||
2797 oper == Operator.LessThanOrEqual ||
2798 oper == Operator.LessThan ||
2799 oper == Operator.GreaterThanOrEqual ||
2800 oper == Operator.GreaterThan){
2801 type = TypeManager.bool_type;
2807 public override Expression DoResolve (EmitContext ec)
2809 if ((oper == Operator.Subtraction) && (left is ParenthesizedExpression)) {
2810 left = ((ParenthesizedExpression) left).Expr;
2811 left = left.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.Type);
2815 if (left.eclass == ExprClass.Type) {
2816 Error (75, "To cast a negative value, you must enclose the value in parentheses");
2820 left = left.Resolve (ec);
2825 Constant lc = left as Constant;
2826 if (lc != null && lc.Type == TypeManager.bool_type &&
2827 ((oper == Operator.LogicalAnd && (bool)lc.GetValue () == false) ||
2828 (oper == Operator.LogicalOr && (bool)lc.GetValue () == true))) {
2830 // TODO: make a sense to resolve unreachable expression as we do for statement
2831 Report.Warning (429, 4, loc, "Unreachable expression code detected");
2835 right = right.Resolve (ec);
2839 eclass = ExprClass.Value;
2841 Constant rc = right as Constant;
2843 if (oper == Operator.BitwiseAnd) {
2844 if (rc != null && rc.IsZeroInteger) {
2845 return lc is EnumConstant ?
2846 new EnumConstant (rc, lc.Type):
2850 if (lc != null && lc.IsZeroInteger) {
2851 return rc is EnumConstant ?
2852 new EnumConstant (lc, rc.Type):
2857 if (rc != null && lc != null){
2858 int prev_e = Report.Errors;
2859 Expression e = ConstantFold.BinaryFold (
2860 ec, oper, lc, rc, loc);
2861 if (e != null || Report.Errors != prev_e)
2865 if (TypeManager.IsNullableType (left.Type) || TypeManager.IsNullableType (right.Type))
2866 return new Nullable.LiftedBinaryOperator (oper, left, right, loc).Resolve (ec);
2868 // Check CS0652 warning here (before resolving operator).
2869 if (oper == Operator.Equality ||
2870 oper == Operator.Inequality ||
2871 oper == Operator.LessThanOrEqual ||
2872 oper == Operator.LessThan ||
2873 oper == Operator.GreaterThanOrEqual ||
2874 oper == Operator.GreaterThan){
2875 CheckUselessComparison (left as Constant, right.Type);
2876 CheckUselessComparison (right as Constant, left.Type);
2879 return ResolveOperator (ec);
2882 private void CheckUselessComparison (Constant c, Type type)
2884 if (c == null || !IsTypeIntegral (type)
2885 || c is StringConstant
2886 || c is BoolConstant
2887 || c is CharConstant
2888 || c is FloatConstant
2889 || c is DoubleConstant
2890 || c is DecimalConstant
2896 if (c is ULongConstant) {
2897 ulong uvalue = ((ULongConstant) c).Value;
2898 if (uvalue > long.MaxValue) {
2899 if (type == TypeManager.byte_type ||
2900 type == TypeManager.sbyte_type ||
2901 type == TypeManager.short_type ||
2902 type == TypeManager.ushort_type ||
2903 type == TypeManager.int32_type ||
2904 type == TypeManager.uint32_type ||
2905 type == TypeManager.int64_type)
2906 WarnUselessComparison (type);
2909 value = (long) uvalue;
2911 else if (c is ByteConstant)
2912 value = ((ByteConstant) c).Value;
2913 else if (c is SByteConstant)
2914 value = ((SByteConstant) c).Value;
2915 else if (c is ShortConstant)
2916 value = ((ShortConstant) c).Value;
2917 else if (c is UShortConstant)
2918 value = ((UShortConstant) c).Value;
2919 else if (c is IntConstant)
2920 value = ((IntConstant) c).Value;
2921 else if (c is UIntConstant)
2922 value = ((UIntConstant) c).Value;
2923 else if (c is LongConstant)
2924 value = ((LongConstant) c).Value;
2927 if (IsValueOutOfRange (value, type))
2928 WarnUselessComparison (type);
2933 private bool IsValueOutOfRange (long value, Type type)
2935 if (IsTypeUnsigned (type) && value < 0)
2937 return type == TypeManager.sbyte_type && (value >= 0x80 || value < -0x80) ||
2938 type == TypeManager.byte_type && value >= 0x100 ||
2939 type == TypeManager.short_type && (value >= 0x8000 || value < -0x8000) ||
2940 type == TypeManager.ushort_type && value >= 0x10000 ||
2941 type == TypeManager.int32_type && (value >= 0x80000000 || value < -0x80000000) ||
2942 type == TypeManager.uint32_type && value >= 0x100000000;
2945 private static bool IsTypeIntegral (Type type)
2947 return type == TypeManager.uint64_type ||
2948 type == TypeManager.int64_type ||
2949 type == TypeManager.uint32_type ||
2950 type == TypeManager.int32_type ||
2951 type == TypeManager.ushort_type ||
2952 type == TypeManager.short_type ||
2953 type == TypeManager.sbyte_type ||
2954 type == TypeManager.byte_type;
2957 private static bool IsTypeUnsigned (Type type)
2959 return type == TypeManager.uint64_type ||
2960 type == TypeManager.uint32_type ||
2961 type == TypeManager.ushort_type ||
2962 type == TypeManager.byte_type;
2965 private void WarnUselessComparison (Type type)
2967 Report.Warning (652, 2, loc, "Comparison to integral constant is useless; the constant is outside the range of type `{0}'",
2968 TypeManager.CSharpName (type));
2972 /// EmitBranchable is called from Statement.EmitBoolExpression in the
2973 /// context of a conditional bool expression. This function will return
2974 /// false if it is was possible to use EmitBranchable, or true if it was.
2976 /// The expression's code is generated, and we will generate a branch to `target'
2977 /// if the resulting expression value is equal to isTrue
2979 public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
2981 ILGenerator ig = ec.ig;
2984 // This is more complicated than it looks, but its just to avoid
2985 // duplicated tests: basically, we allow ==, !=, >, <, >= and <=
2986 // but on top of that we want for == and != to use a special path
2987 // if we are comparing against null
2989 if ((oper == Operator.Equality || oper == Operator.Inequality) && (left is Constant || right is Constant)) {
2990 bool my_on_true = oper == Operator.Inequality ? onTrue : !onTrue;
2993 // put the constant on the rhs, for simplicity
2995 if (left is Constant) {
2996 Expression swap = right;
3001 if (((Constant) right).IsZeroInteger) {
3004 ig.Emit (OpCodes.Brtrue, target);
3006 ig.Emit (OpCodes.Brfalse, target);
3009 } else if (right is BoolConstant){
3011 if (my_on_true != ((BoolConstant) right).Value)
3012 ig.Emit (OpCodes.Brtrue, target);
3014 ig.Emit (OpCodes.Brfalse, target);
3019 } else if (oper == Operator.LogicalAnd) {
3022 Label tests_end = ig.DefineLabel ();
3024 left.EmitBranchable (ec, tests_end, false);
3025 right.EmitBranchable (ec, target, true);
3026 ig.MarkLabel (tests_end);
3028 left.EmitBranchable (ec, target, false);
3029 right.EmitBranchable (ec, target, false);
3034 } else if (oper == Operator.LogicalOr){
3036 left.EmitBranchable (ec, target, true);
3037 right.EmitBranchable (ec, target, true);
3040 Label tests_end = ig.DefineLabel ();
3041 left.EmitBranchable (ec, tests_end, true);
3042 right.EmitBranchable (ec, target, false);
3043 ig.MarkLabel (tests_end);
3048 } else if (!(oper == Operator.LessThan || oper == Operator.GreaterThan ||
3049 oper == Operator.LessThanOrEqual || oper == Operator.GreaterThanOrEqual ||
3050 oper == Operator.Equality || oper == Operator.Inequality)) {
3051 base.EmitBranchable (ec, target, onTrue);
3059 bool isUnsigned = is_unsigned (t) || t == TypeManager.double_type || t == TypeManager.float_type;
3062 case Operator.Equality:
3064 ig.Emit (OpCodes.Beq, target);
3066 ig.Emit (OpCodes.Bne_Un, target);
3069 case Operator.Inequality:
3071 ig.Emit (OpCodes.Bne_Un, target);
3073 ig.Emit (OpCodes.Beq, target);
3076 case Operator.LessThan:
3079 ig.Emit (OpCodes.Blt_Un, target);
3081 ig.Emit (OpCodes.Blt, target);
3084 ig.Emit (OpCodes.Bge_Un, target);
3086 ig.Emit (OpCodes.Bge, target);
3089 case Operator.GreaterThan:
3092 ig.Emit (OpCodes.Bgt_Un, target);
3094 ig.Emit (OpCodes.Bgt, target);
3097 ig.Emit (OpCodes.Ble_Un, target);
3099 ig.Emit (OpCodes.Ble, target);
3102 case Operator.LessThanOrEqual:
3105 ig.Emit (OpCodes.Ble_Un, target);
3107 ig.Emit (OpCodes.Ble, target);
3110 ig.Emit (OpCodes.Bgt_Un, target);
3112 ig.Emit (OpCodes.Bgt, target);
3116 case Operator.GreaterThanOrEqual:
3119 ig.Emit (OpCodes.Bge_Un, target);
3121 ig.Emit (OpCodes.Bge, target);
3124 ig.Emit (OpCodes.Blt_Un, target);
3126 ig.Emit (OpCodes.Blt, target);
3129 Console.WriteLine (oper);
3130 throw new Exception ("what is THAT");
3134 public override void Emit (EmitContext ec)
3136 ILGenerator ig = ec.ig;
3141 // Handle short-circuit operators differently
3144 if (oper == Operator.LogicalAnd) {
3145 Label load_zero = ig.DefineLabel ();
3146 Label end = ig.DefineLabel ();
3148 left.EmitBranchable (ec, load_zero, false);
3150 ig.Emit (OpCodes.Br, end);
3152 ig.MarkLabel (load_zero);
3153 ig.Emit (OpCodes.Ldc_I4_0);
3156 } else if (oper == Operator.LogicalOr) {
3157 Label load_one = ig.DefineLabel ();
3158 Label end = ig.DefineLabel ();
3160 left.EmitBranchable (ec, load_one, true);
3162 ig.Emit (OpCodes.Br, end);
3164 ig.MarkLabel (load_one);
3165 ig.Emit (OpCodes.Ldc_I4_1);
3173 bool isUnsigned = is_unsigned (left.Type);
3176 case Operator.Multiply:
3178 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
3179 opcode = OpCodes.Mul_Ovf;
3180 else if (isUnsigned)
3181 opcode = OpCodes.Mul_Ovf_Un;
3183 opcode = OpCodes.Mul;
3185 opcode = OpCodes.Mul;
3189 case Operator.Division:
3191 opcode = OpCodes.Div_Un;
3193 opcode = OpCodes.Div;
3196 case Operator.Modulus:
3198 opcode = OpCodes.Rem_Un;
3200 opcode = OpCodes.Rem;
3203 case Operator.Addition:
3205 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
3206 opcode = OpCodes.Add_Ovf;
3207 else if (isUnsigned)
3208 opcode = OpCodes.Add_Ovf_Un;
3210 opcode = OpCodes.Add;
3212 opcode = OpCodes.Add;
3215 case Operator.Subtraction:
3217 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
3218 opcode = OpCodes.Sub_Ovf;
3219 else if (isUnsigned)
3220 opcode = OpCodes.Sub_Ovf_Un;
3222 opcode = OpCodes.Sub;
3224 opcode = OpCodes.Sub;
3227 case Operator.RightShift:
3229 opcode = OpCodes.Shr_Un;
3231 opcode = OpCodes.Shr;
3234 case Operator.LeftShift:
3235 opcode = OpCodes.Shl;
3238 case Operator.Equality:
3239 opcode = OpCodes.Ceq;
3242 case Operator.Inequality:
3243 ig.Emit (OpCodes.Ceq);
3244 ig.Emit (OpCodes.Ldc_I4_0);
3246 opcode = OpCodes.Ceq;
3249 case Operator.LessThan:
3251 opcode = OpCodes.Clt_Un;
3253 opcode = OpCodes.Clt;
3256 case Operator.GreaterThan:
3258 opcode = OpCodes.Cgt_Un;
3260 opcode = OpCodes.Cgt;
3263 case Operator.LessThanOrEqual:
3264 Type lt = left.Type;
3266 if (isUnsigned || (lt == TypeManager.double_type || lt == TypeManager.float_type))
3267 ig.Emit (OpCodes.Cgt_Un);
3269 ig.Emit (OpCodes.Cgt);
3270 ig.Emit (OpCodes.Ldc_I4_0);
3272 opcode = OpCodes.Ceq;
3275 case Operator.GreaterThanOrEqual:
3276 Type le = left.Type;
3278 if (isUnsigned || (le == TypeManager.double_type || le == TypeManager.float_type))
3279 ig.Emit (OpCodes.Clt_Un);
3281 ig.Emit (OpCodes.Clt);
3283 ig.Emit (OpCodes.Ldc_I4_0);
3285 opcode = OpCodes.Ceq;
3288 case Operator.BitwiseOr:
3289 opcode = OpCodes.Or;
3292 case Operator.BitwiseAnd:
3293 opcode = OpCodes.And;
3296 case Operator.ExclusiveOr:
3297 opcode = OpCodes.Xor;
3301 throw new Exception ("This should not happen: Operator = "
3302 + oper.ToString ());
3310 // Object created by Binary when the binary operator uses an method instead of being
3311 // a binary operation that maps to a CIL binary operation.
3313 public class BinaryMethod : Expression {
3314 public MethodBase method;
3315 public ArrayList Arguments;
3317 public BinaryMethod (Type t, MethodBase m, ArrayList args)
3322 eclass = ExprClass.Value;
3325 public override Expression DoResolve (EmitContext ec)
3330 public override void Emit (EmitContext ec)
3332 ILGenerator ig = ec.ig;
3334 if (Arguments != null)
3335 Invocation.EmitArguments (ec, method, Arguments, false, null);
3337 if (method is MethodInfo)
3338 ig.Emit (OpCodes.Call, (MethodInfo) method);
3340 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3345 // Represents the operation a + b [+ c [+ d [+ ...]]], where a is a string
3346 // b, c, d... may be strings or objects.
3348 public class StringConcat : Expression {
3350 bool invalid = false;
3351 bool emit_conv_done = false;
3353 // Are we also concating objects?
3355 bool is_strings_only = true;
3357 public StringConcat (EmitContext ec, Location loc, Expression left, Expression right)
3360 type = TypeManager.string_type;
3361 eclass = ExprClass.Value;
3363 operands = new ArrayList (2);
3368 public override Expression DoResolve (EmitContext ec)
3376 public void Append (EmitContext ec, Expression operand)
3381 if (operand is StringConstant && operands.Count != 0) {
3382 StringConstant last_operand = operands [operands.Count - 1] as StringConstant;
3383 if (last_operand != null) {
3384 operands [operands.Count - 1] = new StringConstant (last_operand.Value + ((StringConstant) operand).Value);
3390 // Conversion to object
3392 if (operand.Type != TypeManager.string_type) {
3393 Expression no = Convert.ImplicitConversion (ec, operand, TypeManager.object_type, loc);
3396 Binary.Error_OperatorCannotBeApplied (loc, "+", TypeManager.string_type, operand.Type);
3402 operands.Add (operand);
3405 public override void Emit (EmitContext ec)
3407 MethodInfo concat_method = null;
3410 // Do conversion to arguments; check for strings only
3413 // This can get called multiple times, so we have to deal with that.
3414 if (!emit_conv_done) {
3415 emit_conv_done = true;
3416 for (int i = 0; i < operands.Count; i ++) {
3417 Expression e = (Expression) operands [i];
3418 is_strings_only &= e.Type == TypeManager.string_type;
3421 for (int i = 0; i < operands.Count; i ++) {
3422 Expression e = (Expression) operands [i];
3424 if (! is_strings_only && e.Type == TypeManager.string_type) {
3425 // need to make sure this is an object, because the EmitParams
3426 // method might look at the type of this expression, see it is a
3427 // string and emit a string [] when we want an object [];
3429 e = new EmptyCast (e, TypeManager.object_type);
3431 operands [i] = new Argument (e, Argument.AType.Expression);
3436 // Find the right method
3438 switch (operands.Count) {
3441 // This should not be possible, because simple constant folding
3442 // is taken care of in the Binary code.
3444 throw new Exception ("how did you get here?");
3447 concat_method = is_strings_only ?
3448 TypeManager.string_concat_string_string :
3449 TypeManager.string_concat_object_object ;
3452 concat_method = is_strings_only ?
3453 TypeManager.string_concat_string_string_string :
3454 TypeManager.string_concat_object_object_object ;
3458 // There is not a 4 param overlaod for object (the one that there is
3459 // is actually a varargs methods, and is only in corlib because it was
3460 // introduced there before.).
3462 if (!is_strings_only)
3465 concat_method = TypeManager.string_concat_string_string_string_string;
3468 concat_method = is_strings_only ?
3469 TypeManager.string_concat_string_dot_dot_dot :
3470 TypeManager.string_concat_object_dot_dot_dot ;
3474 Invocation.EmitArguments (ec, concat_method, operands, false, null);
3475 ec.ig.Emit (OpCodes.Call, concat_method);
3480 // Object created with +/= on delegates
3482 public class BinaryDelegate : Expression {
3486 public BinaryDelegate (Type t, MethodInfo mi, ArrayList args)
3491 eclass = ExprClass.Value;
3494 public override Expression DoResolve (EmitContext ec)
3499 public override void Emit (EmitContext ec)
3501 ILGenerator ig = ec.ig;
3503 Invocation.EmitArguments (ec, method, args, false, null);
3505 ig.Emit (OpCodes.Call, (MethodInfo) method);
3506 ig.Emit (OpCodes.Castclass, type);
3509 public Expression Right {
3511 Argument arg = (Argument) args [1];
3516 public bool IsAddition {
3518 return method == TypeManager.delegate_combine_delegate_delegate;
3524 // User-defined conditional logical operator
3525 public class ConditionalLogicalOperator : Expression {
3526 Expression left, right;
3529 public ConditionalLogicalOperator (bool is_and, Expression left, Expression right, Type t, Location loc)
3532 eclass = ExprClass.Value;
3536 this.is_and = is_and;
3539 protected void Error19 ()
3541 Binary.Error_OperatorCannotBeApplied (loc, is_and ? "&&" : "||", type, type);
3544 protected void Error218 ()
3546 Error (218, "The type ('" + TypeManager.CSharpName (type) + "') must contain " +
3547 "declarations of operator true and operator false");
3550 Expression op_true, op_false, op;
3551 LocalTemporary left_temp;
3553 public override Expression DoResolve (EmitContext ec)
3556 Expression operator_group;
3558 operator_group = MethodLookup (ec, type, is_and ? "op_BitwiseAnd" : "op_BitwiseOr", loc);
3559 if (operator_group == null) {
3564 left_temp = new LocalTemporary (ec, type);
3566 ArrayList arguments = new ArrayList ();
3567 arguments.Add (new Argument (left_temp, Argument.AType.Expression));
3568 arguments.Add (new Argument (right, Argument.AType.Expression));
3569 method = Invocation.OverloadResolve (
3570 ec, (MethodGroupExpr) operator_group, arguments, false, loc)
3572 if (method == null) {
3577 if (method.ReturnType != type) {
3578 Report.Error (217, loc, "In order to be applicable as a short circuit operator a user-defined logical operator `{0}' " +
3579 "must have the same return type as the type of its 2 parameters", TypeManager.CSharpSignature (method));
3583 op = new StaticCallExpr (method, arguments, loc);
3585 op_true = GetOperatorTrue (ec, left_temp, loc);
3586 op_false = GetOperatorFalse (ec, left_temp, loc);
3587 if ((op_true == null) || (op_false == null)) {
3595 public override void Emit (EmitContext ec)
3597 ILGenerator ig = ec.ig;
3598 Label false_target = ig.DefineLabel ();
3599 Label end_target = ig.DefineLabel ();
3602 left_temp.Store (ec);
3604 (is_and ? op_false : op_true).EmitBranchable (ec, false_target, false);
3605 left_temp.Emit (ec);
3606 ig.Emit (OpCodes.Br, end_target);
3607 ig.MarkLabel (false_target);
3609 ig.MarkLabel (end_target);
3613 public class PointerArithmetic : Expression {
3614 Expression left, right;
3618 // We assume that `l' is always a pointer
3620 public PointerArithmetic (bool is_addition, Expression l, Expression r, Type t, Location loc)
3626 is_add = is_addition;
3629 public override Expression DoResolve (EmitContext ec)
3631 eclass = ExprClass.Variable;
3633 if (left.Type == TypeManager.void_ptr_type) {
3634 Error (242, "The operation in question is undefined on void pointers");
3641 public override void Emit (EmitContext ec)
3643 Type op_type = left.Type;
3644 ILGenerator ig = ec.ig;
3646 // It must be either array or fixed buffer
3647 Type element = TypeManager.HasElementType (op_type) ?
3648 element = TypeManager.GetElementType (op_type) :
3649 element = AttributeTester.GetFixedBuffer (((FieldExpr)left).FieldInfo).ElementType;
3651 int size = GetTypeSize (element);
3652 Type rtype = right.Type;
3654 if (rtype.IsPointer){
3656 // handle (pointer - pointer)
3660 ig.Emit (OpCodes.Sub);
3664 ig.Emit (OpCodes.Sizeof, element);
3666 IntLiteral.EmitInt (ig, size);
3667 ig.Emit (OpCodes.Div);
3669 ig.Emit (OpCodes.Conv_I8);
3672 // handle + and - on (pointer op int)
3675 ig.Emit (OpCodes.Conv_I);
3677 Constant right_const = right as Constant;
3678 if (right_const != null && size != 0) {
3679 Expression ex = ConstantFold.BinaryFold (ec, Binary.Operator.Multiply, new IntConstant (size), right_const, loc);
3687 ig.Emit (OpCodes.Sizeof, element);
3689 IntLiteral.EmitInt (ig, size);
3690 if (rtype == TypeManager.int64_type)
3691 ig.Emit (OpCodes.Conv_I8);
3692 else if (rtype == TypeManager.uint64_type)
3693 ig.Emit (OpCodes.Conv_U8);
3694 ig.Emit (OpCodes.Mul);
3698 if (rtype == TypeManager.int64_type || rtype == TypeManager.uint64_type)
3699 ig.Emit (OpCodes.Conv_I);
3702 ig.Emit (OpCodes.Add);
3704 ig.Emit (OpCodes.Sub);
3710 /// Implements the ternary conditional operator (?:)
3712 public class Conditional : Expression {
3713 Expression expr, trueExpr, falseExpr;
3715 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr)
3718 this.trueExpr = trueExpr;
3719 this.falseExpr = falseExpr;
3720 this.loc = expr.Location;
3723 public Expression Expr {
3729 public Expression TrueExpr {
3735 public Expression FalseExpr {
3741 public override Expression DoResolve (EmitContext ec)
3743 expr = expr.Resolve (ec);
3748 if (TypeManager.IsNullableType (expr.Type))
3749 return new Nullable.LiftedConditional (expr, trueExpr, falseExpr, loc).Resolve (ec);
3751 if (expr.Type != TypeManager.bool_type){
3752 expr = Expression.ResolveBoolean (
3759 trueExpr = trueExpr.Resolve (ec);
3760 falseExpr = falseExpr.Resolve (ec);
3762 if (trueExpr == null || falseExpr == null)
3765 eclass = ExprClass.Value;
3766 if (trueExpr.Type == falseExpr.Type)
3767 type = trueExpr.Type;
3770 Type true_type = trueExpr.Type;
3771 Type false_type = falseExpr.Type;
3774 // First, if an implicit conversion exists from trueExpr
3775 // to falseExpr, then the result type is of type falseExpr.Type
3777 conv = Convert.ImplicitConversion (ec, trueExpr, false_type, loc);
3780 // Check if both can convert implicitl to each other's type
3782 if (Convert.ImplicitConversion (ec, falseExpr, true_type, loc) != null){
3784 "Can not compute type of conditional expression " +
3785 "as `" + TypeManager.CSharpName (trueExpr.Type) +
3786 "' and `" + TypeManager.CSharpName (falseExpr.Type) +
3787 "' convert implicitly to each other");
3792 } else if ((conv = Convert.ImplicitConversion(ec, falseExpr, true_type,loc))!= null){
3796 Report.Error (173, loc, "Type of conditional expression cannot be determined because there is no implicit conversion between `{0}' and `{1}'",
3797 trueExpr.GetSignatureForError (), falseExpr.GetSignatureForError ());
3802 // Dead code optimalization
3803 if (expr is BoolConstant){
3804 BoolConstant bc = (BoolConstant) expr;
3806 Report.Warning (429, 4, bc.Value ? falseExpr.Location : trueExpr.Location, "Unreachable expression code detected");
3807 return bc.Value ? trueExpr : falseExpr;
3813 public override void Emit (EmitContext ec)
3815 ILGenerator ig = ec.ig;
3816 Label false_target = ig.DefineLabel ();
3817 Label end_target = ig.DefineLabel ();
3819 expr.EmitBranchable (ec, false_target, false);
3821 ig.Emit (OpCodes.Br, end_target);
3822 ig.MarkLabel (false_target);
3823 falseExpr.Emit (ec);
3824 ig.MarkLabel (end_target);
3832 public class LocalVariableReference : Expression, IAssignMethod, IMemoryLocation, IVariable {
3833 public readonly string Name;
3834 public readonly Block Block;
3835 public LocalInfo local_info;
3838 LocalTemporary temp;
3840 public LocalVariableReference (Block block, string name, Location l)
3845 eclass = ExprClass.Variable;
3849 // Setting `is_readonly' to false will allow you to create a writable
3850 // reference to a read-only variable. This is used by foreach and using.
3852 public LocalVariableReference (Block block, string name, Location l,
3853 LocalInfo local_info, bool is_readonly)
3854 : this (block, name, l)
3856 this.local_info = local_info;
3857 this.is_readonly = is_readonly;
3860 public VariableInfo VariableInfo {
3862 return local_info.VariableInfo;
3866 public bool IsReadOnly {
3872 protected Expression DoResolveBase (EmitContext ec, Expression lvalue_right_side)
3874 if (local_info == null) {
3875 local_info = Block.GetLocalInfo (Name);
3878 if (lvalue_right_side == EmptyExpression.Null)
3879 local_info.Used = true;
3881 is_readonly = local_info.ReadOnly;
3884 type = local_info.VariableType;
3886 VariableInfo variable_info = local_info.VariableInfo;
3887 if (lvalue_right_side != null){
3889 if (lvalue_right_side is LocalVariableReference || lvalue_right_side == EmptyExpression.Null)
3890 Report.Error (1657, loc, "Cannot pass `{0}' as a ref or out argument because it is a `{1}'",
3891 Name, local_info.GetReadOnlyContext ());
3893 Report.Error (1656, loc, "Cannot assign to `{0}' because it is a `{1}'",
3894 Name, local_info.GetReadOnlyContext ());
3898 if (variable_info != null)
3899 variable_info.SetAssigned (ec);
3902 Expression e = Block.GetConstantExpression (Name);
3904 local_info.Used = true;
3905 eclass = ExprClass.Value;
3906 return e.Resolve (ec);
3909 if ((variable_info != null) && !variable_info.IsAssigned (ec, loc))
3912 if (lvalue_right_side == null)
3913 local_info.Used = true;
3915 if (ec.CurrentAnonymousMethod != null){
3917 // If we are referencing a variable from the external block
3918 // flag it for capturing
3920 if ((local_info.Block.Toplevel != ec.CurrentBlock.Toplevel) ||
3921 ec.CurrentAnonymousMethod.IsIterator)
3923 if (local_info.AddressTaken){
3924 AnonymousMethod.Error_AddressOfCapturedVar (local_info.Name, loc);
3927 ec.CaptureVariable (local_info);
3934 public override Expression DoResolve (EmitContext ec)
3936 return DoResolveBase (ec, null);
3939 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3941 Expression ret = DoResolveBase (ec, right_side);
3943 CheckObsoleteAttribute (ret.Type);
3948 public bool VerifyFixed ()
3950 // A local Variable is always fixed.
3954 public override int GetHashCode()
3956 return Name.GetHashCode ();
3959 public override bool Equals (object obj)
3961 LocalVariableReference lvr = obj as LocalVariableReference;
3965 return Name == lvr.Name && Block == lvr.Block;
3968 public override void Emit (EmitContext ec)
3970 ILGenerator ig = ec.ig;
3972 if (local_info.FieldBuilder == null){
3974 // A local variable on the local CLR stack
3976 ig.Emit (OpCodes.Ldloc, local_info.LocalBuilder);
3979 // A local variable captured by anonymous methods.
3982 ec.EmitCapturedVariableInstance (local_info);
3984 ig.Emit (OpCodes.Ldfld, local_info.FieldBuilder);
3988 public void Emit (EmitContext ec, bool leave_copy)
3992 ec.ig.Emit (OpCodes.Dup);
3993 if (local_info.FieldBuilder != null){
3994 temp = new LocalTemporary (ec, Type);
4000 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
4002 ILGenerator ig = ec.ig;
4003 prepared = prepare_for_load;
4005 if (local_info.FieldBuilder == null){
4007 // A local variable on the local CLR stack
4009 if (local_info.LocalBuilder == null)
4010 throw new Exception ("This should not happen: both Field and Local are null");
4014 ec.ig.Emit (OpCodes.Dup);
4015 ig.Emit (OpCodes.Stloc, local_info.LocalBuilder);
4018 // A local variable captured by anonymous methods or itereators.
4020 ec.EmitCapturedVariableInstance (local_info);
4022 if (prepare_for_load)
4023 ig.Emit (OpCodes.Dup);
4026 ig.Emit (OpCodes.Dup);
4027 temp = new LocalTemporary (ec, Type);
4030 ig.Emit (OpCodes.Stfld, local_info.FieldBuilder);
4036 public void AddressOf (EmitContext ec, AddressOp mode)
4038 ILGenerator ig = ec.ig;
4040 if (local_info.FieldBuilder == null){
4042 // A local variable on the local CLR stack
4044 ig.Emit (OpCodes.Ldloca, local_info.LocalBuilder);
4047 // A local variable captured by anonymous methods or iterators
4049 ec.EmitCapturedVariableInstance (local_info);
4050 ig.Emit (OpCodes.Ldflda, local_info.FieldBuilder);
4054 public override string ToString ()
4056 return String.Format ("{0} ({1}:{2})", GetType (), Name, loc);
4061 /// This represents a reference to a parameter in the intermediate
4064 public class ParameterReference : Expression, IAssignMethod, IMemoryLocation, IVariable {
4070 public Parameter.Modifier mod;
4071 public bool is_ref, is_out, prepared;
4085 LocalTemporary temp;
4087 public ParameterReference (Parameters pars, Block block, int idx, string name, Location loc)
4094 eclass = ExprClass.Variable;
4097 public ParameterReference (InternalParameters pars, Block block, int idx, Location loc)
4098 : this (pars.Parameters, block, idx, pars.ParameterName (idx), loc)
4101 public VariableInfo VariableInfo {
4105 public bool VerifyFixed ()
4107 // A parameter is fixed if it's a value parameter (i.e., no modifier like out, ref, param).
4108 return mod == Parameter.Modifier.NONE;
4111 public bool IsAssigned (EmitContext ec, Location loc)
4113 if (!ec.DoFlowAnalysis || !is_out || ec.CurrentBranching.IsAssigned (vi))
4116 Report.Error (269, loc,
4117 "Use of unassigned out parameter `{0}'", name);
4121 public bool IsFieldAssigned (EmitContext ec, string field_name, Location loc)
4123 if (!ec.DoFlowAnalysis || !is_out || ec.CurrentBranching.IsFieldAssigned (vi, field_name))
4126 Report.Error (170, loc,
4127 "Use of possibly unassigned field `" + field_name + "'");
4131 public void SetAssigned (EmitContext ec)
4133 if (is_out && ec.DoFlowAnalysis)
4134 ec.CurrentBranching.SetAssigned (vi);
4137 public void SetFieldAssigned (EmitContext ec, string field_name)
4139 if (is_out && ec.DoFlowAnalysis)
4140 ec.CurrentBranching.SetFieldAssigned (vi, field_name);
4143 protected void DoResolveBase (EmitContext ec)
4145 type = pars.GetParameterInfo (ec, idx, out mod);
4146 is_ref = (mod & Parameter.Modifier.ISBYREF) != 0;
4147 is_out = (mod & Parameter.Modifier.OUT) != 0;
4148 eclass = ExprClass.Variable;
4151 vi = block.ParameterMap [idx];
4153 if (ec.CurrentAnonymousMethod != null){
4155 Report.Error (1628, Location, "Cannot use ref or out parameter `{0}' inside an anonymous method block",
4161 // If we are referencing the parameter from the external block
4162 // flag it for capturing
4164 //Console.WriteLine ("Is parameter `{0}' local? {1}", name, block.IsLocalParameter (name));
4165 if (!block.Toplevel.IsLocalParameter (name)){
4166 ec.CaptureParameter (name, type, idx);
4171 public override int GetHashCode()
4173 return name.GetHashCode ();
4176 public override bool Equals (object obj)
4178 ParameterReference pr = obj as ParameterReference;
4182 return name == pr.name && block == pr.block;
4186 // Notice that for ref/out parameters, the type exposed is not the
4187 // same type exposed externally.
4190 // externally we expose "int&"
4191 // here we expose "int".
4193 // We record this in "is_ref". This means that the type system can treat
4194 // the type as it is expected, but when we generate the code, we generate
4195 // the alternate kind of code.
4197 public override Expression DoResolve (EmitContext ec)
4201 if (is_out && ec.DoFlowAnalysis && (!ec.OmitStructFlowAnalysis || !vi.TypeInfo.IsStruct) && !IsAssigned (ec, loc))
4207 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4216 static public void EmitLdArg (ILGenerator ig, int x)
4220 case 0: ig.Emit (OpCodes.Ldarg_0); break;
4221 case 1: ig.Emit (OpCodes.Ldarg_1); break;
4222 case 2: ig.Emit (OpCodes.Ldarg_2); break;
4223 case 3: ig.Emit (OpCodes.Ldarg_3); break;
4224 default: ig.Emit (OpCodes.Ldarg_S, (byte) x); break;
4227 ig.Emit (OpCodes.Ldarg, x);
4231 // This method is used by parameters that are references, that are
4232 // being passed as references: we only want to pass the pointer (that
4233 // is already stored in the parameter, not the address of the pointer,
4234 // and not the value of the variable).
4236 public void EmitLoad (EmitContext ec)
4238 ILGenerator ig = ec.ig;
4241 if (!ec.MethodIsStatic)
4244 EmitLdArg (ig, arg_idx);
4247 // FIXME: Review for anonymous methods
4251 public override void Emit (EmitContext ec)
4256 public void Emit (EmitContext ec, bool leave_copy)
4258 ILGenerator ig = ec.ig;
4261 if (ec.HaveCaptureInfo && ec.IsParameterCaptured (name)){
4263 throw new InternalErrorException ();
4265 ec.EmitParameter (name);
4269 if (!ec.MethodIsStatic)
4272 EmitLdArg (ig, arg_idx);
4276 ec.ig.Emit (OpCodes.Dup);
4279 // If we are a reference, we loaded on the stack a pointer
4280 // Now lets load the real value
4282 LoadFromPtr (ig, type);
4286 ec.ig.Emit (OpCodes.Dup);
4289 temp = new LocalTemporary (ec, type);
4295 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
4297 if (ec.HaveCaptureInfo && ec.IsParameterCaptured (name)){
4298 ec.EmitAssignParameter (name, source, leave_copy, prepare_for_load);
4302 ILGenerator ig = ec.ig;
4305 prepared = prepare_for_load;
4307 if (!ec.MethodIsStatic)
4310 if (is_ref && !prepared)
4311 EmitLdArg (ig, arg_idx);
4316 ec.ig.Emit (OpCodes.Dup);
4320 temp = new LocalTemporary (ec, type);
4324 StoreFromPtr (ig, type);
4330 ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
4332 ig.Emit (OpCodes.Starg, arg_idx);
4336 public void AddressOf (EmitContext ec, AddressOp mode)
4338 if (ec.HaveCaptureInfo && ec.IsParameterCaptured (name)){
4339 ec.EmitAddressOfParameter (name);
4345 if (!ec.MethodIsStatic)
4350 ec.ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
4352 ec.ig.Emit (OpCodes.Ldarg, arg_idx);
4355 ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
4357 ec.ig.Emit (OpCodes.Ldarga, arg_idx);
4364 /// Used for arguments to New(), Invocation()
4366 public class Argument {
4367 public enum AType : byte {
4374 public readonly AType ArgType;
4375 public Expression Expr;
4377 public Argument (Expression expr, AType type)
4380 this.ArgType = type;
4383 public Argument (Expression expr)
4386 this.ArgType = AType.Expression;
4391 if (ArgType == AType.Ref || ArgType == AType.Out)
4392 return TypeManager.GetReferenceType (Expr.Type);
4398 public Parameter.Modifier Modifier
4403 return Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF;
4406 return Parameter.Modifier.REF | Parameter.Modifier.ISBYREF;
4409 return Parameter.Modifier.NONE;
4414 public static string FullDesc (Argument a)
4416 if (a.ArgType == AType.ArgList)
4419 return (a.ArgType == AType.Ref ? "ref " :
4420 (a.ArgType == AType.Out ? "out " : "")) +
4421 TypeManager.CSharpName (a.Expr.Type);
4424 public bool ResolveMethodGroup (EmitContext ec, Location loc)
4426 SimpleName sn = Expr as SimpleName;
4428 Expr = sn.GetMethodGroup ();
4430 // FIXME: csc doesn't report any error if you try to use `ref' or
4431 // `out' in a delegate creation expression.
4432 Expr = Expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
4439 void Error_LValueRequired (Location loc)
4441 Report.Error (1510, loc, "A ref or out argument must be an assignable variable");
4444 public bool Resolve (EmitContext ec, Location loc)
4446 bool old_do_flow_analysis = ec.DoFlowAnalysis;
4447 ec.DoFlowAnalysis = true;
4449 if (ArgType == AType.Ref) {
4450 ec.InRefOutArgumentResolving = true;
4451 Expr = Expr.Resolve (ec);
4452 ec.InRefOutArgumentResolving = false;
4454 ec.DoFlowAnalysis = old_do_flow_analysis;
4458 Expr = Expr.DoResolveLValue (ec, Expr);
4460 Error_LValueRequired (loc);
4461 } else if (ArgType == AType.Out) {
4462 ec.InRefOutArgumentResolving = true;
4463 Expr = Expr.DoResolveLValue (ec, EmptyExpression.Null);
4464 ec.InRefOutArgumentResolving = false;
4467 Error_LValueRequired (loc);
4470 Expr = Expr.Resolve (ec);
4472 ec.DoFlowAnalysis = old_do_flow_analysis;
4477 if (ArgType == AType.Expression)
4481 // Catch errors where fields of a MarshalByRefObject are passed as ref or out
4482 // This is only allowed for `this'
4484 FieldExpr fe = Expr as FieldExpr;
4485 if (fe != null && !fe.IsStatic){
4486 Expression instance = fe.InstanceExpression;
4488 if (instance.GetType () != typeof (This)){
4489 if (fe.InstanceExpression.Type.IsSubclassOf (TypeManager.mbr_type)){
4490 Report.SymbolRelatedToPreviousError (fe.InstanceExpression.Type);
4491 Report.Warning (197, 1, loc,
4492 "Passing `{0}' as ref or out or taking its address may cause a runtime exception because it is a field of a marshal-by-reference class",
4493 fe.GetSignatureForError ());
4500 if (Expr.eclass != ExprClass.Variable){
4502 // We just probe to match the CSC output
4504 if (Expr.eclass == ExprClass.PropertyAccess ||
4505 Expr.eclass == ExprClass.IndexerAccess){
4506 Report.Error (206, loc, "A property or indexer `{0}' may not be passed as an out or ref parameter",
4507 Expr.GetSignatureForError ());
4509 Error_LValueRequired (loc);
4517 public void Emit (EmitContext ec)
4520 // Ref and Out parameters need to have their addresses taken.
4522 // ParameterReferences might already be references, so we want
4523 // to pass just the value
4525 if (ArgType == AType.Ref || ArgType == AType.Out){
4526 AddressOp mode = AddressOp.Store;
4528 if (ArgType == AType.Ref)
4529 mode |= AddressOp.Load;
4531 if (Expr is ParameterReference){
4532 ParameterReference pr = (ParameterReference) Expr;
4538 pr.AddressOf (ec, mode);
4541 if (Expr is IMemoryLocation)
4542 ((IMemoryLocation) Expr).AddressOf (ec, mode);
4544 Error_LValueRequired (Expr.Location);
4554 /// Invocation of methods or delegates.
4556 public class Invocation : ExpressionStatement {
4557 public readonly ArrayList Arguments;
4560 MethodBase method = null;
4563 // arguments is an ArrayList, but we do not want to typecast,
4564 // as it might be null.
4566 // FIXME: only allow expr to be a method invocation or a
4567 // delegate invocation (7.5.5)
4569 public Invocation (Expression expr, ArrayList arguments)
4572 Arguments = arguments;
4573 loc = expr.Location;
4576 public Expression Expr {
4583 /// Determines "better conversion" as specified in 14.4.2.3
4585 /// Returns : p if a->p is better,
4586 /// q if a->q is better,
4587 /// null if neither is better
4589 static Type BetterConversion (EmitContext ec, Argument a, Type p, Type q, Location loc)
4591 Type argument_type = TypeManager.TypeToCoreType (a.Type);
4592 Expression argument_expr = a.Expr;
4594 // p = TypeManager.TypeToCoreType (p);
4595 // q = TypeManager.TypeToCoreType (q);
4597 if (argument_type == null)
4598 throw new Exception ("Expression of type " + a.Expr +
4599 " does not resolve its type");
4601 if (p == null || q == null)
4602 throw new InternalErrorException ("BetterConversion Got a null conversion");
4607 if (argument_expr is NullLiteral) {
4609 // If the argument is null and one of the types to compare is 'object' and
4610 // the other is a reference type, we prefer the other.
4612 // This follows from the usual rules:
4613 // * There is an implicit conversion from 'null' to type 'object'
4614 // * There is an implicit conversion from 'null' to any reference type
4615 // * There is an implicit conversion from any reference type to type 'object'
4616 // * There is no implicit conversion from type 'object' to other reference types
4617 // => Conversion of 'null' to a reference type is better than conversion to 'object'
4619 // FIXME: This probably isn't necessary, since the type of a NullLiteral is the
4620 // null type. I think it used to be 'object' and thus needed a special
4621 // case to avoid the immediately following two checks.
4623 if (!p.IsValueType && q == TypeManager.object_type)
4625 if (!q.IsValueType && p == TypeManager.object_type)
4629 if (argument_type == p)
4632 if (argument_type == q)
4635 Expression p_tmp = new EmptyExpression (p);
4636 Expression q_tmp = new EmptyExpression (q);
4638 bool p_to_q = Convert.ImplicitConversionExists (ec, p_tmp, q);
4639 bool q_to_p = Convert.ImplicitConversionExists (ec, q_tmp, p);
4641 if (p_to_q && !q_to_p)
4644 if (q_to_p && !p_to_q)
4647 if (p == TypeManager.sbyte_type)
4648 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
4649 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
4651 if (q == TypeManager.sbyte_type)
4652 if (p == TypeManager.byte_type || p == TypeManager.ushort_type ||
4653 p == TypeManager.uint32_type || p == TypeManager.uint64_type)
4656 if (p == TypeManager.short_type)
4657 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
4658 q == TypeManager.uint64_type)
4661 if (q == TypeManager.short_type)
4662 if (p == TypeManager.ushort_type || p == TypeManager.uint32_type ||
4663 p == TypeManager.uint64_type)
4666 if (p == TypeManager.int32_type)
4667 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
4670 if (q == TypeManager.int32_type)
4671 if (p == TypeManager.uint32_type || p == TypeManager.uint64_type)
4674 if (p == TypeManager.int64_type)
4675 if (q == TypeManager.uint64_type)
4677 if (q == TypeManager.int64_type)
4678 if (p == TypeManager.uint64_type)
4685 /// Determines "Better function" between candidate
4686 /// and the current best match
4689 /// Returns a boolean indicating :
4690 /// false if candidate ain't better
4691 /// true if candidate is better than the current best match
4693 static bool BetterFunction (EmitContext ec, ArrayList args, int argument_count,
4694 MethodBase candidate, bool candidate_params,
4695 MethodBase best, bool best_params, Location loc)
4697 ParameterData candidate_pd = TypeManager.GetParameterData (candidate);
4698 ParameterData best_pd = TypeManager.GetParameterData (best);
4700 bool better_at_least_one = false;
4702 for (int j = 0; j < argument_count; ++j) {
4703 Argument a = (Argument) args [j];
4705 Type ct = TypeManager.TypeToCoreType (candidate_pd.ParameterType (j));
4706 Type bt = TypeManager.TypeToCoreType (best_pd.ParameterType (j));
4708 if (candidate_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
4709 if (candidate_params)
4710 ct = TypeManager.GetElementType (ct);
4712 if (best_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
4714 bt = TypeManager.GetElementType (bt);
4720 Type better = BetterConversion (ec, a, ct, bt, loc);
4721 // for each argument, the conversion to 'ct' should be no worse than
4722 // the conversion to 'bt'.
4726 // for at least one argument, the conversion to 'ct' should be better than
4727 // the conversion to 'bt'.
4729 better_at_least_one = true;
4732 if (better_at_least_one)
4739 // If two methods have equal parameter types, but
4740 // only one of them is generic, the non-generic one wins.
4742 if (TypeManager.IsGenericMethod (best) && !TypeManager.IsGenericMethod (candidate))
4744 else if (!TypeManager.IsGenericMethod (best) && TypeManager.IsGenericMethod (candidate))
4748 // Note that this is not just an optimization. This handles the case
4749 // This handles the case
4751 // Add (float f1, float f2, float f3);
4752 // Add (params decimal [] foo);
4754 // The call Add (3, 4, 5) should be ambiguous. Without this check, the
4755 // first candidate would've chosen as better.
4758 // This handles the following cases:
4760 // Trim () is better than Trim (params char[] chars)
4761 // Concat (string s1, string s2, string s3) is better than
4762 // Concat (string s1, params string [] srest)
4764 return !candidate_params && best_params;
4767 static bool IsOverride (MethodBase cand_method, MethodBase base_method)
4769 if (!IsAncestralType (base_method.DeclaringType, cand_method.DeclaringType))
4772 ParameterData cand_pd = TypeManager.GetParameterData (cand_method);
4773 ParameterData base_pd = TypeManager.GetParameterData (base_method);
4775 if (cand_pd.Count != base_pd.Count)
4778 for (int j = 0; j < cand_pd.Count; ++j) {
4779 Parameter.Modifier cm = cand_pd.ParameterModifier (j);
4780 Parameter.Modifier bm = base_pd.ParameterModifier (j);
4781 Type ct = TypeManager.TypeToCoreType (cand_pd.ParameterType (j));
4782 Type bt = TypeManager.TypeToCoreType (base_pd.ParameterType (j));
4784 if (cm != bm || ct != bt)
4791 public static string FullMethodDesc (MethodBase mb)
4797 if (mb is MethodInfo) {
4798 sb = new StringBuilder (TypeManager.CSharpName (((MethodInfo) mb).ReturnType));
4802 sb = new StringBuilder ();
4804 sb.Append (TypeManager.CSharpSignature (mb));
4805 return sb.ToString ();
4808 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2, Location loc)
4810 MemberInfo [] miset;
4811 MethodGroupExpr union;
4816 return (MethodGroupExpr) mg2;
4819 return (MethodGroupExpr) mg1;
4822 MethodGroupExpr left_set = null, right_set = null;
4823 int length1 = 0, length2 = 0;
4825 left_set = (MethodGroupExpr) mg1;
4826 length1 = left_set.Methods.Length;
4828 right_set = (MethodGroupExpr) mg2;
4829 length2 = right_set.Methods.Length;
4831 ArrayList common = new ArrayList ();
4833 foreach (MethodBase r in right_set.Methods){
4834 if (TypeManager.ArrayContainsMethod (left_set.Methods, r))
4838 miset = new MemberInfo [length1 + length2 - common.Count];
4839 left_set.Methods.CopyTo (miset, 0);
4843 foreach (MethodBase r in right_set.Methods) {
4844 if (!common.Contains (r))
4848 union = new MethodGroupExpr (miset, loc);
4853 public static bool IsParamsMethodApplicable (EmitContext ec, MethodGroupExpr me,
4854 ArrayList arguments, int arg_count,
4855 ref MethodBase candidate)
4857 return IsParamsMethodApplicable (
4858 ec, me, arguments, arg_count, false, ref candidate) ||
4859 IsParamsMethodApplicable (
4860 ec, me, arguments, arg_count, true, ref candidate);
4865 static bool IsParamsMethodApplicable (EmitContext ec, MethodGroupExpr me,
4866 ArrayList arguments, int arg_count,
4867 bool do_varargs, ref MethodBase candidate)
4869 if (!me.HasTypeArguments &&
4870 !TypeManager.InferParamsTypeArguments (ec, arguments, ref candidate))
4873 return IsParamsMethodApplicable (
4874 ec, arguments, arg_count, candidate, do_varargs);
4878 /// Determines if the candidate method, if a params method, is applicable
4879 /// in its expanded form to the given set of arguments
4881 static bool IsParamsMethodApplicable (EmitContext ec, ArrayList arguments,
4882 int arg_count, MethodBase candidate,
4885 ParameterData pd = TypeManager.GetParameterData (candidate);
4887 int pd_count = pd.Count;
4892 int count = pd_count - 1;
4894 if (pd.ParameterModifier (count) != Parameter.Modifier.ARGLIST)
4896 if (pd_count != arg_count)
4899 if (pd.ParameterModifier (count) != Parameter.Modifier.PARAMS)
4903 if (count > arg_count)
4906 if (pd_count == 1 && arg_count == 0)
4910 // If we have come this far, the case which
4911 // remains is when the number of parameters is
4912 // less than or equal to the argument count.
4914 for (int i = 0; i < count; ++i) {
4916 Argument a = (Argument) arguments [i];
4918 Parameter.Modifier a_mod = a.Modifier &
4919 (unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF)));
4920 Parameter.Modifier p_mod = pd.ParameterModifier (i) &
4921 (unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF)));
4923 if (a_mod == p_mod) {
4925 if (a_mod == Parameter.Modifier.NONE)
4926 if (!Convert.ImplicitConversionExists (ec,
4928 pd.ParameterType (i)))
4931 if ((a_mod & Parameter.Modifier.ISBYREF) != 0) {
4932 Type pt = pd.ParameterType (i);
4935 pt = TypeManager.GetReferenceType (pt);
4946 Argument a = (Argument) arguments [count];
4947 if (!(a.Expr is Arglist))
4953 Type element_type = TypeManager.GetElementType (pd.ParameterType (pd_count - 1));
4955 for (int i = pd_count - 1; i < arg_count; i++) {
4956 Argument a = (Argument) arguments [i];
4958 if (!Convert.ImplicitConversionExists (ec, a.Expr, element_type))
4965 public static bool IsApplicable (EmitContext ec, MethodGroupExpr me,
4966 ArrayList arguments, int arg_count,
4967 ref MethodBase candidate)
4969 if (!me.HasTypeArguments &&
4970 !TypeManager.InferTypeArguments (ec, arguments, ref candidate))
4973 return IsApplicable (ec, arguments, arg_count, candidate);
4977 /// Determines if the candidate method is applicable (section 14.4.2.1)
4978 /// to the given set of arguments
4980 static bool IsApplicable (EmitContext ec, ArrayList arguments, int arg_count,
4981 MethodBase candidate)
4983 ParameterData pd = TypeManager.GetParameterData (candidate);
4985 if (arg_count != pd.Count)
4988 for (int i = arg_count; i > 0; ) {
4991 Argument a = (Argument) arguments [i];
4993 Parameter.Modifier a_mod = a.Modifier &
4994 unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF));
4995 Parameter.Modifier p_mod = pd.ParameterModifier (i) &
4996 unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF));
4998 if (a_mod == p_mod ||
4999 (a_mod == Parameter.Modifier.NONE && p_mod == Parameter.Modifier.PARAMS)) {
5000 if (a_mod == Parameter.Modifier.NONE) {
5001 if (!Convert.ImplicitConversionExists (ec,
5003 pd.ParameterType (i)))
5007 if ((a_mod & Parameter.Modifier.ISBYREF) != 0) {
5008 Type pt = pd.ParameterType (i);
5011 pt = TypeManager.GetReferenceType (pt);
5023 static private bool IsAncestralType (Type first_type, Type second_type)
5025 return first_type != second_type &&
5026 (second_type.IsSubclassOf (first_type) ||
5027 TypeManager.ImplementsInterface (second_type, first_type));
5031 /// Find the Applicable Function Members (7.4.2.1)
5033 /// me: Method Group expression with the members to select.
5034 /// it might contain constructors or methods (or anything
5035 /// that maps to a method).
5037 /// Arguments: ArrayList containing resolved Argument objects.
5039 /// loc: The location if we want an error to be reported, or a Null
5040 /// location for "probing" purposes.
5042 /// Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
5043 /// that is the best match of me on Arguments.
5046 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
5047 ArrayList Arguments, bool may_fail,
5050 MethodBase method = null;
5051 bool method_params = false;
5052 Type applicable_type = null;
5054 ArrayList candidates = new ArrayList (2);
5055 ArrayList candidate_overrides = null;
5058 // Used to keep a map between the candidate
5059 // and whether it is being considered in its
5060 // normal or expanded form
5062 // false is normal form, true is expanded form
5064 Hashtable candidate_to_form = null;
5066 if (Arguments != null)
5067 arg_count = Arguments.Count;
5069 if ((me.Name == "Invoke") &&
5070 TypeManager.IsDelegateType (me.DeclaringType)) {
5071 Error_InvokeOnDelegate (loc);
5075 MethodBase[] methods = me.Methods;
5078 // First we construct the set of applicable methods
5080 bool is_sorted = true;
5081 for (int i = 0; i < methods.Length; i++){
5082 Type decl_type = methods [i].DeclaringType;
5085 // If we have already found an applicable method
5086 // we eliminate all base types (Section 14.5.5.1)
5088 if ((applicable_type != null) &&
5089 IsAncestralType (decl_type, applicable_type))
5093 // Methods marked 'override' don't take part in 'applicable_type'
5094 // computation, nor in the actual overload resolution.
5095 // However, they still need to be emitted instead of a base virtual method.
5096 // We avoid doing the 'applicable' test here, since it'll anyway be applied
5097 // to the base virtual function, and IsOverride is much faster than IsApplicable.
5099 if (!me.IsBase && TypeManager.IsOverride (methods [i])) {
5100 if (candidate_overrides == null)
5101 candidate_overrides = new ArrayList ();
5102 candidate_overrides.Add (methods [i]);
5107 // Check if candidate is applicable (section 14.4.2.1)
5108 // Is candidate applicable in normal form?
5110 bool is_applicable = IsApplicable (
5111 ec, me, Arguments, arg_count, ref methods [i]);
5113 if (!is_applicable &&
5114 (IsParamsMethodApplicable (
5115 ec, me, Arguments, arg_count, ref methods [i]))) {
5116 MethodBase candidate = methods [i];
5117 if (candidate_to_form == null)
5118 candidate_to_form = new PtrHashtable ();
5119 candidate_to_form [candidate] = candidate;
5120 // Candidate is applicable in expanded form
5121 is_applicable = true;
5127 candidates.Add (methods [i]);
5129 if (applicable_type == null)
5130 applicable_type = decl_type;
5131 else if (applicable_type != decl_type) {
5133 if (IsAncestralType (applicable_type, decl_type))
5134 applicable_type = decl_type;
5138 int candidate_top = candidates.Count;
5140 if (applicable_type == null) {
5142 // Okay so we have failed to find anything so we
5143 // return by providing info about the closest match
5145 int errors = Report.Errors;
5146 for (int i = 0; i < methods.Length; ++i) {
5147 MethodBase c = (MethodBase) methods [i];
5148 ParameterData pd = TypeManager.GetParameterData (c);
5150 if (pd.Count != arg_count)
5153 if (!TypeManager.InferTypeArguments (ec, Arguments, ref c))
5156 VerifyArgumentsCompat (ec, Arguments, arg_count,
5157 c, false, null, may_fail, loc);
5159 if (!may_fail && errors == Report.Errors)
5160 throw new InternalErrorException (
5161 "VerifyArgumentsCompat and IsApplicable do not agree; " +
5162 "likely reason: ImplicitConversion and ImplicitConversionExists have gone out of sync");
5167 if (!may_fail && errors == Report.Errors) {
5168 string report_name = me.Name;
5169 if (report_name == ".ctor")
5170 report_name = me.DeclaringType.ToString ();
5172 for (int i = 0; i < methods.Length; ++i) {
5173 MethodBase c = methods [i];
5174 ParameterData pd = TypeManager.GetParameterData (c);
5176 if (pd.Count != arg_count)
5179 if (TypeManager.InferTypeArguments (ec, Arguments, ref c))
5183 411, loc, "The type arguments for " +
5184 "method `{0}' cannot be infered from " +
5185 "the usage. Try specifying the type " +
5186 "arguments explicitly.", report_name);
5190 Error_WrongNumArguments (loc, report_name, arg_count);
5198 // At this point, applicable_type is _one_ of the most derived types
5199 // in the set of types containing the methods in this MethodGroup.
5200 // Filter the candidates so that they only contain methods from the
5201 // most derived types.
5204 int finalized = 0; // Number of finalized candidates
5207 // Invariant: applicable_type is a most derived type
5209 // We'll try to complete Section 14.5.5.1 for 'applicable_type' by
5210 // eliminating all it's base types. At the same time, we'll also move
5211 // every unrelated type to the end of the array, and pick the next
5212 // 'applicable_type'.
5214 Type next_applicable_type = null;
5215 int j = finalized; // where to put the next finalized candidate
5216 int k = finalized; // where to put the next undiscarded candidate
5217 for (int i = finalized; i < candidate_top; ++i) {
5218 MethodBase candidate = (MethodBase) candidates [i];
5219 Type decl_type = candidate.DeclaringType;
5221 if (decl_type == applicable_type) {
5222 candidates [k++] = candidates [j];
5223 candidates [j++] = candidates [i];
5227 if (IsAncestralType (decl_type, applicable_type))
5230 if (next_applicable_type != null &&
5231 IsAncestralType (decl_type, next_applicable_type))
5234 candidates [k++] = candidates [i];
5236 if (next_applicable_type == null ||
5237 IsAncestralType (next_applicable_type, decl_type))
5238 next_applicable_type = decl_type;
5241 applicable_type = next_applicable_type;
5244 } while (applicable_type != null);
5248 // Now we actually find the best method
5251 method = (MethodBase) candidates [0];
5252 method_params = candidate_to_form != null && candidate_to_form.Contains (method);
5253 for (int ix = 1; ix < candidate_top; ix++){
5254 MethodBase candidate = (MethodBase) candidates [ix];
5256 if (candidate == method)
5259 bool cand_params = candidate_to_form != null && candidate_to_form.Contains (candidate);
5261 if (BetterFunction (ec, Arguments, arg_count,
5262 candidate, cand_params,
5263 method, method_params, loc)) {
5265 method_params = cand_params;
5269 // Now check that there are no ambiguities i.e the selected method
5270 // should be better than all the others
5272 MethodBase ambiguous = null;
5273 for (int ix = 0; ix < candidate_top; ix++){
5274 MethodBase candidate = (MethodBase) candidates [ix];
5276 if (candidate == method)
5279 bool cand_params = candidate_to_form != null && candidate_to_form.Contains (candidate);
5280 if (!BetterFunction (ec, Arguments, arg_count,
5281 method, method_params,
5282 candidate, cand_params,
5284 Report.SymbolRelatedToPreviousError (candidate);
5285 ambiguous = candidate;
5289 if (ambiguous != null) {
5290 Report.SymbolRelatedToPreviousError (method);
5291 Report.Error (121, loc, "The call is ambiguous between the following methods or properties: `{0}' and `{1}'",
5292 TypeManager.CSharpSignature (ambiguous), TypeManager.CSharpSignature (method));
5297 // If the method is a virtual function, pick an override closer to the LHS type.
5299 if (!me.IsBase && method.IsVirtual) {
5300 if (TypeManager.IsOverride (method))
5301 throw new InternalErrorException (
5302 "Should not happen. An 'override' method took part in overload resolution: " + method);
5304 if (candidate_overrides != null)
5305 foreach (MethodBase candidate in candidate_overrides) {
5306 if (IsOverride (candidate, method))
5312 // And now check if the arguments are all
5313 // compatible, perform conversions if
5314 // necessary etc. and return if everything is
5317 if (!VerifyArgumentsCompat (ec, Arguments, arg_count, method,
5318 method_params, null, may_fail, loc))
5321 if (method != null) {
5322 MethodBase the_method = method;
5323 if (the_method.Mono_IsInflatedMethod)
5324 the_method = the_method.GetGenericMethodDefinition ();
5325 IMethodData data = TypeManager.GetMethod (the_method);
5327 data.SetMemberIsUsed ();
5332 public static void Error_WrongNumArguments (Location loc, String name, int arg_count)
5334 Report.Error (1501, loc, "No overload for method `{0}' takes `{1}' arguments",
5338 static void Error_InvokeOnDelegate (Location loc)
5340 Report.Error (1533, loc,
5341 "Invoke cannot be called directly on a delegate");
5344 static void Error_InvalidArguments (Location loc, int idx, MethodBase method,
5345 Type delegate_type, Argument a, ParameterData expected_par)
5347 if (delegate_type == null)
5348 Report.Error (1502, loc, "The best overloaded method match for `{0}' has some invalid arguments",
5349 TypeManager.CSharpSignature (method));
5351 Report.Error (1594, loc, "Delegate `{0}' has some invalid arguments",
5352 TypeManager.CSharpName (delegate_type));
5354 string par_desc = expected_par.ParameterDesc (idx);
5356 if (a.Modifier != expected_par.ParameterModifier (idx)) {
5357 if ((expected_par.ParameterModifier (idx) & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) == 0)
5358 Report.Error (1615, loc, "Argument `{0}' should not be passed with the `{1}' keyword",
5359 idx + 1, Parameter.GetModifierSignature (a.Modifier));
5361 Report.Error (1620, loc, "Argument `{0}' must be passed with the `{1}' keyword",
5362 idx + 1, Parameter.GetModifierSignature (expected_par.ParameterModifier (idx)));
5366 Report.Error (1503, loc,
5367 String.Format ("Argument {0}: Cannot convert from `{1}' to `{2}'",
5368 idx + 1, Argument.FullDesc (a), par_desc));
5371 public static bool VerifyArgumentsCompat (EmitContext ec, ArrayList Arguments,
5372 int arg_count, MethodBase method,
5373 bool chose_params_expanded,
5374 Type delegate_type, bool may_fail,
5377 ParameterData pd = TypeManager.GetParameterData (method);
5378 int pd_count = pd.Count;
5380 for (int j = 0; j < arg_count; j++) {
5381 Argument a = (Argument) Arguments [j];
5382 Expression a_expr = a.Expr;
5383 Type parameter_type = pd.ParameterType (j);
5384 Parameter.Modifier pm = pd.ParameterModifier (j);
5386 if (pm == Parameter.Modifier.PARAMS){
5387 if ((pm & ~Parameter.Modifier.PARAMS) != a.Modifier) {
5389 Error_InvalidArguments (
5390 loc, j, method, delegate_type,
5395 if (chose_params_expanded)
5396 parameter_type = TypeManager.GetElementType (parameter_type);
5397 } else if (pm == Parameter.Modifier.ARGLIST){
5403 if (pd.ParameterModifier (j) != a.Modifier){
5405 Error_InvalidArguments (
5406 loc, j, method, delegate_type,
5415 if (!TypeManager.IsEqual (a.Type, parameter_type)){
5418 conv = Convert.ImplicitConversion (ec, a_expr, parameter_type, loc);
5422 Error_InvalidArguments (loc, j, method, delegate_type, a, pd);
5427 // Update the argument with the implicit conversion
5433 if (parameter_type.IsPointer){
5440 Parameter.Modifier a_mod = a.Modifier &
5441 unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF));
5442 Parameter.Modifier p_mod = pd.ParameterModifier (j) &
5443 unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF));
5445 if (a_mod != p_mod &&
5446 pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS) {
5448 Invocation.Error_InvalidArguments (loc, j, method, null, a, pd);
5458 public override Expression DoResolve (EmitContext ec)
5461 // First, resolve the expression that is used to
5462 // trigger the invocation
5464 SimpleName sn = expr as SimpleName;
5466 expr = sn.GetMethodGroup ();
5468 expr = expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
5472 if (!(expr is MethodGroupExpr)) {
5473 Type expr_type = expr.Type;
5475 if (expr_type != null){
5476 bool IsDelegate = TypeManager.IsDelegateType (expr_type);
5478 return (new DelegateInvocation (
5479 this.expr, Arguments, loc)).Resolve (ec);
5483 if (!(expr is MethodGroupExpr)){
5484 expr.Error_UnexpectedKind (ResolveFlags.MethodGroup, loc);
5489 // Next, evaluate all the expressions in the argument list
5491 if (Arguments != null){
5492 foreach (Argument a in Arguments){
5493 if (!a.Resolve (ec, loc))
5498 MethodGroupExpr mg = (MethodGroupExpr) expr;
5499 method = OverloadResolve (ec, mg, Arguments, false, loc);
5504 MethodInfo mi = method as MethodInfo;
5506 type = TypeManager.TypeToCoreType (mi.ReturnType);
5507 Expression iexpr = mg.InstanceExpression;
5509 if (iexpr == null ||
5510 iexpr is This || iexpr is EmptyExpression ||
5511 mg.IdenticalTypeName) {
5512 mg.InstanceExpression = null;
5514 MemberExpr.error176 (loc, TypeManager.CSharpSignature (mi));
5518 if (iexpr == null || iexpr is EmptyExpression) {
5519 SimpleName.Error_ObjectRefRequired (ec, loc, TypeManager.CSharpSignature (mi));
5525 if (type.IsPointer){
5533 // Only base will allow this invocation to happen.
5535 if (mg.IsBase && method.IsAbstract){
5536 Error_CannotCallAbstractBase (TypeManager.CSharpSignature (method));
5540 if (Arguments == null && method.Name == "Finalize") {
5542 Report.Error (250, loc, "Do not directly call your base class Finalize method. It is called automatically from your destructor");
5544 Report.Error (245, loc, "Destructors and object.Finalize cannot be called directly. Consider calling IDisposable.Dispose if available");
5548 if ((method.Attributes & MethodAttributes.SpecialName) != 0 && IsSpecialMethodInvocation (method)) {
5552 if (mg.InstanceExpression != null)
5553 mg.InstanceExpression.CheckMarshallByRefAccess (ec.ContainerType);
5555 eclass = ExprClass.Value;
5559 bool IsSpecialMethodInvocation (MethodBase method)
5561 IMethodData md = TypeManager.GetMethod (method);
5563 if (!(md is AbstractPropertyEventMethod) && !(md is Operator))
5566 if (!TypeManager.IsSpecialMethod (method))
5569 int args = TypeManager.GetParameterData (method).Count;
5570 if (method.Name.StartsWith ("get_") && args > 0)
5572 else if (method.Name.StartsWith ("set_") && args > 2)
5575 // TODO: check operators and events as well ?
5578 Report.SymbolRelatedToPreviousError (method);
5579 Report.Error (571, loc, "`{0}': cannot explicitly call operator or accessor",
5580 TypeManager.CSharpSignature (method, true));
5586 // Emits the list of arguments as an array
5588 static void EmitParams (EmitContext ec, int idx, ArrayList arguments)
5590 ILGenerator ig = ec.ig;
5591 int count = arguments.Count - idx;
5592 Argument a = (Argument) arguments [idx];
5593 Type t = a.Expr.Type;
5595 IntConstant.EmitInt (ig, count);
5596 ig.Emit (OpCodes.Newarr, TypeManager.TypeToCoreType (t));
5598 int top = arguments.Count;
5599 for (int j = idx; j < top; j++){
5600 a = (Argument) arguments [j];
5602 ig.Emit (OpCodes.Dup);
5603 IntConstant.EmitInt (ig, j - idx);
5605 bool is_stobj, has_type_arg;
5606 OpCode op = ArrayAccess.GetStoreOpcode (t, out is_stobj, out has_type_arg);
5608 ig.Emit (OpCodes.Ldelema, t);
5620 /// Emits a list of resolved Arguments that are in the arguments
5623 /// The MethodBase argument might be null if the
5624 /// emission of the arguments is known not to contain
5625 /// a `params' field (for example in constructors or other routines
5626 /// that keep their arguments in this structure)
5628 /// if `dup_args' is true, a copy of the arguments will be left
5629 /// on the stack. If `dup_args' is true, you can specify `this_arg'
5630 /// which will be duplicated before any other args. Only EmitCall
5631 /// should be using this interface.
5633 public static void EmitArguments (EmitContext ec, MethodBase mb, ArrayList arguments, bool dup_args, LocalTemporary this_arg)
5637 pd = TypeManager.GetParameterData (mb);
5641 LocalTemporary [] temps = null;
5644 temps = new LocalTemporary [arguments.Count];
5647 // If we are calling a params method with no arguments, special case it
5649 if (arguments == null){
5650 if (pd != null && pd.Count > 0 &&
5651 pd.ParameterModifier (0) == Parameter.Modifier.PARAMS){
5652 ILGenerator ig = ec.ig;
5654 IntConstant.EmitInt (ig, 0);
5655 ig.Emit (OpCodes.Newarr, TypeManager.GetElementType (pd.ParameterType (0)));
5661 int top = arguments.Count;
5663 for (int i = 0; i < top; i++){
5664 Argument a = (Argument) arguments [i];
5667 if (pd.ParameterModifier (i) == Parameter.Modifier.PARAMS){
5669 // Special case if we are passing the same data as the
5670 // params argument, do not put it in an array.
5672 if (pd.ParameterType (i) == a.Type)
5675 EmitParams (ec, i, arguments);
5682 ec.ig.Emit (OpCodes.Dup);
5683 (temps [i] = new LocalTemporary (ec, a.Type)).Store (ec);
5688 if (this_arg != null)
5691 for (int i = 0; i < top; i ++)
5692 temps [i].Emit (ec);
5695 if (pd != null && pd.Count > top &&
5696 pd.ParameterModifier (top) == Parameter.Modifier.PARAMS){
5697 ILGenerator ig = ec.ig;
5699 IntConstant.EmitInt (ig, 0);
5700 ig.Emit (OpCodes.Newarr, TypeManager.GetElementType (pd.ParameterType (top)));
5704 static Type[] GetVarargsTypes (EmitContext ec, MethodBase mb,
5705 ArrayList arguments)
5707 ParameterData pd = TypeManager.GetParameterData (mb);
5709 if (arguments == null)
5710 return new Type [0];
5712 Argument a = (Argument) arguments [pd.Count - 1];
5713 Arglist list = (Arglist) a.Expr;
5715 return list.ArgumentTypes;
5719 /// This checks the ConditionalAttribute on the method
5721 static bool IsMethodExcluded (MethodBase method, EmitContext ec)
5723 if (method.IsConstructor)
5726 IMethodData md = TypeManager.GetMethod (method);
5728 return md.IsExcluded (ec);
5730 // For some methods (generated by delegate class) GetMethod returns null
5731 // because they are not included in builder_to_method table
5732 if (method.DeclaringType is TypeBuilder)
5735 return AttributeTester.IsConditionalMethodExcluded (method);
5739 /// is_base tells whether we want to force the use of the `call'
5740 /// opcode instead of using callvirt. Call is required to call
5741 /// a specific method, while callvirt will always use the most
5742 /// recent method in the vtable.
5744 /// is_static tells whether this is an invocation on a static method
5746 /// instance_expr is an expression that represents the instance
5747 /// it must be non-null if is_static is false.
5749 /// method is the method to invoke.
5751 /// Arguments is the list of arguments to pass to the method or constructor.
5753 public static void EmitCall (EmitContext ec, bool is_base,
5754 bool is_static, Expression instance_expr,
5755 MethodBase method, ArrayList Arguments, Location loc)
5757 EmitCall (ec, is_base, is_static, instance_expr, method, Arguments, loc, false, false);
5760 // `dup_args' leaves an extra copy of the arguments on the stack
5761 // `omit_args' does not leave any arguments at all.
5762 // So, basically, you could make one call with `dup_args' set to true,
5763 // and then another with `omit_args' set to true, and the two calls
5764 // would have the same set of arguments. However, each argument would
5765 // only have been evaluated once.
5766 public static void EmitCall (EmitContext ec, bool is_base,
5767 bool is_static, Expression instance_expr,
5768 MethodBase method, ArrayList Arguments, Location loc,
5769 bool dup_args, bool omit_args)
5771 ILGenerator ig = ec.ig;
5772 bool struct_call = false;
5773 bool this_call = false;
5774 LocalTemporary this_arg = null;
5776 Type decl_type = method.DeclaringType;
5778 if (!RootContext.StdLib) {
5779 // Replace any calls to the system's System.Array type with calls to
5780 // the newly created one.
5781 if (method == TypeManager.system_int_array_get_length)
5782 method = TypeManager.int_array_get_length;
5783 else if (method == TypeManager.system_int_array_get_rank)
5784 method = TypeManager.int_array_get_rank;
5785 else if (method == TypeManager.system_object_array_clone)
5786 method = TypeManager.object_array_clone;
5787 else if (method == TypeManager.system_int_array_get_length_int)
5788 method = TypeManager.int_array_get_length_int;
5789 else if (method == TypeManager.system_int_array_get_lower_bound_int)
5790 method = TypeManager.int_array_get_lower_bound_int;
5791 else if (method == TypeManager.system_int_array_get_upper_bound_int)
5792 method = TypeManager.int_array_get_upper_bound_int;
5793 else if (method == TypeManager.system_void_array_copyto_array_int)
5794 method = TypeManager.void_array_copyto_array_int;
5797 if (ec.TestObsoleteMethodUsage) {
5799 // This checks ObsoleteAttribute on the method and on the declaring type
5801 ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (method);
5803 AttributeTester.Report_ObsoleteMessage (oa, TypeManager.CSharpSignature (method), loc);
5805 oa = AttributeTester.GetObsoleteAttribute (method.DeclaringType);
5807 AttributeTester.Report_ObsoleteMessage (oa, method.DeclaringType.FullName, loc);
5811 if (IsMethodExcluded (method, ec))
5815 if (instance_expr == EmptyExpression.Null) {
5816 SimpleName.Error_ObjectRefRequired (ec, loc, TypeManager.CSharpSignature (method));
5820 this_call = instance_expr is This;
5821 if (decl_type.IsValueType || (!this_call && instance_expr.Type.IsValueType))
5825 // If this is ourselves, push "this"
5830 ig.Emit (OpCodes.Ldarg_0);
5833 Type iexpr_type = instance_expr.Type;
5836 // Push the instance expression
5838 if (TypeManager.IsValueType (iexpr_type)) {
5840 // Special case: calls to a function declared in a
5841 // reference-type with a value-type argument need
5842 // to have their value boxed.
5843 if (decl_type.IsValueType ||
5844 iexpr_type.IsGenericParameter) {
5846 // If the expression implements IMemoryLocation, then
5847 // we can optimize and use AddressOf on the
5850 // If not we have to use some temporary storage for
5852 if (instance_expr is IMemoryLocation) {
5853 ((IMemoryLocation)instance_expr).
5854 AddressOf (ec, AddressOp.LoadStore);
5856 LocalTemporary temp = new LocalTemporary (ec, iexpr_type);
5857 instance_expr.Emit (ec);
5859 temp.AddressOf (ec, AddressOp.Load);
5862 // avoid the overhead of doing this all the time.
5864 t = TypeManager.GetReferenceType (iexpr_type);
5866 instance_expr.Emit (ec);
5867 ig.Emit (OpCodes.Box, instance_expr.Type);
5868 t = TypeManager.object_type;
5871 instance_expr.Emit (ec);
5872 t = instance_expr.Type;
5877 this_arg = new LocalTemporary (ec, t);
5878 ig.Emit (OpCodes.Dup);
5879 this_arg.Store (ec);
5885 EmitArguments (ec, method, Arguments, dup_args, this_arg);
5887 if ((instance_expr != null) && (instance_expr.Type.IsGenericParameter))
5888 ig.Emit (OpCodes.Constrained, instance_expr.Type);
5891 if (is_static || struct_call || is_base || (this_call && !method.IsVirtual))
5892 call_op = OpCodes.Call;
5894 call_op = OpCodes.Callvirt;
5896 if ((method.CallingConvention & CallingConventions.VarArgs) != 0) {
5897 Type[] varargs_types = GetVarargsTypes (ec, method, Arguments);
5898 ig.EmitCall (call_op, (MethodInfo) method, varargs_types);
5905 // and DoFoo is not virtual, you can omit the callvirt,
5906 // because you don't need the null checking behavior.
5908 if (method is MethodInfo)
5909 ig.Emit (call_op, (MethodInfo) method);
5911 ig.Emit (call_op, (ConstructorInfo) method);
5914 public override void Emit (EmitContext ec)
5916 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
5918 EmitCall (ec, mg.IsBase, method.IsStatic, mg.InstanceExpression, method, Arguments, loc);
5921 public override void EmitStatement (EmitContext ec)
5926 // Pop the return value if there is one
5928 if (method is MethodInfo){
5929 Type ret = ((MethodInfo)method).ReturnType;
5930 if (TypeManager.TypeToCoreType (ret) != TypeManager.void_type)
5931 ec.ig.Emit (OpCodes.Pop);
5936 public class InvocationOrCast : ExpressionStatement
5939 Expression argument;
5941 public InvocationOrCast (Expression expr, Expression argument)
5944 this.argument = argument;
5945 this.loc = expr.Location;
5948 public override Expression DoResolve (EmitContext ec)
5951 // First try to resolve it as a cast.
5953 TypeExpr te = expr.ResolveAsTypeStep (ec) as TypeExpr;
5954 if ((te != null) && (te.eclass == ExprClass.Type)) {
5955 Cast cast = new Cast (te, argument, loc);
5956 return cast.Resolve (ec);
5960 // This can either be a type or a delegate invocation.
5961 // Let's just resolve it and see what we'll get.
5963 expr = expr.Resolve (ec, ResolveFlags.Type | ResolveFlags.VariableOrValue);
5968 // Ok, so it's a Cast.
5970 if (expr.eclass == ExprClass.Type) {
5971 Cast cast = new Cast (new TypeExpression (expr.Type, loc), argument, loc);
5972 return cast.Resolve (ec);
5976 // It's a delegate invocation.
5978 if (!TypeManager.IsDelegateType (expr.Type)) {
5979 Error (149, "Method name expected");
5983 ArrayList args = new ArrayList ();
5984 args.Add (new Argument (argument, Argument.AType.Expression));
5985 DelegateInvocation invocation = new DelegateInvocation (expr, args, loc);
5986 return invocation.Resolve (ec);
5991 Error (201, "Only assignment, call, increment, decrement and new object " +
5992 "expressions can be used as a statement");
5995 public override ExpressionStatement ResolveStatement (EmitContext ec)
5998 // First try to resolve it as a cast.
6000 TypeExpr te = expr.ResolveAsTypeStep (ec) as TypeExpr;
6001 if ((te != null) && (te.eclass == ExprClass.Type)) {
6007 // This can either be a type or a delegate invocation.
6008 // Let's just resolve it and see what we'll get.
6010 expr = expr.Resolve (ec, ResolveFlags.Type | ResolveFlags.VariableOrValue);
6011 if ((expr == null) || (expr.eclass == ExprClass.Type)) {
6017 // It's a delegate invocation.
6019 if (!TypeManager.IsDelegateType (expr.Type)) {
6020 Error (149, "Method name expected");
6024 ArrayList args = new ArrayList ();
6025 args.Add (new Argument (argument, Argument.AType.Expression));
6026 DelegateInvocation invocation = new DelegateInvocation (expr, args, loc);
6027 return invocation.ResolveStatement (ec);
6030 public override void Emit (EmitContext ec)
6032 throw new Exception ("Cannot happen");
6035 public override void EmitStatement (EmitContext ec)
6037 throw new Exception ("Cannot happen");
6042 // This class is used to "disable" the code generation for the
6043 // temporary variable when initializing value types.
6045 class EmptyAddressOf : EmptyExpression, IMemoryLocation {
6046 public void AddressOf (EmitContext ec, AddressOp Mode)
6053 /// Implements the new expression
6055 public class New : ExpressionStatement, IMemoryLocation {
6056 public readonly ArrayList Arguments;
6059 // During bootstrap, it contains the RequestedType,
6060 // but if `type' is not null, it *might* contain a NewDelegate
6061 // (because of field multi-initialization)
6063 public Expression RequestedType;
6065 MethodBase method = null;
6068 // If set, the new expression is for a value_target, and
6069 // we will not leave anything on the stack.
6071 Expression value_target;
6072 bool value_target_set = false;
6073 bool is_type_parameter = false;
6075 public New (Expression requested_type, ArrayList arguments, Location l)
6077 RequestedType = requested_type;
6078 Arguments = arguments;
6082 public bool SetValueTypeVariable (Expression value)
6084 value_target = value;
6085 value_target_set = true;
6086 if (!(value_target is IMemoryLocation)){
6087 Error_UnexpectedKind (null, "variable", loc);
6094 // This function is used to disable the following code sequence for
6095 // value type initialization:
6097 // AddressOf (temporary)
6101 // Instead the provide will have provided us with the address on the
6102 // stack to store the results.
6104 static Expression MyEmptyExpression;
6106 public void DisableTemporaryValueType ()
6108 if (MyEmptyExpression == null)
6109 MyEmptyExpression = new EmptyAddressOf ();
6112 // To enable this, look into:
6113 // test-34 and test-89 and self bootstrapping.
6115 // For instance, we can avoid a copy by using `newobj'
6116 // instead of Call + Push-temp on value types.
6117 // value_target = MyEmptyExpression;
6122 /// Converts complex core type syntax like 'new int ()' to simple constant
6124 public static Constant Constantify (Type t)
6126 if (t == TypeManager.int32_type)
6127 return new IntConstant (0);
6128 if (t == TypeManager.uint32_type)
6129 return new UIntConstant (0);
6130 if (t == TypeManager.int64_type)
6131 return new LongConstant (0);
6132 if (t == TypeManager.uint64_type)
6133 return new ULongConstant (0);
6134 if (t == TypeManager.float_type)
6135 return new FloatConstant (0);
6136 if (t == TypeManager.double_type)
6137 return new DoubleConstant (0);
6138 if (t == TypeManager.short_type)
6139 return new ShortConstant (0);
6140 if (t == TypeManager.ushort_type)
6141 return new UShortConstant (0);
6142 if (t == TypeManager.sbyte_type)
6143 return new SByteConstant (0);
6144 if (t == TypeManager.byte_type)
6145 return new ByteConstant (0);
6146 if (t == TypeManager.char_type)
6147 return new CharConstant ('\0');
6148 if (t == TypeManager.bool_type)
6149 return new BoolConstant (false);
6150 if (t == TypeManager.decimal_type)
6151 return new DecimalConstant (0);
6156 public override Expression DoResolve (EmitContext ec)
6159 // The New DoResolve might be called twice when initializing field
6160 // expressions (see EmitFieldInitializers, the call to
6161 // GetInitializerExpression will perform a resolve on the expression,
6162 // and later the assign will trigger another resolution
6164 // This leads to bugs (#37014)
6167 if (RequestedType is NewDelegate)
6168 return RequestedType;
6172 TypeExpr texpr = RequestedType.ResolveAsTypeTerminal (ec);
6176 if (Arguments == null) {
6177 Expression c = Constantify (type);
6186 CheckObsoleteAttribute (type);
6188 if (TypeManager.IsDelegateType (type)) {
6189 RequestedType = (new NewDelegate (type, Arguments, loc)).Resolve (ec);
6190 if (RequestedType != null)
6191 if (!(RequestedType is DelegateCreation))
6192 throw new Exception ("NewDelegate.Resolve returned a non NewDelegate: " + RequestedType.GetType ());
6193 return RequestedType;
6196 if (type.IsGenericParameter) {
6197 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (type);
6199 if ((gc == null) || (!gc.HasConstructorConstraint && !gc.IsValueType)) {
6200 Error (304, String.Format (
6201 "Cannot create an instance of the " +
6202 "variable type '{0}' because it " +
6203 "doesn't have the new() constraint",
6208 if ((Arguments != null) && (Arguments.Count != 0)) {
6209 Error (417, String.Format (
6210 "`{0}': cannot provide arguments " +
6211 "when creating an instance of a " +
6212 "variable type.", type));
6216 is_type_parameter = true;
6217 eclass = ExprClass.Value;
6221 if (type.IsAbstract && type.IsSealed) {
6222 Report.SymbolRelatedToPreviousError (type);
6223 Report.Error (712, loc, "Cannot create an instance of the static class `{0}'", TypeManager.CSharpName (type));
6227 if (type.IsInterface || type.IsAbstract){
6228 Report.SymbolRelatedToPreviousError (type);
6229 Report.Error (144, loc, "Cannot create an instance of the abstract class or interface `{0}'", TypeManager.CSharpName (type));
6233 bool is_struct = type.IsValueType;
6234 eclass = ExprClass.Value;
6237 // SRE returns a match for .ctor () on structs (the object constructor),
6238 // so we have to manually ignore it.
6240 if (is_struct && Arguments == null)
6243 Expression ml = MemberLookupFinal (ec, type, type, ".ctor",
6244 MemberTypes.Constructor, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
6249 MethodGroupExpr mg = ml as MethodGroupExpr;
6252 ml.Error_UnexpectedKind (ec, "method group", loc);
6256 if (Arguments != null){
6257 foreach (Argument a in Arguments){
6258 if (!a.Resolve (ec, loc))
6263 method = Invocation.OverloadResolve (ec, mg, Arguments, false, loc);
6264 if (method == null) {
6265 if (almostMatchedMembers.Count != 0)
6266 MemberLookupFailed (ec, type, type, ".ctor", null, true, loc);
6273 bool DoEmitTypeParameter (EmitContext ec)
6275 ILGenerator ig = ec.ig;
6277 ig.Emit (OpCodes.Ldtoken, type);
6278 ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
6279 ig.Emit (OpCodes.Call, TypeManager.activator_create_instance);
6280 ig.Emit (OpCodes.Unbox_Any, type);
6286 // This DoEmit can be invoked in two contexts:
6287 // * As a mechanism that will leave a value on the stack (new object)
6288 // * As one that wont (init struct)
6290 // You can control whether a value is required on the stack by passing
6291 // need_value_on_stack. The code *might* leave a value on the stack
6292 // so it must be popped manually
6294 // If we are dealing with a ValueType, we have a few
6295 // situations to deal with:
6297 // * The target is a ValueType, and we have been provided
6298 // the instance (this is easy, we are being assigned).
6300 // * The target of New is being passed as an argument,
6301 // to a boxing operation or a function that takes a
6304 // In this case, we need to create a temporary variable
6305 // that is the argument of New.
6307 // Returns whether a value is left on the stack
6309 bool DoEmit (EmitContext ec, bool need_value_on_stack)
6311 bool is_value_type = TypeManager.IsValueType (type);
6312 ILGenerator ig = ec.ig;
6317 // Allow DoEmit() to be called multiple times.
6318 // We need to create a new LocalTemporary each time since
6319 // you can't share LocalBuilders among ILGeneators.
6320 if (!value_target_set)
6321 value_target = new LocalTemporary (ec, type);
6323 ml = (IMemoryLocation) value_target;
6324 ml.AddressOf (ec, AddressOp.Store);
6328 Invocation.EmitArguments (ec, method, Arguments, false, null);
6332 ig.Emit (OpCodes.Initobj, type);
6334 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
6335 if (need_value_on_stack){
6336 value_target.Emit (ec);
6341 ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
6346 public override void Emit (EmitContext ec)
6348 if (is_type_parameter)
6349 DoEmitTypeParameter (ec);
6354 public override void EmitStatement (EmitContext ec)
6356 if (is_type_parameter)
6357 throw new InvalidOperationException ();
6359 if (DoEmit (ec, false))
6360 ec.ig.Emit (OpCodes.Pop);
6363 public void AddressOf (EmitContext ec, AddressOp Mode)
6365 if (is_type_parameter)
6366 throw new InvalidOperationException ();
6368 if (!type.IsValueType){
6370 // We throw an exception. So far, I believe we only need to support
6372 // foreach (int j in new StructType ())
6375 throw new Exception ("AddressOf should not be used for classes");
6378 if (!value_target_set)
6379 value_target = new LocalTemporary (ec, type);
6381 IMemoryLocation ml = (IMemoryLocation) value_target;
6382 ml.AddressOf (ec, AddressOp.Store);
6384 Invocation.EmitArguments (ec, method, Arguments, false, null);
6387 ec.ig.Emit (OpCodes.Initobj, type);
6389 ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
6391 ((IMemoryLocation) value_target).AddressOf (ec, Mode);
6396 /// 14.5.10.2: Represents an array creation expression.
6400 /// There are two possible scenarios here: one is an array creation
6401 /// expression that specifies the dimensions and optionally the
6402 /// initialization data and the other which does not need dimensions
6403 /// specified but where initialization data is mandatory.
6405 public class ArrayCreation : Expression {
6406 Expression requested_base_type;
6407 ArrayList initializers;
6410 // The list of Argument types.
6411 // This is used to construct the `newarray' or constructor signature
6413 ArrayList arguments;
6416 // Method used to create the array object.
6418 MethodBase new_method = null;
6420 Type array_element_type;
6421 Type underlying_type;
6422 bool is_one_dimensional = false;
6423 bool is_builtin_type = false;
6424 bool expect_initializers = false;
6425 int num_arguments = 0;
6429 ArrayList array_data;
6434 // The number of array initializers that we can handle
6435 // via the InitializeArray method - through EmitStaticInitializers
6437 int num_automatic_initializers;
6439 const int max_automatic_initializers = 6;
6441 public ArrayCreation (Expression requested_base_type, ArrayList exprs, string rank, ArrayList initializers, Location l)
6443 this.requested_base_type = requested_base_type;
6444 this.initializers = initializers;
6448 arguments = new ArrayList ();
6450 foreach (Expression e in exprs) {
6451 arguments.Add (new Argument (e, Argument.AType.Expression));
6456 public ArrayCreation (Expression requested_base_type, string rank, ArrayList initializers, Location l)
6458 this.requested_base_type = requested_base_type;
6459 this.initializers = initializers;
6463 //this.rank = rank.Substring (0, rank.LastIndexOf ('['));
6465 //string tmp = rank.Substring (rank.LastIndexOf ('['));
6467 //dimensions = tmp.Length - 1;
6468 expect_initializers = true;
6471 public Expression FormArrayType (Expression base_type, int idx_count, string rank)
6473 StringBuilder sb = new StringBuilder (rank);
6476 for (int i = 1; i < idx_count; i++)
6481 return new ComposedCast (base_type, sb.ToString (), loc);
6484 void Error_IncorrectArrayInitializer ()
6486 Error (178, "Invalid rank specifier: expected `,' or `]'");
6489 public bool CheckIndices (EmitContext ec, ArrayList probe, int idx, bool specified_dims)
6491 if (specified_dims) {
6492 Argument a = (Argument) arguments [idx];
6494 if (!a.Resolve (ec, loc))
6497 if (!(a.Expr is Constant)) {
6498 Error (150, "A constant value is expected");
6502 int value = (int) ((Constant) a.Expr).GetValue ();
6504 if (value != probe.Count) {
6505 Error_IncorrectArrayInitializer ();
6509 bounds [idx] = value;
6512 int child_bounds = -1;
6513 for (int i = 0; i < probe.Count; ++i) {
6514 object o = probe [i];
6515 if (o is ArrayList) {
6516 ArrayList sub_probe = o as ArrayList;
6517 int current_bounds = sub_probe.Count;
6519 if (child_bounds == -1)
6520 child_bounds = current_bounds;
6522 else if (child_bounds != current_bounds){
6523 Error_IncorrectArrayInitializer ();
6526 if (specified_dims && (idx + 1 >= arguments.Count)){
6527 Error (623, "Array initializers can only be used in a variable or field initializer. Try using a new expression instead");
6531 bool ret = CheckIndices (ec, sub_probe, idx + 1, specified_dims);
6535 if (child_bounds != -1){
6536 Error_IncorrectArrayInitializer ();
6540 Expression tmp = (Expression) o;
6541 tmp = tmp.Resolve (ec);
6546 // Console.WriteLine ("I got: " + tmp);
6547 // Handle initialization from vars, fields etc.
6549 Expression conv = Convert.ImplicitConversionRequired (
6550 ec, tmp, underlying_type, loc);
6555 if (conv is StringConstant || conv is DecimalConstant || conv is NullCast) {
6556 // These are subclasses of Constant that can appear as elements of an
6557 // array that cannot be statically initialized (with num_automatic_initializers
6558 // > max_automatic_initializers), so num_automatic_initializers should be left as zero.
6559 array_data.Add (conv);
6560 } else if (conv is Constant) {
6561 // These are the types of Constant that can appear in arrays that can be
6562 // statically allocated.
6563 array_data.Add (conv);
6564 num_automatic_initializers++;
6566 array_data.Add (conv);
6573 public void UpdateIndices (EmitContext ec)
6576 for (ArrayList probe = initializers; probe != null;) {
6577 if (probe.Count > 0 && probe [0] is ArrayList) {
6578 Expression e = new IntConstant (probe.Count);
6579 arguments.Add (new Argument (e, Argument.AType.Expression));
6581 bounds [i++] = probe.Count;
6583 probe = (ArrayList) probe [0];
6586 Expression e = new IntConstant (probe.Count);
6587 arguments.Add (new Argument (e, Argument.AType.Expression));
6589 bounds [i++] = probe.Count;
6596 public bool ValidateInitializers (EmitContext ec, Type array_type)
6598 if (initializers == null) {
6599 if (expect_initializers)
6605 if (underlying_type == null)
6609 // We use this to store all the date values in the order in which we
6610 // will need to store them in the byte blob later
6612 array_data = new ArrayList ();
6613 bounds = new Hashtable ();
6617 if (arguments != null) {
6618 ret = CheckIndices (ec, initializers, 0, true);
6621 arguments = new ArrayList ();
6623 ret = CheckIndices (ec, initializers, 0, false);
6630 if (arguments.Count != dimensions) {
6631 Error_IncorrectArrayInitializer ();
6640 // Creates the type of the array
6642 bool LookupType (EmitContext ec)
6644 StringBuilder array_qualifier = new StringBuilder (rank);
6647 // `In the first form allocates an array instace of the type that results
6648 // from deleting each of the individual expression from the expression list'
6650 if (num_arguments > 0) {
6651 array_qualifier.Append ("[");
6652 for (int i = num_arguments-1; i > 0; i--)
6653 array_qualifier.Append (",");
6654 array_qualifier.Append ("]");
6660 TypeExpr array_type_expr;
6661 array_type_expr = new ComposedCast (requested_base_type, array_qualifier.ToString (), loc);
6662 array_type_expr = array_type_expr.ResolveAsTypeTerminal (ec);
6663 if (array_type_expr == null)
6666 type = array_type_expr.Type;
6668 if (!type.IsArray) {
6669 Error (622, "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
6672 underlying_type = TypeManager.GetElementType (type);
6673 dimensions = type.GetArrayRank ();
6678 public override Expression DoResolve (EmitContext ec)
6682 if (!LookupType (ec))
6686 // First step is to validate the initializers and fill
6687 // in any missing bits
6689 if (!ValidateInitializers (ec, type))
6692 if (arguments == null)
6695 arg_count = arguments.Count;
6696 foreach (Argument a in arguments){
6697 if (!a.Resolve (ec, loc))
6700 Expression real_arg = ExpressionToArrayArgument (ec, a.Expr, loc);
6701 if (real_arg == null)
6708 array_element_type = TypeManager.GetElementType (type);
6710 if (array_element_type.IsAbstract && array_element_type.IsSealed) {
6711 Report.Error (719, loc, "`{0}': array elements cannot be of static type", TypeManager.CSharpName (array_element_type));
6715 if (arg_count == 1) {
6716 is_one_dimensional = true;
6717 eclass = ExprClass.Value;
6721 is_builtin_type = TypeManager.IsBuiltinType (type);
6723 if (is_builtin_type) {
6726 ml = MemberLookup (ec, type, ".ctor", MemberTypes.Constructor,
6727 AllBindingFlags, loc);
6729 if (!(ml is MethodGroupExpr)) {
6730 ml.Error_UnexpectedKind (ec, "method group", loc);
6735 Error (-6, "New invocation: Can not find a constructor for " +
6736 "this argument list");
6740 new_method = Invocation.OverloadResolve (
6741 ec, (MethodGroupExpr) ml, arguments, false, loc);
6743 if (new_method == null) {
6744 Error (-6, "New invocation: Can not find a constructor for " +
6745 "this argument list");
6749 eclass = ExprClass.Value;
6752 ModuleBuilder mb = CodeGen.Module.Builder;
6753 ArrayList args = new ArrayList ();
6755 if (arguments != null) {
6756 for (int i = 0; i < arg_count; i++)
6757 args.Add (TypeManager.int32_type);
6760 Type [] arg_types = null;
6763 arg_types = new Type [args.Count];
6765 args.CopyTo (arg_types, 0);
6767 new_method = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
6770 if (new_method == null) {
6771 Error (-6, "New invocation: Can not find a constructor for " +
6772 "this argument list");
6776 eclass = ExprClass.Value;
6781 public static byte [] MakeByteBlob (ArrayList array_data, Type underlying_type, Location loc)
6786 int count = array_data.Count;
6788 if (underlying_type.IsEnum)
6789 underlying_type = TypeManager.EnumToUnderlying (underlying_type);
6791 factor = GetTypeSize (underlying_type);
6793 throw new Exception ("unrecognized type in MakeByteBlob: " + underlying_type);
6795 data = new byte [(count * factor + 4) & ~3];
6798 for (int i = 0; i < count; ++i) {
6799 object v = array_data [i];
6801 if (v is EnumConstant)
6802 v = ((EnumConstant) v).Child;
6804 if (v is Constant && !(v is StringConstant))
6805 v = ((Constant) v).GetValue ();
6811 if (underlying_type == TypeManager.int64_type){
6812 if (!(v is Expression)){
6813 long val = (long) v;
6815 for (int j = 0; j < factor; ++j) {
6816 data [idx + j] = (byte) (val & 0xFF);
6820 } else if (underlying_type == TypeManager.uint64_type){
6821 if (!(v is Expression)){
6822 ulong val = (ulong) v;
6824 for (int j = 0; j < factor; ++j) {
6825 data [idx + j] = (byte) (val & 0xFF);
6829 } else if (underlying_type == TypeManager.float_type) {
6830 if (!(v is Expression)){
6831 element = BitConverter.GetBytes ((float) v);
6833 for (int j = 0; j < factor; ++j)
6834 data [idx + j] = element [j];
6836 } else if (underlying_type == TypeManager.double_type) {
6837 if (!(v is Expression)){
6838 element = BitConverter.GetBytes ((double) v);
6840 for (int j = 0; j < factor; ++j)
6841 data [idx + j] = element [j];
6843 } else if (underlying_type == TypeManager.char_type){
6844 if (!(v is Expression)){
6845 int val = (int) ((char) v);
6847 data [idx] = (byte) (val & 0xff);
6848 data [idx+1] = (byte) (val >> 8);
6850 } else if (underlying_type == TypeManager.short_type){
6851 if (!(v is Expression)){
6852 int val = (int) ((short) v);
6854 data [idx] = (byte) (val & 0xff);
6855 data [idx+1] = (byte) (val >> 8);
6857 } else if (underlying_type == TypeManager.ushort_type){
6858 if (!(v is Expression)){
6859 int val = (int) ((ushort) v);
6861 data [idx] = (byte) (val & 0xff);
6862 data [idx+1] = (byte) (val >> 8);
6864 } else if (underlying_type == TypeManager.int32_type) {
6865 if (!(v is Expression)){
6868 data [idx] = (byte) (val & 0xff);
6869 data [idx+1] = (byte) ((val >> 8) & 0xff);
6870 data [idx+2] = (byte) ((val >> 16) & 0xff);
6871 data [idx+3] = (byte) (val >> 24);
6873 } else if (underlying_type == TypeManager.uint32_type) {
6874 if (!(v is Expression)){
6875 uint val = (uint) v;
6877 data [idx] = (byte) (val & 0xff);
6878 data [idx+1] = (byte) ((val >> 8) & 0xff);
6879 data [idx+2] = (byte) ((val >> 16) & 0xff);
6880 data [idx+3] = (byte) (val >> 24);
6882 } else if (underlying_type == TypeManager.sbyte_type) {
6883 if (!(v is Expression)){
6884 sbyte val = (sbyte) v;
6885 data [idx] = (byte) val;
6887 } else if (underlying_type == TypeManager.byte_type) {
6888 if (!(v is Expression)){
6889 byte val = (byte) v;
6890 data [idx] = (byte) val;
6892 } else if (underlying_type == TypeManager.bool_type) {
6893 if (!(v is Expression)){
6894 bool val = (bool) v;
6895 data [idx] = (byte) (val ? 1 : 0);
6897 } else if (underlying_type == TypeManager.decimal_type){
6898 if (!(v is Expression)){
6899 int [] bits = Decimal.GetBits ((decimal) v);
6902 // FIXME: For some reason, this doesn't work on the MS runtime.
6903 int [] nbits = new int [4];
6904 nbits [0] = bits [3];
6905 nbits [1] = bits [2];
6906 nbits [2] = bits [0];
6907 nbits [3] = bits [1];
6909 for (int j = 0; j < 4; j++){
6910 data [p++] = (byte) (nbits [j] & 0xff);
6911 data [p++] = (byte) ((nbits [j] >> 8) & 0xff);
6912 data [p++] = (byte) ((nbits [j] >> 16) & 0xff);
6913 data [p++] = (byte) (nbits [j] >> 24);
6917 throw new Exception ("Unrecognized type in MakeByteBlob: " + underlying_type);
6926 // Emits the initializers for the array
6928 void EmitStaticInitializers (EmitContext ec)
6931 // First, the static data
6934 ILGenerator ig = ec.ig;
6936 byte [] data = MakeByteBlob (array_data, underlying_type, loc);
6938 fb = RootContext.MakeStaticData (data);
6940 ig.Emit (OpCodes.Dup);
6941 ig.Emit (OpCodes.Ldtoken, fb);
6942 ig.Emit (OpCodes.Call,
6943 TypeManager.void_initializearray_array_fieldhandle);
6947 // Emits pieces of the array that can not be computed at compile
6948 // time (variables and string locations).
6950 // This always expect the top value on the stack to be the array
6952 void EmitDynamicInitializers (EmitContext ec)
6954 ILGenerator ig = ec.ig;
6955 int dims = bounds.Count;
6956 int [] current_pos = new int [dims];
6957 int top = array_data.Count;
6959 MethodInfo set = null;
6963 ModuleBuilder mb = null;
6964 mb = CodeGen.Module.Builder;
6965 args = new Type [dims + 1];
6968 for (j = 0; j < dims; j++)
6969 args [j] = TypeManager.int32_type;
6971 args [j] = array_element_type;
6973 set = mb.GetArrayMethod (
6975 CallingConventions.HasThis | CallingConventions.Standard,
6976 TypeManager.void_type, args);
6979 for (int i = 0; i < top; i++){
6981 Expression e = null;
6983 if (array_data [i] is Expression)
6984 e = (Expression) array_data [i];
6988 // Basically we do this for string literals and
6989 // other non-literal expressions
6991 if (e is EnumConstant){
6992 e = ((EnumConstant) e).Child;
6995 if (e is StringConstant || e is DecimalConstant || !(e is Constant) ||
6996 num_automatic_initializers <= max_automatic_initializers) {
6997 Type etype = e.Type;
6999 ig.Emit (OpCodes.Dup);
7001 for (int idx = 0; idx < dims; idx++)
7002 IntConstant.EmitInt (ig, current_pos [idx]);
7005 // If we are dealing with a struct, get the
7006 // address of it, so we can store it.
7009 TypeManager.IsValueType (etype) &&
7010 (!TypeManager.IsBuiltinOrEnum (etype) ||
7011 etype == TypeManager.decimal_type)) {
7016 // Let new know that we are providing
7017 // the address where to store the results
7019 n.DisableTemporaryValueType ();
7022 ig.Emit (OpCodes.Ldelema, etype);
7028 bool is_stobj, has_type_arg;
7029 OpCode op = ArrayAccess.GetStoreOpcode (
7030 etype, out is_stobj,
7033 ig.Emit (OpCodes.Stobj, etype);
7034 else if (has_type_arg)
7035 ig.Emit (op, etype);
7039 ig.Emit (OpCodes.Call, set);
7046 for (int j = dims - 1; j >= 0; j--){
7048 if (current_pos [j] < (int) bounds [j])
7050 current_pos [j] = 0;
7055 void EmitArrayArguments (EmitContext ec)
7057 ILGenerator ig = ec.ig;
7059 foreach (Argument a in arguments) {
7060 Type atype = a.Type;
7063 if (atype == TypeManager.uint64_type)
7064 ig.Emit (OpCodes.Conv_Ovf_U4);
7065 else if (atype == TypeManager.int64_type)
7066 ig.Emit (OpCodes.Conv_Ovf_I4);
7070 public override void Emit (EmitContext ec)
7072 ILGenerator ig = ec.ig;
7074 EmitArrayArguments (ec);
7075 if (is_one_dimensional)
7076 ig.Emit (OpCodes.Newarr, array_element_type);
7078 if (is_builtin_type)
7079 ig.Emit (OpCodes.Newobj, (ConstructorInfo) new_method);
7081 ig.Emit (OpCodes.Newobj, (MethodInfo) new_method);
7084 if (initializers != null){
7086 // FIXME: Set this variable correctly.
7088 bool dynamic_initializers = true;
7090 // This will never be true for array types that cannot be statically
7091 // initialized. num_automatic_initializers will always be zero. See
7093 if (num_automatic_initializers > max_automatic_initializers)
7094 EmitStaticInitializers (ec);
7096 if (dynamic_initializers)
7097 EmitDynamicInitializers (ec);
7101 public object EncodeAsAttribute ()
7103 if (!is_one_dimensional){
7104 Report.Error (-211, Location, "attribute can not encode multi-dimensional arrays");
7108 if (array_data == null){
7109 Report.Error (-212, Location, "array should be initialized when passing it to an attribute");
7113 object [] ret = new object [array_data.Count];
7115 foreach (Expression e in array_data){
7118 if (e is NullLiteral)
7121 if (!Attribute.GetAttributeArgumentExpression (e, Location, array_element_type, out v))
7131 /// Represents the `this' construct
7133 public class This : Expression, IAssignMethod, IMemoryLocation, IVariable {
7136 VariableInfo variable_info;
7138 public This (Block block, Location loc)
7144 public This (Location loc)
7149 public VariableInfo VariableInfo {
7150 get { return variable_info; }
7153 public bool VerifyFixed ()
7155 // Treat 'this' as a value parameter for the purpose of fixed variable determination.
7159 public bool ResolveBase (EmitContext ec)
7161 eclass = ExprClass.Variable;
7163 if (ec.TypeContainer.CurrentType != null)
7164 type = ec.TypeContainer.CurrentType;
7166 type = ec.ContainerType;
7169 Error (26, "Keyword `this' is not valid in a static property, static method, or static field initializer");
7173 if (block != null && block.Toplevel.ThisVariable != null)
7174 variable_info = block.Toplevel.ThisVariable.VariableInfo;
7176 if (ec.CurrentAnonymousMethod != null)
7182 public override Expression DoResolve (EmitContext ec)
7184 if (!ResolveBase (ec))
7187 if ((variable_info != null) && !(type.IsValueType && ec.OmitStructFlowAnalysis) && !variable_info.IsAssigned (ec)) {
7188 Error (188, "The `this' object cannot be used before all of its fields are assigned to");
7189 variable_info.SetAssigned (ec);
7193 if (ec.IsFieldInitializer) {
7194 Error (27, "Keyword `this' is not available in the current context");
7201 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
7203 if (!ResolveBase (ec))
7206 if (variable_info != null)
7207 variable_info.SetAssigned (ec);
7209 if (ec.TypeContainer is Class){
7210 Error (1604, "Cannot assign to 'this' because it is read-only");
7217 public void Emit (EmitContext ec, bool leave_copy)
7221 ec.ig.Emit (OpCodes.Dup);
7224 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
7226 ILGenerator ig = ec.ig;
7228 if (ec.TypeContainer is Struct){
7232 ec.ig.Emit (OpCodes.Dup);
7233 ig.Emit (OpCodes.Stobj, type);
7235 throw new Exception ("how did you get here");
7239 public override void Emit (EmitContext ec)
7241 ILGenerator ig = ec.ig;
7244 if (ec.TypeContainer is Struct)
7245 ig.Emit (OpCodes.Ldobj, type);
7248 public override int GetHashCode()
7250 return block.GetHashCode ();
7253 public override bool Equals (object obj)
7255 This t = obj as This;
7259 return block == t.block;
7262 public void AddressOf (EmitContext ec, AddressOp mode)
7267 // FIGURE OUT WHY LDARG_S does not work
7269 // consider: struct X { int val; int P { set { val = value; }}}
7271 // Yes, this looks very bad. Look at `NOTAS' for
7273 // ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
7278 /// Represents the `__arglist' construct
7280 public class ArglistAccess : Expression
7282 public ArglistAccess (Location loc)
7287 public bool ResolveBase (EmitContext ec)
7289 eclass = ExprClass.Variable;
7290 type = TypeManager.runtime_argument_handle_type;
7294 public override Expression DoResolve (EmitContext ec)
7296 if (!ResolveBase (ec))
7299 if (ec.IsFieldInitializer || !ec.CurrentBlock.Toplevel.HasVarargs) {
7300 Error (190, "The __arglist construct is valid only within " +
7301 "a variable argument method.");
7308 public override void Emit (EmitContext ec)
7310 ec.ig.Emit (OpCodes.Arglist);
7315 /// Represents the `__arglist (....)' construct
7317 public class Arglist : Expression
7319 public readonly Argument[] Arguments;
7321 public Arglist (Argument[] args, Location l)
7327 public Type[] ArgumentTypes {
7329 Type[] retval = new Type [Arguments.Length];
7330 for (int i = 0; i < Arguments.Length; i++)
7331 retval [i] = Arguments [i].Type;
7336 public override Expression DoResolve (EmitContext ec)
7338 eclass = ExprClass.Variable;
7339 type = TypeManager.runtime_argument_handle_type;
7341 foreach (Argument arg in Arguments) {
7342 if (!arg.Resolve (ec, loc))
7349 public override void Emit (EmitContext ec)
7351 foreach (Argument arg in Arguments)
7357 // This produces the value that renders an instance, used by the iterators code
7359 public class ProxyInstance : Expression, IMemoryLocation {
7360 public override Expression DoResolve (EmitContext ec)
7362 eclass = ExprClass.Variable;
7363 type = ec.ContainerType;
7367 public override void Emit (EmitContext ec)
7369 ec.ig.Emit (OpCodes.Ldarg_0);
7373 public void AddressOf (EmitContext ec, AddressOp mode)
7375 ec.ig.Emit (OpCodes.Ldarg_0);
7380 /// Implements the typeof operator
7382 public class TypeOf : Expression {
7383 public Expression QueriedType;
7384 protected Type typearg;
7386 public TypeOf (Expression queried_type, Location l)
7388 QueriedType = queried_type;
7392 public override Expression DoResolve (EmitContext ec)
7394 TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec);
7398 typearg = texpr.Type;
7400 if (typearg == TypeManager.void_type) {
7401 Error (673, "System.Void cannot be used from C#. Use typeof (void) to get the void type object");
7405 if (typearg.IsPointer && !ec.InUnsafe){
7409 CheckObsoleteAttribute (typearg);
7411 type = TypeManager.type_type;
7412 // Even though what is returned is a type object, it's treated as a value by the compiler.
7413 // In particular, 'typeof (Foo).X' is something totally different from 'Foo.X'.
7414 eclass = ExprClass.Value;
7418 public override void Emit (EmitContext ec)
7420 ec.ig.Emit (OpCodes.Ldtoken, typearg);
7421 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
7424 public Type TypeArg {
7425 get { return typearg; }
7430 /// Implements the `typeof (void)' operator
7432 public class TypeOfVoid : TypeOf {
7433 public TypeOfVoid (Location l) : base (null, l)
7438 public override Expression DoResolve (EmitContext ec)
7440 type = TypeManager.type_type;
7441 typearg = TypeManager.void_type;
7442 // See description in TypeOf.
7443 eclass = ExprClass.Value;
7449 /// Implements the sizeof expression
7451 public class SizeOf : Expression {
7452 public Expression QueriedType;
7455 public SizeOf (Expression queried_type, Location l)
7457 this.QueriedType = queried_type;
7461 public override Expression DoResolve (EmitContext ec)
7463 TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec);
7467 if (texpr is TypeParameterExpr){
7468 ((TypeParameterExpr)texpr).Error_CannotUseAsUnmanagedType (loc);
7472 type_queried = texpr.Type;
7474 int size_of = GetTypeSize (type_queried);
7476 return new IntConstant (size_of);
7480 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)",
7481 TypeManager.CSharpName (type_queried));
7485 CheckObsoleteAttribute (type_queried);
7487 if (!TypeManager.VerifyUnManaged (type_queried, loc)){
7491 type = TypeManager.int32_type;
7492 eclass = ExprClass.Value;
7496 public override void Emit (EmitContext ec)
7498 int size = GetTypeSize (type_queried);
7501 ec.ig.Emit (OpCodes.Sizeof, type_queried);
7503 IntConstant.EmitInt (ec.ig, size);
7508 /// Implements the qualified-alias-member (::) expression.
7510 public class QualifiedAliasMember : Expression
7512 string alias, identifier;
7514 public QualifiedAliasMember (string alias, string identifier, Location l)
7517 this.identifier = identifier;
7521 public override FullNamedExpression ResolveAsTypeStep (EmitContext ec, bool silent)
7523 if (alias == "global")
7524 return new MemberAccess (Namespace.Root, identifier, loc).ResolveAsTypeStep (ec, silent);
7526 int errors = Report.Errors;
7527 FullNamedExpression fne = ec.DeclSpace.NamespaceEntry.LookupAlias (alias);
7529 if (errors == Report.Errors)
7530 Report.Error (432, loc, "Alias `{0}' not found", alias);
7533 if (fne.eclass != ExprClass.Namespace) {
7535 Report.Error (431, loc, "`{0}' cannot be used with '::' since it denotes a type", alias);
7538 return new MemberAccess (fne, identifier, loc).ResolveAsTypeStep (ec, silent);
7541 public override Expression DoResolve (EmitContext ec)
7543 FullNamedExpression fne;
7544 if (alias == "global") {
7545 fne = Namespace.Root;
7547 int errors = Report.Errors;
7548 fne = ec.DeclSpace.NamespaceEntry.LookupAlias (alias);
7550 if (errors == Report.Errors)
7551 Report.Error (432, loc, "Alias `{0}' not found", alias);
7556 Expression retval = new MemberAccess (fne, identifier, loc).DoResolve (ec);
7560 if (!(retval is FullNamedExpression)) {
7561 Report.Error (687, loc, "The expression `{0}::{1}' did not resolve to a namespace or a type", alias, identifier);
7565 // We defer this check till the end to match the behaviour of CSC
7566 if (fne.eclass != ExprClass.Namespace) {
7567 Report.Error (431, loc, "`{0}' cannot be used with '::' since it denotes a type", alias);
7573 public override void Emit (EmitContext ec)
7575 throw new InternalErrorException ("QualifiedAliasMember found in resolved tree");
7579 public override string ToString ()
7581 return alias + "::" + identifier;
7584 public override string GetSignatureForError ()
7591 /// Implements the member access expression
7593 public class MemberAccess : Expression {
7594 public readonly string Identifier; // TODO: LocatedToken
7598 public MemberAccess (Expression expr, string id, Location l)
7605 public MemberAccess (Expression expr, string id, TypeArguments args,
7607 : this (expr, id, l)
7612 public Expression Expr {
7613 get { return expr; }
7616 // TODO: this method has very poor performace for Enum fields and
7617 // probably for other constants as well
7618 Expression DoResolve (EmitContext ec, Expression right_side)
7621 throw new Exception ();
7624 // Resolve the expression with flow analysis turned off, we'll do the definite
7625 // assignment checks later. This is because we don't know yet what the expression
7626 // will resolve to - it may resolve to a FieldExpr and in this case we must do the
7627 // definite assignment check on the actual field and not on the whole struct.
7630 SimpleName original = expr as SimpleName;
7631 Expression new_expr = expr.Resolve (ec,
7632 ResolveFlags.VariableOrValue | ResolveFlags.Type |
7633 ResolveFlags.Intermediate | ResolveFlags.DisableStructFlowAnalysis);
7635 if (new_expr == null)
7638 if (new_expr is Namespace) {
7639 Namespace ns = (Namespace) new_expr;
7640 string lookup_id = MemberName.MakeName (Identifier, args);
7641 FullNamedExpression retval = ns.Lookup (ec.DeclSpace, lookup_id, loc);
7642 if ((retval != null) && (args != null))
7643 retval = new ConstructedType (retval, args, loc).ResolveAsTypeStep (ec);
7645 Report.Error (234, loc, "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing an assembly reference?",
7646 Identifier, ns.FullName);
7650 Type expr_type = new_expr.Type;
7651 if (expr_type.IsPointer){
7652 Error (23, "The `.' operator can not be applied to pointer operands (" +
7653 TypeManager.CSharpName (expr_type) + ")");
7657 Expression member_lookup;
7658 member_lookup = MemberLookup (
7659 ec, expr_type, expr_type, Identifier, loc);
7660 if ((member_lookup == null) && (args != null)) {
7661 string lookup_id = MemberName.MakeName (Identifier, args);
7662 member_lookup = MemberLookup (
7663 ec, expr_type, expr_type, lookup_id, loc);
7665 if (member_lookup == null) {
7666 MemberLookupFailed (
7667 ec, expr_type, expr_type, Identifier, null, true, loc);
7671 if (member_lookup is TypeExpr) {
7672 if (!(new_expr is TypeExpr) &&
7673 (original == null || !original.IdenticalNameAndTypeName (ec, new_expr, loc))) {
7674 Report.Error (572, loc, "`{0}': cannot reference a type through an expression; try `{1}' instead",
7675 Identifier, member_lookup.GetSignatureForError ());
7679 ConstructedType ct = new_expr as ConstructedType;
7682 // When looking up a nested type in a generic instance
7683 // via reflection, we always get a generic type definition
7684 // and not a generic instance - so we have to do this here.
7686 // See gtest-172-lib.cs and gtest-172.cs for an example.
7688 ct = new ConstructedType (
7689 member_lookup.Type, ct.TypeArguments, loc);
7691 return ct.ResolveAsTypeStep (ec);
7694 return member_lookup;
7697 MemberExpr me = (MemberExpr) member_lookup;
7698 member_lookup = me.ResolveMemberAccess (ec, new_expr, loc, original);
7699 if (member_lookup == null)
7703 MethodGroupExpr mg = member_lookup as MethodGroupExpr;
7705 throw new InternalErrorException ();
7707 return mg.ResolveGeneric (ec, args);
7710 // The following DoResolve/DoResolveLValue will do the definite assignment
7713 if (right_side != null)
7714 member_lookup = member_lookup.DoResolveLValue (ec, right_side);
7716 member_lookup = member_lookup.DoResolve (ec);
7718 return member_lookup;
7721 public override Expression DoResolve (EmitContext ec)
7723 return DoResolve (ec, null);
7726 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
7728 return DoResolve (ec, right_side);
7731 public override FullNamedExpression ResolveAsTypeStep (EmitContext ec, bool silent)
7733 return ResolveNamespaceOrType (ec, silent);
7736 public FullNamedExpression ResolveNamespaceOrType (EmitContext ec, bool silent)
7738 FullNamedExpression new_expr = expr.ResolveAsTypeStep (ec, silent);
7740 if (new_expr == null) {
7741 Report.Error (234, "No such name or typespace {0}", expr);
7745 string lookup_id = MemberName.MakeName (Identifier, args);
7747 if (new_expr is Namespace) {
7748 Namespace ns = (Namespace) new_expr;
7749 FullNamedExpression retval = ns.Lookup (ec.DeclSpace, lookup_id, loc);
7750 if ((retval != null) && (args != null))
7751 retval = new ConstructedType (retval, args, loc).ResolveAsTypeStep (ec);
7752 if (!silent && retval == null)
7753 Report.Error (234, loc, "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing an assembly reference?",
7754 Identifier, ns.FullName);
7758 TypeExpr tnew_expr = new_expr.ResolveAsTypeTerminal (ec);
7759 if (tnew_expr == null)
7762 Type expr_type = tnew_expr.Type;
7764 if (expr_type.IsPointer){
7765 Error (23, "The `.' operator can not be applied to pointer operands (" +
7766 TypeManager.CSharpName (expr_type) + ")");
7770 Expression member_lookup = MemberLookup (
7771 ec, ec.ContainerType, expr_type, expr_type, lookup_id,
7772 MemberTypes.NestedType, BindingFlags.Public | BindingFlags.NonPublic, loc);
7773 if (member_lookup == null) {
7774 int errors = Report.Errors;
7775 MemberLookupFailed (ec, expr_type, expr_type, lookup_id, null, false, loc);
7777 if (!silent && errors == Report.Errors) {
7778 Report.Error (426, loc, "The nested type `{0}' does not exist in the type `{1}'",
7779 Identifier, new_expr.GetSignatureForError ());
7784 if (!(member_lookup is TypeExpr)) {
7785 new_expr.Error_UnexpectedKind (ec, "type", loc);
7789 TypeExpr texpr = member_lookup.ResolveAsTypeTerminal (ec);
7793 TypeArguments the_args = args;
7794 if (TypeManager.HasGenericArguments (expr_type)) {
7795 Type[] decl_args = TypeManager.GetTypeArguments (expr_type);
7797 TypeArguments new_args = new TypeArguments (loc);
7798 foreach (Type decl in decl_args)
7799 new_args.Add (new TypeExpression (decl, loc));
7802 new_args.Add (args);
7804 the_args = new_args;
7807 if (the_args != null) {
7808 ConstructedType ctype = new ConstructedType (texpr.Type, the_args, loc);
7809 return ctype.ResolveAsTypeStep (ec);
7815 public override void Emit (EmitContext ec)
7817 throw new Exception ("Should not happen");
7820 public override string ToString ()
7822 return expr + "." + MemberName.MakeName (Identifier, args);
7825 public override string GetSignatureForError ()
7827 return expr.GetSignatureForError () + "." + Identifier;
7832 /// Implements checked expressions
7834 public class CheckedExpr : Expression {
7836 public Expression Expr;
7838 public CheckedExpr (Expression e, Location l)
7844 public override Expression DoResolve (EmitContext ec)
7846 bool last_check = ec.CheckState;
7847 bool last_const_check = ec.ConstantCheckState;
7849 ec.CheckState = true;
7850 ec.ConstantCheckState = true;
7851 Expr = Expr.Resolve (ec);
7852 ec.CheckState = last_check;
7853 ec.ConstantCheckState = last_const_check;
7858 if (Expr is Constant)
7861 eclass = Expr.eclass;
7866 public override void Emit (EmitContext ec)
7868 bool last_check = ec.CheckState;
7869 bool last_const_check = ec.ConstantCheckState;
7871 ec.CheckState = true;
7872 ec.ConstantCheckState = true;
7874 ec.CheckState = last_check;
7875 ec.ConstantCheckState = last_const_check;
7881 /// Implements the unchecked expression
7883 public class UnCheckedExpr : Expression {
7885 public Expression Expr;
7887 public UnCheckedExpr (Expression e, Location l)
7893 public override Expression DoResolve (EmitContext ec)
7895 bool last_check = ec.CheckState;
7896 bool last_const_check = ec.ConstantCheckState;
7898 ec.CheckState = false;
7899 ec.ConstantCheckState = false;
7900 Expr = Expr.Resolve (ec);
7901 ec.CheckState = last_check;
7902 ec.ConstantCheckState = last_const_check;
7907 if (Expr is Constant)
7910 eclass = Expr.eclass;
7915 public override void Emit (EmitContext ec)
7917 bool last_check = ec.CheckState;
7918 bool last_const_check = ec.ConstantCheckState;
7920 ec.CheckState = false;
7921 ec.ConstantCheckState = false;
7923 ec.CheckState = last_check;
7924 ec.ConstantCheckState = last_const_check;
7930 /// An Element Access expression.
7932 /// During semantic analysis these are transformed into
7933 /// IndexerAccess, ArrayAccess or a PointerArithmetic.
7935 public class ElementAccess : Expression {
7936 public ArrayList Arguments;
7937 public Expression Expr;
7939 public ElementAccess (Expression e, ArrayList e_list)
7948 Arguments = new ArrayList ();
7949 foreach (Expression tmp in e_list)
7950 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
7954 bool CommonResolve (EmitContext ec)
7956 Expr = Expr.Resolve (ec);
7961 if (Arguments == null)
7964 foreach (Argument a in Arguments){
7965 if (!a.Resolve (ec, loc))
7972 Expression MakePointerAccess (EmitContext ec, Type t)
7974 if (t == TypeManager.void_ptr_type){
7975 Error (242, "The array index operation is not valid on void pointers");
7978 if (Arguments.Count != 1){
7979 Error (196, "A pointer must be indexed by only one value");
7984 p = new PointerArithmetic (true, Expr, ((Argument)Arguments [0]).Expr, t, loc).Resolve (ec);
7987 return new Indirection (p, loc).Resolve (ec);
7990 public override Expression DoResolve (EmitContext ec)
7992 if (!CommonResolve (ec))
7996 // We perform some simple tests, and then to "split" the emit and store
7997 // code we create an instance of a different class, and return that.
7999 // I am experimenting with this pattern.
8003 if (t == TypeManager.array_type){
8004 Report.Error (21, loc, "Cannot apply indexing with [] to an expression of type `System.Array'");
8009 return (new ArrayAccess (this, loc)).Resolve (ec);
8011 return MakePointerAccess (ec, Expr.Type);
8013 FieldExpr fe = Expr as FieldExpr;
8015 IFixedBuffer ff = AttributeTester.GetFixedBuffer (fe.FieldInfo);
8017 return MakePointerAccess (ec, ff.ElementType);
8020 return (new IndexerAccess (this, loc)).Resolve (ec);
8023 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
8025 if (!CommonResolve (ec))
8030 return (new ArrayAccess (this, loc)).DoResolveLValue (ec, right_side);
8033 return MakePointerAccess (ec, Expr.Type);
8035 FieldExpr fe = Expr as FieldExpr;
8037 IFixedBuffer ff = AttributeTester.GetFixedBuffer (fe.FieldInfo);
8039 if (!(fe.InstanceExpression is LocalVariableReference) &&
8040 !(fe.InstanceExpression is This)) {
8041 Report.Error (1708, loc, "Fixed size buffers can only be accessed through locals or fields");
8044 // TODO: not sure whether it is correct
8045 // if (!ec.InFixedInitializer) {
8046 // Error (1666, "You cannot use fixed sized buffers contained in unfixed expressions. Try using the fixed statement");
8049 return MakePointerAccess (ec, ff.ElementType);
8052 return (new IndexerAccess (this, loc)).DoResolveLValue (ec, right_side);
8055 public override void Emit (EmitContext ec)
8057 throw new Exception ("Should never be reached");
8062 /// Implements array access
8064 public class ArrayAccess : Expression, IAssignMethod, IMemoryLocation {
8066 // Points to our "data" repository
8070 LocalTemporary temp;
8073 public ArrayAccess (ElementAccess ea_data, Location l)
8076 eclass = ExprClass.Variable;
8080 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
8082 return DoResolve (ec);
8085 public override Expression DoResolve (EmitContext ec)
8088 ExprClass eclass = ea.Expr.eclass;
8090 // As long as the type is valid
8091 if (!(eclass == ExprClass.Variable || eclass == ExprClass.PropertyAccess ||
8092 eclass == ExprClass.Value)) {
8093 ea.Expr.Error_UnexpectedKind ("variable or value");
8098 Type t = ea.Expr.Type;
8099 if (t.GetArrayRank () != ea.Arguments.Count){
8100 Report.Error (22, ea.Location, "Wrong number of indexes `{0}' inside [], expected `{1}'",
8101 ea.Arguments.Count, t.GetArrayRank ());
8105 type = TypeManager.GetElementType (t);
8106 if (type.IsPointer && !ec.InUnsafe){
8107 UnsafeError (ea.Location);
8111 foreach (Argument a in ea.Arguments){
8112 Type argtype = a.Type;
8114 if (argtype == TypeManager.int32_type ||
8115 argtype == TypeManager.uint32_type ||
8116 argtype == TypeManager.int64_type ||
8117 argtype == TypeManager.uint64_type) {
8118 Constant c = a.Expr as Constant;
8119 if (c != null && c.IsNegative) {
8120 Report.Warning (251, 2, ea.Location, "Indexing an array with a negative index (array indices always start at zero)");
8126 // Mhm. This is strage, because the Argument.Type is not the same as
8127 // Argument.Expr.Type: the value changes depending on the ref/out setting.
8129 // Wonder if I will run into trouble for this.
8131 a.Expr = ExpressionToArrayArgument (ec, a.Expr, ea.Location);
8136 eclass = ExprClass.Variable;
8142 /// Emits the right opcode to load an object of Type `t'
8143 /// from an array of T
8145 static public void EmitLoadOpcode (ILGenerator ig, Type type)
8147 if (type == TypeManager.byte_type || type == TypeManager.bool_type)
8148 ig.Emit (OpCodes.Ldelem_U1);
8149 else if (type == TypeManager.sbyte_type)
8150 ig.Emit (OpCodes.Ldelem_I1);
8151 else if (type == TypeManager.short_type)
8152 ig.Emit (OpCodes.Ldelem_I2);
8153 else if (type == TypeManager.ushort_type || type == TypeManager.char_type)
8154 ig.Emit (OpCodes.Ldelem_U2);
8155 else if (type == TypeManager.int32_type)
8156 ig.Emit (OpCodes.Ldelem_I4);
8157 else if (type == TypeManager.uint32_type)
8158 ig.Emit (OpCodes.Ldelem_U4);
8159 else if (type == TypeManager.uint64_type)
8160 ig.Emit (OpCodes.Ldelem_I8);
8161 else if (type == TypeManager.int64_type)
8162 ig.Emit (OpCodes.Ldelem_I8);
8163 else if (type == TypeManager.float_type)
8164 ig.Emit (OpCodes.Ldelem_R4);
8165 else if (type == TypeManager.double_type)
8166 ig.Emit (OpCodes.Ldelem_R8);
8167 else if (type == TypeManager.intptr_type)
8168 ig.Emit (OpCodes.Ldelem_I);
8169 else if (TypeManager.IsEnumType (type)){
8170 EmitLoadOpcode (ig, TypeManager.EnumToUnderlying (type));
8171 } else if (type.IsValueType){
8172 ig.Emit (OpCodes.Ldelema, type);
8173 ig.Emit (OpCodes.Ldobj, type);
8174 } else if (type.IsGenericParameter)
8175 ig.Emit (OpCodes.Ldelem_Any, type);
8176 else if (type.IsPointer)
8177 ig.Emit (OpCodes.Ldelem_I);
8179 ig.Emit (OpCodes.Ldelem_Ref);
8183 /// Returns the right opcode to store an object of Type `t'
8184 /// from an array of T.
8186 static public OpCode GetStoreOpcode (Type t, out bool is_stobj, out bool has_type_arg)
8188 //Console.WriteLine (new System.Diagnostics.StackTrace ());
8189 has_type_arg = false; is_stobj = false;
8190 t = TypeManager.TypeToCoreType (t);
8191 if (TypeManager.IsEnumType (t))
8192 t = TypeManager.EnumToUnderlying (t);
8193 if (t == TypeManager.byte_type || t == TypeManager.sbyte_type ||
8194 t == TypeManager.bool_type)
8195 return OpCodes.Stelem_I1;
8196 else if (t == TypeManager.short_type || t == TypeManager.ushort_type ||
8197 t == TypeManager.char_type)
8198 return OpCodes.Stelem_I2;
8199 else if (t == TypeManager.int32_type || t == TypeManager.uint32_type)
8200 return OpCodes.Stelem_I4;
8201 else if (t == TypeManager.int64_type || t == TypeManager.uint64_type)
8202 return OpCodes.Stelem_I8;
8203 else if (t == TypeManager.float_type)
8204 return OpCodes.Stelem_R4;
8205 else if (t == TypeManager.double_type)
8206 return OpCodes.Stelem_R8;
8207 else if (t == TypeManager.intptr_type) {
8208 has_type_arg = true;
8210 return OpCodes.Stobj;
8211 } else if (t.IsValueType) {
8212 has_type_arg = true;
8214 return OpCodes.Stobj;
8215 } else if (t.IsGenericParameter) {
8216 has_type_arg = true;
8217 return OpCodes.Stelem_Any;
8218 } else if (t.IsPointer)
8219 return OpCodes.Stelem_I;
8221 return OpCodes.Stelem_Ref;
8224 MethodInfo FetchGetMethod ()
8226 ModuleBuilder mb = CodeGen.Module.Builder;
8227 int arg_count = ea.Arguments.Count;
8228 Type [] args = new Type [arg_count];
8231 for (int i = 0; i < arg_count; i++){
8232 //args [i++] = a.Type;
8233 args [i] = TypeManager.int32_type;
8236 get = mb.GetArrayMethod (
8237 ea.Expr.Type, "Get",
8238 CallingConventions.HasThis |
8239 CallingConventions.Standard,
8245 MethodInfo FetchAddressMethod ()
8247 ModuleBuilder mb = CodeGen.Module.Builder;
8248 int arg_count = ea.Arguments.Count;
8249 Type [] args = new Type [arg_count];
8253 ret_type = TypeManager.GetReferenceType (type);
8255 for (int i = 0; i < arg_count; i++){
8256 //args [i++] = a.Type;
8257 args [i] = TypeManager.int32_type;
8260 address = mb.GetArrayMethod (
8261 ea.Expr.Type, "Address",
8262 CallingConventions.HasThis |
8263 CallingConventions.Standard,
8270 // Load the array arguments into the stack.
8272 // If we have been requested to cache the values (cached_locations array
8273 // initialized), then load the arguments the first time and store them
8274 // in locals. otherwise load from local variables.
8276 void LoadArrayAndArguments (EmitContext ec)
8278 ILGenerator ig = ec.ig;
8281 foreach (Argument a in ea.Arguments){
8282 Type argtype = a.Expr.Type;
8286 if (argtype == TypeManager.int64_type)
8287 ig.Emit (OpCodes.Conv_Ovf_I);
8288 else if (argtype == TypeManager.uint64_type)
8289 ig.Emit (OpCodes.Conv_Ovf_I_Un);
8293 public void Emit (EmitContext ec, bool leave_copy)
8295 int rank = ea.Expr.Type.GetArrayRank ();
8296 ILGenerator ig = ec.ig;
8299 LoadArrayAndArguments (ec);
8302 EmitLoadOpcode (ig, type);
8306 method = FetchGetMethod ();
8307 ig.Emit (OpCodes.Call, method);
8310 LoadFromPtr (ec.ig, this.type);
8313 ec.ig.Emit (OpCodes.Dup);
8314 temp = new LocalTemporary (ec, this.type);
8319 public override void Emit (EmitContext ec)
8324 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
8326 int rank = ea.Expr.Type.GetArrayRank ();
8327 ILGenerator ig = ec.ig;
8328 Type t = source.Type;
8329 prepared = prepare_for_load;
8331 if (prepare_for_load) {
8332 AddressOf (ec, AddressOp.LoadStore);
8333 ec.ig.Emit (OpCodes.Dup);
8336 ec.ig.Emit (OpCodes.Dup);
8337 temp = new LocalTemporary (ec, this.type);
8340 StoreFromPtr (ec.ig, t);
8348 LoadArrayAndArguments (ec);
8351 bool is_stobj, has_type_arg;
8352 OpCode op = GetStoreOpcode (t, out is_stobj, out has_type_arg);
8355 // The stobj opcode used by value types will need
8356 // an address on the stack, not really an array/array
8360 ig.Emit (OpCodes.Ldelema, t);
8364 ec.ig.Emit (OpCodes.Dup);
8365 temp = new LocalTemporary (ec, this.type);
8370 ig.Emit (OpCodes.Stobj, t);
8371 else if (has_type_arg)
8376 ModuleBuilder mb = CodeGen.Module.Builder;
8377 int arg_count = ea.Arguments.Count;
8378 Type [] args = new Type [arg_count + 1];
8383 ec.ig.Emit (OpCodes.Dup);
8384 temp = new LocalTemporary (ec, this.type);
8388 for (int i = 0; i < arg_count; i++){
8389 //args [i++] = a.Type;
8390 args [i] = TypeManager.int32_type;
8393 args [arg_count] = type;
8395 set = mb.GetArrayMethod (
8396 ea.Expr.Type, "Set",
8397 CallingConventions.HasThis |
8398 CallingConventions.Standard,
8399 TypeManager.void_type, args);
8401 ig.Emit (OpCodes.Call, set);
8408 public void AddressOf (EmitContext ec, AddressOp mode)
8410 int rank = ea.Expr.Type.GetArrayRank ();
8411 ILGenerator ig = ec.ig;
8413 LoadArrayAndArguments (ec);
8416 ig.Emit (OpCodes.Ldelema, type);
8418 MethodInfo address = FetchAddressMethod ();
8419 ig.Emit (OpCodes.Call, address);
8423 public void EmitGetLength (EmitContext ec, int dim)
8425 int rank = ea.Expr.Type.GetArrayRank ();
8426 ILGenerator ig = ec.ig;
8430 ig.Emit (OpCodes.Ldlen);
8431 ig.Emit (OpCodes.Conv_I4);
8433 IntLiteral.EmitInt (ig, dim);
8434 ig.Emit (OpCodes.Callvirt, TypeManager.int_getlength_int);
8440 // note that the ArrayList itself in mutable. We just can't assign to 'Properties' again.
8441 public readonly ArrayList Properties;
8442 static Indexers empty;
8444 public struct Indexer {
8445 public readonly PropertyInfo PropertyInfo;
8446 public readonly MethodInfo Getter, Setter;
8448 public Indexer (PropertyInfo property_info, MethodInfo get, MethodInfo set)
8450 this.PropertyInfo = property_info;
8458 empty = new Indexers (null);
8461 Indexers (ArrayList array)
8466 static void Append (ref Indexers ix, Type caller_type, MemberInfo [] mi)
8471 foreach (PropertyInfo property in mi){
8472 MethodInfo get, set;
8474 get = property.GetGetMethod (true);
8475 set = property.GetSetMethod (true);
8476 if (get != null && !Expression.IsAccessorAccessible (caller_type, get, out dummy))
8478 if (set != null && !Expression.IsAccessorAccessible (caller_type, set, out dummy))
8480 if (get != null || set != null) {
8482 ix = new Indexers (new ArrayList ());
8483 ix.Properties.Add (new Indexer (property, get, set));
8488 static private MemberInfo [] GetIndexersForTypeOrInterface (Type caller_type, Type lookup_type)
8490 string p_name = TypeManager.IndexerPropertyName (lookup_type);
8492 return TypeManager.MemberLookup (
8493 caller_type, caller_type, lookup_type, MemberTypes.Property,
8494 BindingFlags.Public | BindingFlags.Instance |
8495 BindingFlags.DeclaredOnly, p_name, null);
8498 static public Indexers GetIndexersForType (Type caller_type, Type lookup_type, Location loc)
8500 Indexers ix = empty;
8502 Type copy = lookup_type;
8503 while (copy != TypeManager.object_type && copy != null){
8504 Append (ref ix, caller_type, GetIndexersForTypeOrInterface (caller_type, copy));
8505 copy = copy.BaseType;
8508 if (lookup_type.IsInterface) {
8509 Type [] ifaces = TypeManager.GetInterfaces (lookup_type);
8510 if (ifaces != null) {
8511 foreach (Type itype in ifaces)
8512 Append (ref ix, caller_type, GetIndexersForTypeOrInterface (caller_type, itype));
8521 /// Expressions that represent an indexer call.
8523 public class IndexerAccess : Expression, IAssignMethod {
8525 // Points to our "data" repository
8527 MethodInfo get, set;
8528 ArrayList set_arguments;
8529 bool is_base_indexer;
8531 protected Type indexer_type;
8532 protected Type current_type;
8533 protected Expression instance_expr;
8534 protected ArrayList arguments;
8536 public IndexerAccess (ElementAccess ea, Location loc)
8537 : this (ea.Expr, false, loc)
8539 this.arguments = ea.Arguments;
8542 protected IndexerAccess (Expression instance_expr, bool is_base_indexer,
8545 this.instance_expr = instance_expr;
8546 this.is_base_indexer = is_base_indexer;
8547 this.eclass = ExprClass.Value;
8551 protected virtual bool CommonResolve (EmitContext ec)
8553 indexer_type = instance_expr.Type;
8554 current_type = ec.ContainerType;
8559 public override Expression DoResolve (EmitContext ec)
8561 ArrayList AllGetters = new ArrayList();
8562 if (!CommonResolve (ec))
8566 // Step 1: Query for all `Item' *properties*. Notice
8567 // that the actual methods are pointed from here.
8569 // This is a group of properties, piles of them.
8571 bool found_any = false, found_any_getters = false;
8572 Type lookup_type = indexer_type;
8574 Indexers ilist = Indexers.GetIndexersForType (current_type, lookup_type, loc);
8575 if (ilist.Properties != null) {
8577 foreach (Indexers.Indexer ix in ilist.Properties) {
8578 if (ix.Getter != null)
8579 AllGetters.Add (ix.Getter);
8583 if (AllGetters.Count > 0) {
8584 found_any_getters = true;
8585 get = (MethodInfo) Invocation.OverloadResolve (
8586 ec, new MethodGroupExpr (AllGetters, loc),
8587 arguments, false, loc);
8591 Report.Error (21, loc, "Cannot apply indexing with [] to an expression of type `{0}'",
8592 TypeManager.CSharpName (indexer_type));
8596 if (!found_any_getters) {
8597 Report.Error (154, loc, "The property or indexer `{0}' cannot be used in this context because it lacks the `get' accessor",
8603 Invocation.Error_WrongNumArguments (loc, "this", arguments.Count);
8608 // Only base will allow this invocation to happen.
8610 if (get.IsAbstract && this is BaseIndexerAccess){
8611 Error_CannotCallAbstractBase (TypeManager.CSharpSignature (get));
8615 type = get.ReturnType;
8616 if (type.IsPointer && !ec.InUnsafe){
8621 instance_expr.CheckMarshallByRefAccess (ec.ContainerType);
8623 eclass = ExprClass.IndexerAccess;
8627 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
8629 ArrayList AllSetters = new ArrayList();
8630 if (!CommonResolve (ec))
8633 bool found_any = false, found_any_setters = false;
8635 Indexers ilist = Indexers.GetIndexersForType (current_type, indexer_type, loc);
8636 if (ilist.Properties != null) {
8638 foreach (Indexers.Indexer ix in ilist.Properties) {
8639 if (ix.Setter != null)
8640 AllSetters.Add (ix.Setter);
8643 if (AllSetters.Count > 0) {
8644 found_any_setters = true;
8645 set_arguments = (ArrayList) arguments.Clone ();
8646 set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
8647 set = (MethodInfo) Invocation.OverloadResolve (
8648 ec, new MethodGroupExpr (AllSetters, loc),
8649 set_arguments, false, loc);
8653 Report.Error (21, loc, "Cannot apply indexing with [] to an expression of type `{0}'",
8654 TypeManager.CSharpName (indexer_type));
8658 if (!found_any_setters) {
8659 Error (154, "indexer can not be used in this context, because " +
8660 "it lacks a `set' accessor");
8665 Invocation.Error_WrongNumArguments (loc, "this", arguments.Count);
8670 // Only base will allow this invocation to happen.
8672 if (set.IsAbstract && this is BaseIndexerAccess){
8673 Error_CannotCallAbstractBase (TypeManager.CSharpSignature (set));
8678 // Now look for the actual match in the list of indexers to set our "return" type
8680 type = TypeManager.void_type; // default value
8681 foreach (Indexers.Indexer ix in ilist.Properties){
8682 if (ix.Setter == set){
8683 type = ix.PropertyInfo.PropertyType;
8688 instance_expr.CheckMarshallByRefAccess (ec.ContainerType);
8690 eclass = ExprClass.IndexerAccess;
8694 bool prepared = false;
8695 LocalTemporary temp;
8697 public void Emit (EmitContext ec, bool leave_copy)
8699 Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, get, arguments, loc, prepared, false);
8701 ec.ig.Emit (OpCodes.Dup);
8702 temp = new LocalTemporary (ec, Type);
8708 // source is ignored, because we already have a copy of it from the
8709 // LValue resolution and we have already constructed a pre-cached
8710 // version of the arguments (ea.set_arguments);
8712 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
8714 prepared = prepare_for_load;
8715 Argument a = (Argument) set_arguments [set_arguments.Count - 1];
8720 ec.ig.Emit (OpCodes.Dup);
8721 temp = new LocalTemporary (ec, Type);
8724 } else if (leave_copy) {
8725 temp = new LocalTemporary (ec, Type);
8731 Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, set, set_arguments, loc, false, prepared);
8738 public override void Emit (EmitContext ec)
8745 /// The base operator for method names
8747 public class BaseAccess : Expression {
8750 public BaseAccess (string member, Location l)
8752 this.member = member;
8756 public override Expression DoResolve (EmitContext ec)
8758 Expression c = CommonResolve (ec);
8764 // MethodGroups use this opportunity to flag an error on lacking ()
8766 if (!(c is MethodGroupExpr))
8767 return c.Resolve (ec);
8771 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
8773 Expression c = CommonResolve (ec);
8779 // MethodGroups use this opportunity to flag an error on lacking ()
8781 if (! (c is MethodGroupExpr))
8782 return c.DoResolveLValue (ec, right_side);
8787 Expression CommonResolve (EmitContext ec)
8789 Expression member_lookup;
8790 Type current_type = ec.ContainerType;
8791 Type base_type = current_type.BaseType;
8794 Error (1511, "Keyword `base' is not available in a static method");
8798 if (ec.IsFieldInitializer){
8799 Error (1512, "Keyword `base' is not available in the current context");
8803 member_lookup = MemberLookup (ec, ec.ContainerType, null, base_type,
8804 member, AllMemberTypes, AllBindingFlags,
8806 if (member_lookup == null) {
8807 MemberLookupFailed (ec, base_type, base_type, member, null, true, loc);
8814 left = new TypeExpression (base_type, loc);
8816 left = ec.GetThis (loc);
8818 MemberExpr me = (MemberExpr) member_lookup;
8820 Expression e = me.ResolveMemberAccess (ec, left, loc, null);
8822 if (e is PropertyExpr) {
8823 PropertyExpr pe = (PropertyExpr) e;
8828 if (e is MethodGroupExpr)
8829 ((MethodGroupExpr) e).IsBase = true;
8834 public override void Emit (EmitContext ec)
8836 throw new Exception ("Should never be called");
8841 /// The base indexer operator
8843 public class BaseIndexerAccess : IndexerAccess {
8844 public BaseIndexerAccess (ArrayList args, Location loc)
8845 : base (null, true, loc)
8847 arguments = new ArrayList ();
8848 foreach (Expression tmp in args)
8849 arguments.Add (new Argument (tmp, Argument.AType.Expression));
8852 protected override bool CommonResolve (EmitContext ec)
8854 instance_expr = ec.GetThis (loc);
8856 current_type = ec.ContainerType.BaseType;
8857 indexer_type = current_type;
8859 foreach (Argument a in arguments){
8860 if (!a.Resolve (ec, loc))
8869 /// This class exists solely to pass the Type around and to be a dummy
8870 /// that can be passed to the conversion functions (this is used by
8871 /// foreach implementation to typecast the object return value from
8872 /// get_Current into the proper type. All code has been generated and
8873 /// we only care about the side effect conversions to be performed
8875 /// This is also now used as a placeholder where a no-action expression
8876 /// is needed (the `New' class).
8878 public class EmptyExpression : Expression {
8879 public static readonly EmptyExpression Null = new EmptyExpression ();
8881 static EmptyExpression temp = new EmptyExpression ();
8882 public static EmptyExpression Grab ()
8885 throw new InternalErrorException ("Nested Grab");
8886 EmptyExpression retval = temp;
8891 public static void Release (EmptyExpression e)
8894 throw new InternalErrorException ("Already released");
8898 // TODO: should be protected
8899 public EmptyExpression ()
8901 type = TypeManager.object_type;
8902 eclass = ExprClass.Value;
8903 loc = Location.Null;
8906 public EmptyExpression (Type t)
8909 eclass = ExprClass.Value;
8910 loc = Location.Null;
8913 public override Expression DoResolve (EmitContext ec)
8918 public override void Emit (EmitContext ec)
8920 // nothing, as we only exist to not do anything.
8924 // This is just because we might want to reuse this bad boy
8925 // instead of creating gazillions of EmptyExpressions.
8926 // (CanImplicitConversion uses it)
8928 public void SetType (Type t)
8934 public class UserCast : Expression {
8938 public UserCast (MethodInfo method, Expression source, Location l)
8940 this.method = method;
8941 this.source = source;
8942 type = method.ReturnType;
8943 eclass = ExprClass.Value;
8947 public Expression Source {
8953 public override Expression DoResolve (EmitContext ec)
8956 // We are born fully resolved
8961 public override void Emit (EmitContext ec)
8963 ILGenerator ig = ec.ig;
8967 if (method is MethodInfo)
8968 ig.Emit (OpCodes.Call, (MethodInfo) method);
8970 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
8976 // This class is used to "construct" the type during a typecast
8977 // operation. Since the Type.GetType class in .NET can parse
8978 // the type specification, we just use this to construct the type
8979 // one bit at a time.
8981 public class ComposedCast : TypeExpr {
8985 public ComposedCast (Expression left, string dim)
8986 : this (left, dim, left.Location)
8990 public ComposedCast (Expression left, string dim, Location l)
8997 public Expression RemoveNullable ()
8999 if (dim.EndsWith ("?")) {
9000 dim = dim.Substring (0, dim.Length - 1);
9008 protected override TypeExpr DoResolveAsTypeStep (EmitContext ec)
9010 TypeExpr lexpr = left.ResolveAsTypeTerminal (ec);
9014 Type ltype = lexpr.Type;
9016 if ((ltype == TypeManager.void_type) && (dim != "*")) {
9017 Report.Error (1547, Location,
9018 "Keyword 'void' cannot be used in this context");
9022 if ((dim.Length > 0) && (dim [0] == '?')) {
9023 TypeExpr nullable = new NullableType (left, loc);
9025 nullable = new ComposedCast (nullable, dim.Substring (1), loc);
9026 return nullable.ResolveAsTypeTerminal (ec);
9029 if (dim == "*" && !TypeManager.VerifyUnManaged (ltype, loc)) {
9034 type = TypeManager.GetConstructedType (ltype, dim);
9039 throw new InternalErrorException ("Couldn't create computed type " + ltype + dim);
9042 if (!ec.InUnsafe && type.IsPointer){
9047 if (type.IsArray && (type.GetElementType () == TypeManager.arg_iterator_type ||
9048 type.GetElementType () == TypeManager.typed_reference_type)) {
9049 Report.Error (611, loc, "Array elements cannot be of type `{0}'", TypeManager.CSharpName (type.GetElementType ()));
9053 eclass = ExprClass.Type;
9057 public override string Name {
9063 public override string FullName {
9065 return type.FullName;
9070 public class FixedBufferPtr: Expression {
9073 public FixedBufferPtr (Expression array, Type array_type, Location l)
9078 type = TypeManager.GetPointerType (array_type);
9079 eclass = ExprClass.Value;
9082 public override void Emit(EmitContext ec)
9087 public override Expression DoResolve (EmitContext ec)
9090 // We are born fully resolved
9098 // This class is used to represent the address of an array, used
9099 // only by the Fixed statement, this generates "&a [0]" construct
9100 // for fixed (char *pa = a)
9102 public class ArrayPtr : FixedBufferPtr {
9105 public ArrayPtr (Expression array, Type array_type, Location l):
9106 base (array, array_type, l)
9108 this.array_type = array_type;
9111 public override void Emit (EmitContext ec)
9115 ILGenerator ig = ec.ig;
9116 IntLiteral.EmitInt (ig, 0);
9117 ig.Emit (OpCodes.Ldelema, array_type);
9122 // Used by the fixed statement
9124 public class StringPtr : Expression {
9127 public StringPtr (LocalBuilder b, Location l)
9130 eclass = ExprClass.Value;
9131 type = TypeManager.char_ptr_type;
9135 public override Expression DoResolve (EmitContext ec)
9137 // This should never be invoked, we are born in fully
9138 // initialized state.
9143 public override void Emit (EmitContext ec)
9145 ILGenerator ig = ec.ig;
9147 ig.Emit (OpCodes.Ldloc, b);
9148 ig.Emit (OpCodes.Conv_I);
9149 ig.Emit (OpCodes.Call, TypeManager.int_get_offset_to_string_data);
9150 ig.Emit (OpCodes.Add);
9155 // Implements the `stackalloc' keyword
9157 public class StackAlloc : Expression {
9162 public StackAlloc (Expression type, Expression count, Location l)
9169 public override Expression DoResolve (EmitContext ec)
9171 count = count.Resolve (ec);
9175 if (count.Type != TypeManager.int32_type){
9176 count = Convert.ImplicitConversionRequired (ec, count, TypeManager.int32_type, loc);
9181 Constant c = count as Constant;
9182 if (c != null && c.IsNegative) {
9183 Report.Error (247, loc, "Cannot use a negative size with stackalloc");
9187 if (ec.InCatch || ec.InFinally) {
9188 Error (255, "Cannot use stackalloc in finally or catch");
9192 TypeExpr texpr = t.ResolveAsTypeTerminal (ec);
9198 if (!TypeManager.VerifyUnManaged (otype, loc))
9201 type = TypeManager.GetPointerType (otype);
9202 eclass = ExprClass.Value;
9207 public override void Emit (EmitContext ec)
9209 int size = GetTypeSize (otype);
9210 ILGenerator ig = ec.ig;
9213 ig.Emit (OpCodes.Sizeof, otype);
9215 IntConstant.EmitInt (ig, size);
9217 ig.Emit (OpCodes.Mul);
9218 ig.Emit (OpCodes.Localloc);