X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fmcs%2Fexpression.cs;h=f384ab46612781e8d1492713b5025535b9ef7f43;hb=968c2830f973d10a72e5d2907c42744331116299;hp=ac99ad916a3300ecf15d0e950caec3f4e6a9127a;hpb=5d4ffa960e3b1f7af7c15610ccd38c670a0e23f2;p=mono.git diff --git a/mcs/mcs/expression.cs b/mcs/mcs/expression.cs index ac99ad916a3..f384ab46612 100755 --- a/mcs/mcs/expression.cs +++ b/mcs/mcs/expression.cs @@ -25,7 +25,7 @@ namespace Mono.CSharp { ArrayList args; MethodInfo mi; - StaticCallExpr (MethodInfo m, ArrayList a, Location l) + public StaticCallExpr (MethodInfo m, ArrayList a, Location l) { mi = m; args = a; @@ -59,7 +59,13 @@ namespace Mono.CSharp { MethodBase method; args = new ArrayList (1); - args.Add (new Argument (e, Argument.AType.Expression)); + Argument a = new Argument (e, Argument.AType.Expression); + + // We need to resolve the arguments before sending them in ! + if (!a.Resolve (ec, loc)) + return null; + + args.Add (a); method = Invocation.OverloadResolve (ec, (MethodGroupExpr) mg, args, loc); if (method == null) @@ -124,7 +130,7 @@ namespace Mono.CSharp { return oper.ToString (); } - static string [] oper_names; + public static readonly string [] oper_names; static Unary () { @@ -155,7 +161,7 @@ namespace Mono.CSharp { static Expression TryReduceNegative (Constant expr) { Expression e = null; - + if (expr is IntConstant) e = new IntConstant (-((IntConstant) expr).Value); else if (expr is UIntConstant){ @@ -164,7 +170,7 @@ namespace Mono.CSharp { if (value < 2147483649) return new IntConstant (-(int)value); else - e = new LongConstant (value); + e = new LongConstant (-value); } else if (expr is LongConstant) e = new LongConstant (-((LongConstant) expr).Value); @@ -223,9 +229,30 @@ namespace Mono.CSharp { (expr_type == TypeManager.int64_type) || (expr_type == TypeManager.uint64_type) || (expr_type.IsSubclassOf (TypeManager.enum_type)))){ + result = null; - Error23 (expr_type); - return false; + if (Convert.ImplicitConversionExists (ec, e, TypeManager.int32_type)){ + result = new Cast (new TypeExpr (TypeManager.int32_type, loc), e, loc); + result = result.Resolve (ec); + } else if (Convert.ImplicitConversionExists (ec, e, TypeManager.uint32_type)){ + result = new Cast (new TypeExpr (TypeManager.uint32_type, loc), e, loc); + result = result.Resolve (ec); + } else if (Convert.ImplicitConversionExists (ec, e, TypeManager.int64_type)){ + result = new Cast (new TypeExpr (TypeManager.int64_type, loc), e, loc); + result = result.Resolve (ec); + } else if (Convert.ImplicitConversionExists (ec, e, TypeManager.uint64_type)){ + result = new Cast (new TypeExpr (TypeManager.uint64_type, loc), e, loc); + result = result.Resolve (ec); + } + + if (result == null || !(result is Constant)){ + result = null; + Error23 (expr_type); + return false; + } + + expr_type = result.Type; + e = (Constant) result; } if (e is EnumConstant){ @@ -314,8 +341,11 @@ namespace Mono.CSharp { switch (Oper){ case Operator.LogicalNot: if (expr_type != TypeManager.bool_type) { - Error23 (Expr.Type); - return null; + Expr = ResolveBoolean (ec, Expr, loc); + if (Expr == null){ + Error23 (expr_type); + return null; + } } type = TypeManager.bool_type; @@ -329,22 +359,22 @@ namespace Mono.CSharp { (expr_type.IsSubclassOf (TypeManager.enum_type)))){ Expression e; - e = ConvertImplicit (ec, Expr, TypeManager.int32_type, loc); + e = Convert.ImplicitConversion (ec, Expr, TypeManager.int32_type, loc); if (e != null){ type = TypeManager.int32_type; return this; } - e = ConvertImplicit (ec, Expr, TypeManager.uint32_type, loc); + e = Convert.ImplicitConversion (ec, Expr, TypeManager.uint32_type, loc); if (e != null){ type = TypeManager.uint32_type; return this; } - e = ConvertImplicit (ec, Expr, TypeManager.int64_type, loc); + e = Convert.ImplicitConversion (ec, Expr, TypeManager.int64_type, loc); if (e != null){ type = TypeManager.int64_type; return this; } - e = ConvertImplicit (ec, Expr, TypeManager.uint64_type, loc); + e = Convert.ImplicitConversion (ec, Expr, TypeManager.uint64_type, loc); if (e != null){ type = TypeManager.uint64_type; return this; @@ -369,10 +399,8 @@ namespace Mono.CSharp { if (!TypeManager.VerifyUnManaged (Expr.Type, loc)){ return null; } - - string ptr_type_name = Expr.Type.FullName + "*"; - type = TypeManager.LookupType (ptr_type_name); - + + type = TypeManager.GetPointerType (Expr.Type); return this; case Operator.Indirection: @@ -382,9 +410,7 @@ namespace Mono.CSharp { } if (!expr_type.IsPointer){ - Error ( - 193, - "The * or -> operator can only be applied to pointers"); + Error (193, "The * or -> operator can only be applied to pointers"); return null; } @@ -427,7 +453,7 @@ namespace Mono.CSharp { // // // The following is inneficient, because we call - // ConvertImplicit too many times. + // ImplicitConversion too many times. // // It is also not clear if we should convert to Float // or Double initially. @@ -439,7 +465,7 @@ namespace Mono.CSharp { // bt wrote as a decimal interger literal // type = TypeManager.int64_type; - Expr = ConvertImplicit (ec, Expr, type, loc); + Expr = Convert.ImplicitConversion (ec, Expr, type, loc); return this; } @@ -458,21 +484,21 @@ namespace Mono.CSharp { return this; } - expr = ConvertImplicit (ec, Expr, TypeManager.int32_type, loc); + expr = Convert.ImplicitConversion (ec, Expr, TypeManager.int32_type, loc); if (expr != null){ Expr = expr; type = expr.Type; return this; } - expr = ConvertImplicit (ec, Expr, TypeManager.int64_type, loc); + expr = Convert.ImplicitConversion (ec, Expr, TypeManager.int64_type, loc); if (expr != null){ Expr = expr; type = expr.Type; return this; } - expr = ConvertImplicit (ec, Expr, TypeManager.double_type, loc); + expr = Convert.ImplicitConversion (ec, Expr, TypeManager.double_type, loc); if (expr != null){ Expr = expr; type = expr.Type; @@ -600,15 +626,15 @@ namespace Mono.CSharp { public void EmitAssign (EmitContext ec, Expression source) { if (temporary != null){ - if (have_temporary){ + if (have_temporary) temporary.Emit (ec); - return; + else { + expr.Emit (ec); + ec.ig.Emit (OpCodes.Dup); + temporary.Store (ec); + have_temporary = true; } - expr.Emit (ec); - ec.ig.Emit (OpCodes.Dup); - temporary.Store (ec); - have_temporary = true; - } else + } else expr.Emit (ec); source.Emit (ec); @@ -642,6 +668,11 @@ namespace Mono.CSharp { { temporary = new LocalTemporary (ec, type); } + + public override string ToString () + { + return "*(" + expr + ")"; + } } /// @@ -657,12 +688,19 @@ namespace Mono.CSharp { /// for the common case, and one with the extra fields for more complex /// classes (indexers require temporary access; overloaded require method) /// - /// Maybe we should have classes PreIncrement, PostIncrement, PreDecrement, - /// PostDecrement, that way we could save the `Mode' byte as well. /// public class UnaryMutator : ExpressionStatement { + [Flags] public enum Mode : byte { - PreIncrement, PreDecrement, PostIncrement, PostDecrement + IsIncrement = 0, + IsDecrement = 1, + IsPre = 0, + IsPost = 2, + + PreIncrement = 0, + PreDecrement = IsDecrement, + PostIncrement = IsPost, + PostDecrement = IsPost | IsDecrement } Mode mode; @@ -776,7 +814,7 @@ namespace Mono.CSharp { return null; } else { - expr.Error118 ("variable, indexer or property access"); + expr.Error_UnexpectedKind ("variable, indexer or property access"); return null; } @@ -802,12 +840,19 @@ namespace Mono.CSharp { } // - // Loads the proper "1" into the stack based on the type + // Loads the proper "1" into the stack based on the type, then it emits the + // opcode for the operation requested // - static void LoadOne (ILGenerator ig, Type t) + void LoadOneAndEmitOp (EmitContext ec, Type t) { + // + // Measure if getting the typecode and using that is more/less efficient + // that comparing types. t.GetTypeCode() is an internal call. + // + ILGenerator ig = ec.ig; + if (t == TypeManager.uint64_type || t == TypeManager.int64_type) - ig.Emit (OpCodes.Ldc_I8, 1L); + LongConstant.EmitLong (ig, 1); else if (t == TypeManager.double_type) ig.Emit (OpCodes.Ldc_R8, 1.0); else if (t == TypeManager.float_type) @@ -821,65 +866,120 @@ namespace Mono.CSharp { IntConstant.EmitInt (ig, n); } else ig.Emit (OpCodes.Ldc_I4_1); + + // + // Now emit the operation + // + if (ec.CheckState){ + if (t == TypeManager.int32_type || + t == TypeManager.int64_type){ + if ((mode & Mode.IsDecrement) != 0) + ig.Emit (OpCodes.Sub_Ovf); + else + ig.Emit (OpCodes.Add_Ovf); + } else if (t == TypeManager.uint32_type || + t == TypeManager.uint64_type){ + if ((mode & Mode.IsDecrement) != 0) + ig.Emit (OpCodes.Sub_Ovf_Un); + else + ig.Emit (OpCodes.Add_Ovf_Un); + } else { + if ((mode & Mode.IsDecrement) != 0) + ig.Emit (OpCodes.Sub_Ovf); + else + ig.Emit (OpCodes.Add_Ovf); + } + } else { + if ((mode & Mode.IsDecrement) != 0) + ig.Emit (OpCodes.Sub); + else + ig.Emit (OpCodes.Add); + } + + if (t == TypeManager.sbyte_type){ + if (ec.CheckState) + ig.Emit (OpCodes.Conv_Ovf_I1); + else + ig.Emit (OpCodes.Conv_I1); + } else if (t == TypeManager.byte_type){ + if (ec.CheckState) + ig.Emit (OpCodes.Conv_Ovf_U1); + else + ig.Emit (OpCodes.Conv_U1); + } else if (t == TypeManager.short_type){ + if (ec.CheckState) + ig.Emit (OpCodes.Conv_Ovf_I2); + else + ig.Emit (OpCodes.Conv_I2); + } else if (t == TypeManager.ushort_type || t == TypeManager.char_type){ + if (ec.CheckState) + ig.Emit (OpCodes.Conv_Ovf_U2); + else + ig.Emit (OpCodes.Conv_U2); + } + } + static EmptyExpression empty_expr; - // - // FIXME: We need some way of avoiding the use of temp_storage - // for some types of storage (parameters, local variables, - // static fields) and single-dimension array access. - // void EmitCode (EmitContext ec, bool is_expr) { ILGenerator ig = ec.ig; IAssignMethod ia = (IAssignMethod) expr; Type expr_type = expr.Type; - - if (temp_storage == null) - temp_storage = new LocalTemporary (ec, expr_type); ia.CacheTemporaries (ec); - ig.Emit (OpCodes.Nop); + + if (temp_storage == null){ + // + // Temporary improvement: if we are dealing with something that does + // not require complicated instance setup, avoid using a temporary + // + // For now: only localvariables when not remapped + // + + if (method == null && + (expr is LocalVariableReference && ec.RemapToProxy == false) || + (expr is FieldExpr && ((FieldExpr) expr).FieldInfo.IsStatic)){ + if (empty_expr == null) + empty_expr = new EmptyExpression (); + + switch (mode){ + case Mode.PreIncrement: + case Mode.PreDecrement: + expr.Emit (ec); + + LoadOneAndEmitOp (ec, expr_type); + if (is_expr) + ig.Emit (OpCodes.Dup); + ia.EmitAssign (ec, empty_expr); + break; + + case Mode.PostIncrement: + case Mode.PostDecrement: + expr.Emit (ec); + if (is_expr) + ig.Emit (OpCodes.Dup); + + LoadOneAndEmitOp (ec, expr_type); + ia.EmitAssign (ec, empty_expr); + break; + } + return; + } + temp_storage = new LocalTemporary (ec, expr_type); + } + switch (mode){ case Mode.PreIncrement: case Mode.PreDecrement: if (method == null){ expr.Emit (ec); - - LoadOne (ig, expr_type); - // - // Select the opcode based on the check state (then the type) - // and the actual operation - // - if (ec.CheckState){ - if (expr_type == TypeManager.int32_type || - expr_type == TypeManager.int64_type){ - if (mode == Mode.PreDecrement) - ig.Emit (OpCodes.Sub_Ovf); - else - ig.Emit (OpCodes.Add_Ovf); - } else if (expr_type == TypeManager.uint32_type || - expr_type == TypeManager.uint64_type){ - if (mode == Mode.PreDecrement) - ig.Emit (OpCodes.Sub_Ovf_Un); - else - ig.Emit (OpCodes.Add_Ovf_Un); - } else { - if (mode == Mode.PreDecrement) - ig.Emit (OpCodes.Sub_Ovf); - else - ig.Emit (OpCodes.Add_Ovf); - } - } else { - if (mode == Mode.PreDecrement) - ig.Emit (OpCodes.Sub); - else - ig.Emit (OpCodes.Add); - } + LoadOneAndEmitOp (ec, expr_type); } else method.Emit (ec); - + temp_storage.Store (ec); ia.EmitAssign (ec, temp_storage); if (is_expr) @@ -896,34 +996,8 @@ namespace Mono.CSharp { expr.Emit (ec); else ig.Emit (OpCodes.Dup); - - LoadOne (ig, expr_type); - if (ec.CheckState){ - if (expr_type == TypeManager.int32_type || - expr_type == TypeManager.int64_type){ - if (mode == Mode.PostDecrement) - ig.Emit (OpCodes.Sub_Ovf); - else - ig.Emit (OpCodes.Add_Ovf); - } else if (expr_type == TypeManager.uint32_type || - expr_type == TypeManager.uint64_type){ - if (mode == Mode.PostDecrement) - ig.Emit (OpCodes.Sub_Ovf_Un); - else - ig.Emit (OpCodes.Add_Ovf_Un); - } else { - if (mode == Mode.PostDecrement) - ig.Emit (OpCodes.Sub_Ovf); - else - ig.Emit (OpCodes.Add_Ovf); - } - } else { - if (mode == Mode.PostDecrement) - ig.Emit (OpCodes.Sub); - else - ig.Emit (OpCodes.Add); - } + LoadOneAndEmitOp (ec, expr_type); } else { method.Emit (ec); } @@ -1014,11 +1088,14 @@ namespace Mono.CSharp { return; case Action.AlwaysTrue: ig.Emit (OpCodes.Pop); - ig.Emit (OpCodes.Nop); IntConstant.EmitInt (ig, 1); return; case Action.LeaveOnStack: // the `e != null' rule. + ig.Emit (OpCodes.Ldnull); + ig.Emit (OpCodes.Ceq); + ig.Emit (OpCodes.Ldc_I4_0); + ig.Emit (OpCodes.Ceq); return; case Action.Probe: ig.Emit (OpCodes.Isinst, probe_type); @@ -1047,7 +1124,7 @@ namespace Mono.CSharp { // First case, if at compile time, there is an implicit conversion // then e != null (objects) or true (value types) // - e = ConvertImplicitStandard (ec, expr, probe_type, loc); + e = Convert.ImplicitConversionStandard (ec, expr, probe_type, loc); if (e != null){ expr = e; if (etype.IsValueType) @@ -1056,7 +1133,7 @@ namespace Mono.CSharp { action = Action.LeaveOnStack; warning_always_matches = true; - } else if (ExplicitReferenceConversionExists (etype, probe_type)){ + } else if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){ // // Second case: explicit reference convresion // @@ -1071,16 +1148,13 @@ namespace Mono.CSharp { if (RootContext.WarningLevel >= 1){ if (warning_always_matches) - Warning ( - 183, - "The expression is always of type `" + - TypeManager.CSharpName (probe_type) + "'"); + Warning (183, "The expression is always of type `" + + TypeManager.CSharpName (probe_type) + "'"); else if (warning_never_matches){ if (!(probe_type.IsInterface || expr.Type.IsInterface)) - Warning ( - 184, - "The expression is never of type `" + - TypeManager.CSharpName (probe_type) + "'"); + Warning (184, + "The expression is never of type `" + + TypeManager.CSharpName (probe_type) + "'"); } } @@ -1135,14 +1209,14 @@ namespace Mono.CSharp { } - e = ConvertImplicit (ec, expr, probe_type, loc); + e = Convert.ImplicitConversion (ec, expr, probe_type, loc); if (e != null){ expr = e; do_isinst = false; return this; } - if (ExplicitReferenceConversionExists (etype, probe_type)){ + if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){ do_isinst = true; return this; } @@ -1184,16 +1258,68 @@ namespace Mono.CSharp { } } + bool CheckRange (EmitContext ec, long value, Type type, long min, long max) + { + if (!ec.ConstantCheckState) + return true; + + if ((value < min) || (value > max)) { + Error (221, "Constant value `" + value + "' cannot be converted " + + "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " + + "syntax to override)"); + return false; + } + + return true; + } + + bool CheckRange (EmitContext ec, ulong value, Type type, ulong max) + { + if (!ec.ConstantCheckState) + return true; + + if (value > max) { + Error (221, "Constant value `" + value + "' cannot be converted " + + "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " + + "syntax to override)"); + return false; + } + + return true; + } + + bool CheckUnsigned (EmitContext ec, long value, Type type) + { + if (!ec.ConstantCheckState) + return true; + + if (value < 0) { + Error (221, "Constant value `" + value + "' cannot be converted " + + "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " + + "syntax to override)"); + return false; + } + + return true; + } + /// /// Attempts to do a compile-time folding of a constant cast. /// Expression TryReduce (EmitContext ec, Type target_type) { - if (expr is ByteConstant){ - byte v = ((ByteConstant) expr).Value; + Expression real_expr = expr; + if (real_expr is EnumConstant) + real_expr = ((EnumConstant) real_expr).Child; + + if (real_expr is ByteConstant){ + byte v = ((ByteConstant) real_expr).Value; - if (target_type == TypeManager.sbyte_type) + if (target_type == TypeManager.sbyte_type) { + if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue)) + return null; return new SByteConstant ((sbyte) v); + } if (target_type == TypeManager.short_type) return new ShortConstant ((short) v); if (target_type == TypeManager.ushort_type) @@ -1215,67 +1341,107 @@ namespace Mono.CSharp { if (target_type == TypeManager.decimal_type) return new DecimalConstant ((decimal) v); } - if (expr is SByteConstant){ - sbyte v = ((SByteConstant) expr).Value; + if (real_expr is SByteConstant){ + sbyte v = ((SByteConstant) real_expr).Value; - if (target_type == TypeManager.byte_type) + if (target_type == TypeManager.byte_type) { + if (!CheckUnsigned (ec, v, target_type)) + return null; return new ByteConstant ((byte) v); + } if (target_type == TypeManager.short_type) return new ShortConstant ((short) v); - if (target_type == TypeManager.ushort_type) + if (target_type == TypeManager.ushort_type) { + if (!CheckUnsigned (ec, v, target_type)) + return null; return new UShortConstant ((ushort) v); - if (target_type == TypeManager.int32_type) + } if (target_type == TypeManager.int32_type) return new IntConstant ((int) v); - if (target_type == TypeManager.uint32_type) + if (target_type == TypeManager.uint32_type) { + if (!CheckUnsigned (ec, v, target_type)) + return null; return new UIntConstant ((uint) v); - if (target_type == TypeManager.int64_type) + } if (target_type == TypeManager.int64_type) return new LongConstant ((long) v); - if (target_type == TypeManager.uint64_type) + if (target_type == TypeManager.uint64_type) { + if (!CheckUnsigned (ec, v, target_type)) + return null; return new ULongConstant ((ulong) v); + } if (target_type == TypeManager.float_type) return new FloatConstant ((float) v); if (target_type == TypeManager.double_type) return new DoubleConstant ((double) v); - if (target_type == TypeManager.char_type) + if (target_type == TypeManager.char_type) { + if (!CheckUnsigned (ec, v, target_type)) + return null; return new CharConstant ((char) v); + } if (target_type == TypeManager.decimal_type) return new DecimalConstant ((decimal) v); } - if (expr is ShortConstant){ - short v = ((ShortConstant) expr).Value; + if (real_expr is ShortConstant){ + short v = ((ShortConstant) real_expr).Value; - if (target_type == TypeManager.byte_type) + if (target_type == TypeManager.byte_type) { + if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue)) + return null; return new ByteConstant ((byte) v); - if (target_type == TypeManager.sbyte_type) + } + if (target_type == TypeManager.sbyte_type) { + if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue)) + return null; return new SByteConstant ((sbyte) v); - if (target_type == TypeManager.ushort_type) + } + if (target_type == TypeManager.ushort_type) { + if (!CheckUnsigned (ec, v, target_type)) + return null; return new UShortConstant ((ushort) v); + } if (target_type == TypeManager.int32_type) return new IntConstant ((int) v); - if (target_type == TypeManager.uint32_type) + if (target_type == TypeManager.uint32_type) { + if (!CheckUnsigned (ec, v, target_type)) + return null; return new UIntConstant ((uint) v); + } if (target_type == TypeManager.int64_type) return new LongConstant ((long) v); - if (target_type == TypeManager.uint64_type) + if (target_type == TypeManager.uint64_type) { + if (!CheckUnsigned (ec, v, target_type)) + return null; return new ULongConstant ((ulong) v); + } if (target_type == TypeManager.float_type) return new FloatConstant ((float) v); if (target_type == TypeManager.double_type) return new DoubleConstant ((double) v); - if (target_type == TypeManager.char_type) + if (target_type == TypeManager.char_type) { + if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue)) + return null; return new CharConstant ((char) v); + } if (target_type == TypeManager.decimal_type) return new DecimalConstant ((decimal) v); } - if (expr is UShortConstant){ - ushort v = ((UShortConstant) expr).Value; + if (real_expr is UShortConstant){ + ushort v = ((UShortConstant) real_expr).Value; - if (target_type == TypeManager.byte_type) + if (target_type == TypeManager.byte_type) { + if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue)) + return null; return new ByteConstant ((byte) v); - if (target_type == TypeManager.sbyte_type) + } + if (target_type == TypeManager.sbyte_type) { + if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue)) + return null; return new SByteConstant ((sbyte) v); - if (target_type == TypeManager.short_type) + } + if (target_type == TypeManager.short_type) { + if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue)) + return null; return new ShortConstant ((short) v); + } if (target_type == TypeManager.int32_type) return new IntConstant ((int) v); if (target_type == TypeManager.uint32_type) @@ -1288,50 +1454,89 @@ namespace Mono.CSharp { return new FloatConstant ((float) v); if (target_type == TypeManager.double_type) return new DoubleConstant ((double) v); - if (target_type == TypeManager.char_type) + if (target_type == TypeManager.char_type) { + if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue)) + return null; return new CharConstant ((char) v); + } if (target_type == TypeManager.decimal_type) return new DecimalConstant ((decimal) v); } - if (expr is IntConstant){ - int v = ((IntConstant) expr).Value; + if (real_expr is IntConstant){ + int v = ((IntConstant) real_expr).Value; - if (target_type == TypeManager.byte_type) + if (target_type == TypeManager.byte_type) { + if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue)) + return null; return new ByteConstant ((byte) v); - if (target_type == TypeManager.sbyte_type) + } + if (target_type == TypeManager.sbyte_type) { + if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue)) + return null; return new SByteConstant ((sbyte) v); - if (target_type == TypeManager.short_type) + } + if (target_type == TypeManager.short_type) { + if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue)) + return null; return new ShortConstant ((short) v); - if (target_type == TypeManager.ushort_type) + } + if (target_type == TypeManager.ushort_type) { + if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue)) + return null; return new UShortConstant ((ushort) v); - if (target_type == TypeManager.uint32_type) + } + if (target_type == TypeManager.uint32_type) { + if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue)) + return null; return new UIntConstant ((uint) v); + } if (target_type == TypeManager.int64_type) return new LongConstant ((long) v); - if (target_type == TypeManager.uint64_type) + if (target_type == TypeManager.uint64_type) { + if (!CheckUnsigned (ec, v, target_type)) + return null; return new ULongConstant ((ulong) v); + } if (target_type == TypeManager.float_type) return new FloatConstant ((float) v); if (target_type == TypeManager.double_type) return new DoubleConstant ((double) v); - if (target_type == TypeManager.char_type) + if (target_type == TypeManager.char_type) { + if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue)) + return null; return new CharConstant ((char) v); + } if (target_type == TypeManager.decimal_type) return new DecimalConstant ((decimal) v); } - if (expr is UIntConstant){ - uint v = ((UIntConstant) expr).Value; + if (real_expr is UIntConstant){ + uint v = ((UIntConstant) real_expr).Value; - if (target_type == TypeManager.byte_type) + if (target_type == TypeManager.byte_type) { + if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue)) + return null; return new ByteConstant ((byte) v); - if (target_type == TypeManager.sbyte_type) + } + if (target_type == TypeManager.sbyte_type) { + if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue)) + return null; return new SByteConstant ((sbyte) v); - if (target_type == TypeManager.short_type) + } + if (target_type == TypeManager.short_type) { + if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue)) + return null; return new ShortConstant ((short) v); - if (target_type == TypeManager.ushort_type) + } + if (target_type == TypeManager.ushort_type) { + if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue)) + return null; return new UShortConstant ((ushort) v); - if (target_type == TypeManager.int32_type) + } + if (target_type == TypeManager.int32_type) { + if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue)) + return null; return new IntConstant ((int) v); + } if (target_type == TypeManager.int64_type) return new LongConstant ((long) v); if (target_type == TypeManager.uint64_type) @@ -1340,39 +1545,116 @@ namespace Mono.CSharp { return new FloatConstant ((float) v); if (target_type == TypeManager.double_type) return new DoubleConstant ((double) v); - if (target_type == TypeManager.char_type) + if (target_type == TypeManager.char_type) { + if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue)) + return null; return new CharConstant ((char) v); + } if (target_type == TypeManager.decimal_type) return new DecimalConstant ((decimal) v); } - if (expr is LongConstant){ - long v = ((LongConstant) expr).Value; + if (real_expr is LongConstant){ + long v = ((LongConstant) real_expr).Value; - if (target_type == TypeManager.byte_type) + if (target_type == TypeManager.byte_type) { + if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue)) + return null; return new ByteConstant ((byte) v); - if (target_type == TypeManager.sbyte_type) + } + if (target_type == TypeManager.sbyte_type) { + if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue)) + return null; return new SByteConstant ((sbyte) v); - if (target_type == TypeManager.short_type) + } + if (target_type == TypeManager.short_type) { + if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue)) + return null; return new ShortConstant ((short) v); - if (target_type == TypeManager.ushort_type) + } + if (target_type == TypeManager.ushort_type) { + if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue)) + return null; return new UShortConstant ((ushort) v); - if (target_type == TypeManager.int32_type) + } + if (target_type == TypeManager.int32_type) { + if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue)) + return null; return new IntConstant ((int) v); - if (target_type == TypeManager.uint32_type) + } + if (target_type == TypeManager.uint32_type) { + if (!CheckRange (ec, v, target_type, UInt32.MinValue, UInt32.MaxValue)) + return null; return new UIntConstant ((uint) v); - if (target_type == TypeManager.uint64_type) + } + if (target_type == TypeManager.uint64_type) { + if (!CheckUnsigned (ec, v, target_type)) + return null; return new ULongConstant ((ulong) v); + } if (target_type == TypeManager.float_type) return new FloatConstant ((float) v); if (target_type == TypeManager.double_type) return new DoubleConstant ((double) v); - if (target_type == TypeManager.char_type) + if (target_type == TypeManager.char_type) { + if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue)) + return null; + return new CharConstant ((char) v); + } + if (target_type == TypeManager.decimal_type) + return new DecimalConstant ((decimal) v); + } + if (real_expr is ULongConstant){ + ulong v = ((ULongConstant) real_expr).Value; + + if (target_type == TypeManager.byte_type) { + if (!CheckRange (ec, v, target_type, Byte.MaxValue)) + return null; + return new ByteConstant ((byte) v); + } + if (target_type == TypeManager.sbyte_type) { + if (!CheckRange (ec, v, target_type, (ulong) SByte.MaxValue)) + return null; + return new SByteConstant ((sbyte) v); + } + if (target_type == TypeManager.short_type) { + if (!CheckRange (ec, v, target_type, (ulong) Int16.MaxValue)) + return null; + return new ShortConstant ((short) v); + } + if (target_type == TypeManager.ushort_type) { + if (!CheckRange (ec, v, target_type, UInt16.MaxValue)) + return null; + return new UShortConstant ((ushort) v); + } + if (target_type == TypeManager.int32_type) { + if (!CheckRange (ec, v, target_type, Int32.MaxValue)) + return null; + return new IntConstant ((int) v); + } + if (target_type == TypeManager.uint32_type) { + if (!CheckRange (ec, v, target_type, UInt32.MaxValue)) + return null; + return new UIntConstant ((uint) v); + } + if (target_type == TypeManager.int64_type) { + if (!CheckRange (ec, v, target_type, (ulong) Int64.MaxValue)) + return null; + return new LongConstant ((long) v); + } + if (target_type == TypeManager.float_type) + return new FloatConstant ((float) v); + if (target_type == TypeManager.double_type) + return new DoubleConstant ((double) v); + if (target_type == TypeManager.char_type) { + if (!CheckRange (ec, v, target_type, Char.MaxValue)) + return null; return new CharConstant ((char) v); + } if (target_type == TypeManager.decimal_type) return new DecimalConstant ((decimal) v); } - if (expr is ULongConstant){ - ulong v = ((ULongConstant) expr).Value; + if (real_expr is FloatConstant){ + float v = ((FloatConstant) real_expr).Value; if (target_type == TypeManager.byte_type) return new ByteConstant ((byte) v); @@ -1388,8 +1670,8 @@ namespace Mono.CSharp { return new UIntConstant ((uint) v); if (target_type == TypeManager.int64_type) return new LongConstant ((long) v); - if (target_type == TypeManager.float_type) - return new FloatConstant ((float) v); + if (target_type == TypeManager.uint64_type) + return new ULongConstant ((ulong) v); if (target_type == TypeManager.double_type) return new DoubleConstant ((double) v); if (target_type == TypeManager.char_type) @@ -1397,8 +1679,8 @@ namespace Mono.CSharp { if (target_type == TypeManager.decimal_type) return new DecimalConstant ((decimal) v); } - if (expr is FloatConstant){ - float v = ((FloatConstant) expr).Value; + if (real_expr is DoubleConstant){ + double v = ((DoubleConstant) real_expr).Value; if (target_type == TypeManager.byte_type) return new ByteConstant ((byte) v); @@ -1416,24 +1698,32 @@ namespace Mono.CSharp { return new LongConstant ((long) v); if (target_type == TypeManager.uint64_type) return new ULongConstant ((ulong) v); - if (target_type == TypeManager.double_type) - return new DoubleConstant ((double) v); + if (target_type == TypeManager.float_type) + return new FloatConstant ((float) v); if (target_type == TypeManager.char_type) return new CharConstant ((char) v); if (target_type == TypeManager.decimal_type) return new DecimalConstant ((decimal) v); } - if (expr is DoubleConstant){ - double v = ((DoubleConstant) expr).Value; - - if (target_type == TypeManager.byte_type) + + if (real_expr is CharConstant){ + char v = ((CharConstant) real_expr).Value; + + if (target_type == TypeManager.byte_type) { + if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue)) + return null; return new ByteConstant ((byte) v); - if (target_type == TypeManager.sbyte_type) + } + if (target_type == TypeManager.sbyte_type) { + if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue)) + return null; return new SByteConstant ((sbyte) v); - if (target_type == TypeManager.short_type) + } + if (target_type == TypeManager.short_type) { + if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue)) + return null; return new ShortConstant ((short) v); - if (target_type == TypeManager.ushort_type) - return new UShortConstant ((ushort) v); + } if (target_type == TypeManager.int32_type) return new IntConstant ((int) v); if (target_type == TypeManager.uint32_type) @@ -1444,8 +1734,13 @@ namespace Mono.CSharp { return new ULongConstant ((ulong) v); if (target_type == TypeManager.float_type) return new FloatConstant ((float) v); - if (target_type == TypeManager.char_type) + if (target_type == TypeManager.double_type) + return new DoubleConstant ((double) v); + if (target_type == TypeManager.char_type) { + if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue)) + return null; return new CharConstant ((char) v); + } if (target_type == TypeManager.decimal_type) return new DecimalConstant ((decimal) v); } @@ -1467,15 +1762,15 @@ namespace Mono.CSharp { return null; eclass = ExprClass.Value; - + if (expr is Constant){ Expression e = TryReduce (ec, type); if (e != null) return e; } - - expr = ConvertExplicit (ec, expr, type, loc); + + expr = Convert.ExplicitConversion (ec, expr, type, loc); return expr; } @@ -1519,8 +1814,8 @@ namespace Mono.CSharp { bool DelegateOperation; // This must be kept in sync with Operator!!! - static string [] oper_names; - + public static readonly string [] oper_names; + static Binary () { oper_names = new string [(int) Operator.TOP]; @@ -1639,7 +1934,7 @@ namespace Mono.CSharp { if (expr.Type == target_type) return expr; - return ConvertImplicit (ec, expr, target_type, new Location (-1)); + return Convert.ImplicitConversion (ec, expr, target_type, loc); } public static void Error_OperatorAmbiguous (Location loc, Operator oper, Type l, Type r) @@ -1652,34 +1947,55 @@ namespace Mono.CSharp { + "'"); } + bool IsOfType (EmitContext ec, Type l, Type r, Type t, bool check_user_conversions) + { + if ((l == t) || (r == t)) + return true; + + if (!check_user_conversions) + return false; + + if (Convert.ImplicitUserConversionExists (ec, l, t)) + return true; + else if (Convert.ImplicitUserConversionExists (ec, r, t)) + return true; + else + return false; + } + // // Note that handling the case l == Decimal || r == Decimal // is taken care of by the Step 1 Operator Overload resolution. // - bool DoNumericPromotions (EmitContext ec, Type l, Type r) - { - if (l == TypeManager.double_type || r == TypeManager.double_type){ + // If `check_user_conv' is true, we also check whether a user-defined conversion + // exists. Note that we only need to do this if both arguments are of a user-defined + // type, otherwise ConvertImplict() already finds the user-defined conversion for us, + // so we don't explicitly check for performance reasons. + // + bool DoNumericPromotions (EmitContext ec, Type l, Type r, bool check_user_conv) + { + if (IsOfType (ec, l, r, TypeManager.double_type, check_user_conv)){ // // If either operand is of type double, the other operand is // conveted to type double. // if (r != TypeManager.double_type) - right = ConvertImplicit (ec, right, TypeManager.double_type, loc); + right = Convert.ImplicitConversion (ec, right, TypeManager.double_type, loc); if (l != TypeManager.double_type) - left = ConvertImplicit (ec, left, TypeManager.double_type, loc); + left = Convert.ImplicitConversion (ec, left, TypeManager.double_type, loc); type = TypeManager.double_type; - } else if (l == TypeManager.float_type || r == TypeManager.float_type){ + } else if (IsOfType (ec, l, r, TypeManager.float_type, check_user_conv)){ // // if either operand is of type float, the other operand is // converted to type float. // if (r != TypeManager.double_type) - right = ConvertImplicit (ec, right, TypeManager.float_type, loc); + right = Convert.ImplicitConversion (ec, right, TypeManager.float_type, loc); if (l != TypeManager.double_type) - left = ConvertImplicit (ec, left, TypeManager.float_type, loc); + left = Convert.ImplicitConversion (ec, left, TypeManager.float_type, loc); type = TypeManager.float_type; - } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){ + } else if (IsOfType (ec, l, r, TypeManager.uint64_type, check_user_conv)){ Expression e; Type other; // @@ -1692,7 +2008,7 @@ namespace Mono.CSharp { if (right is IntConstant){ IntConstant ic = (IntConstant) right; - e = TryImplicitIntConversion (l, ic); + e = Convert.TryImplicitIntConversion (l, ic); if (e != null) right = e; } else if (right is LongConstant){ @@ -1701,7 +2017,7 @@ namespace Mono.CSharp { if (ll > 0) right = new ULongConstant ((ulong) ll); } else { - e = ImplicitNumericConversion (ec, right, l, loc); + e = Convert.ImplicitNumericConversion (ec, right, l, loc); if (e != null) right = e; } @@ -1709,7 +2025,7 @@ namespace Mono.CSharp { other = right.Type; } else { if (left is IntConstant){ - e = TryImplicitIntConversion (r, (IntConstant) left); + e = Convert.TryImplicitIntConversion (r, (IntConstant) left); if (e != null) left = e; } else if (left is LongConstant){ @@ -1718,7 +2034,7 @@ namespace Mono.CSharp { if (ll > 0) left = new ULongConstant ((ulong) ll); } else { - e = ImplicitNumericConversion (ec, left, r, loc); + e = Convert.ImplicitNumericConversion (ec, left, r, loc); if (e != null) left = e; } @@ -1731,18 +2047,18 @@ namespace Mono.CSharp { (other == TypeManager.int64_type)) Error_OperatorAmbiguous (loc, oper, l, r); type = TypeManager.uint64_type; - } else if (l == TypeManager.int64_type || r == TypeManager.int64_type){ + } else if (IsOfType (ec, l, r, TypeManager.int64_type, check_user_conv)){ // // If either operand is of type long, the other operand is converted // to type long. // if (l != TypeManager.int64_type) - left = ConvertImplicit (ec, left, TypeManager.int64_type, loc); + left = Convert.ImplicitConversion (ec, left, TypeManager.int64_type, loc); if (r != TypeManager.int64_type) - right = ConvertImplicit (ec, right, TypeManager.int64_type, loc); + right = Convert.ImplicitConversion (ec, right, TypeManager.int64_type, loc); type = TypeManager.int64_type; - } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){ + } else if (IsOfType (ec, l, r, TypeManager.uint32_type, check_user_conv)){ // // If either operand is of type uint, and the other // operand is of type sbyte, short or int, othe operands are @@ -1795,10 +2111,10 @@ namespace Mono.CSharp { } } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){ if (l != TypeManager.decimal_type) - left = ConvertImplicit (ec, left, TypeManager.decimal_type, loc); + left = Convert.ImplicitConversion (ec, left, TypeManager.decimal_type, loc); if (r != TypeManager.decimal_type) - right = ConvertImplicit (ec, right, TypeManager.decimal_type, loc); + right = Convert.ImplicitConversion (ec, right, TypeManager.decimal_type, loc); type = TypeManager.decimal_type; } else { left = ForceConversion (ec, left, TypeManager.int32_type); @@ -1834,6 +2150,15 @@ namespace Mono.CSharp { return (t == TypeManager.uint32_type || t == TypeManager.uint64_type || t == TypeManager.short_type || t == TypeManager.byte_type); } + + static bool is_user_defined (Type t) + { + if (t.IsSubclassOf (TypeManager.value_type) && + (!TypeManager.IsBuiltinType (t) || t == TypeManager.decimal_type)) + return true; + else + return false; + } Expression CheckShiftArguments (EmitContext ec) { @@ -1848,10 +2173,10 @@ namespace Mono.CSharp { } right = e; - if (((e = ConvertImplicit (ec, left, TypeManager.int32_type, loc)) != null) || - ((e = ConvertImplicit (ec, left, TypeManager.uint32_type, loc)) != null) || - ((e = ConvertImplicit (ec, left, TypeManager.int64_type, loc)) != null) || - ((e = ConvertImplicit (ec, left, TypeManager.uint64_type, loc)) != null)){ + if (((e = Convert.ImplicitConversion (ec, left, TypeManager.int32_type, loc)) != null) || + ((e = Convert.ImplicitConversion (ec, left, TypeManager.uint32_type, loc)) != null) || + ((e = Convert.ImplicitConversion (ec, left, TypeManager.int64_type, loc)) != null) || + ((e = Convert.ImplicitConversion (ec, left, TypeManager.uint64_type, loc)) != null)){ left = e; type = e.Type; @@ -1869,36 +2194,54 @@ namespace Mono.CSharp { bool overload_failed = false; // - // Step 1: Perform Operator Overload location + // Special cases: string comapred to null // - Expression left_expr, right_expr; - - string op = oper_names [(int) oper]; + if (oper == Operator.Equality || oper == Operator.Inequality){ + if ((l == TypeManager.string_type && (right is NullLiteral)) || + (r == TypeManager.string_type && (left is NullLiteral))){ + Type = TypeManager.bool_type; + + return this; + } + } + + // + // Do not perform operator overload resolution when both sides are + // built-in types + // + if (!(TypeManager.IsCLRType (l) && TypeManager.IsCLRType (r))){ + // + // Step 1: Perform Operator Overload location + // + Expression left_expr, right_expr; - MethodGroupExpr union; - left_expr = MemberLookup (ec, l, op, MemberTypes.Method, AllBindingFlags, loc); - if (r != l){ - right_expr = MemberLookup ( - ec, r, op, MemberTypes.Method, AllBindingFlags, loc); - union = Invocation.MakeUnionSet (left_expr, right_expr, loc); - } else - union = (MethodGroupExpr) left_expr; + string op = oper_names [(int) oper]; - if (union != null) { - Arguments = new ArrayList (); - Arguments.Add (new Argument (left, Argument.AType.Expression)); - Arguments.Add (new Argument (right, Argument.AType.Expression)); + MethodGroupExpr union; + left_expr = MemberLookup (ec, l, op, MemberTypes.Method, AllBindingFlags, loc); + if (r != l){ + right_expr = MemberLookup ( + ec, r, op, MemberTypes.Method, AllBindingFlags, loc); + union = Invocation.MakeUnionSet (left_expr, right_expr, loc); + } else + union = (MethodGroupExpr) left_expr; - method = Invocation.OverloadResolve (ec, union, Arguments, Location.Null); - if (method != null) { - MethodInfo mi = (MethodInfo) method; + if (union != null) { + Arguments = new ArrayList (); + Arguments.Add (new Argument (left, Argument.AType.Expression)); + Arguments.Add (new Argument (right, Argument.AType.Expression)); - type = mi.ReturnType; - return this; - } else { - overload_failed = true; + method = Invocation.OverloadResolve (ec, union, Arguments, Location.Null); + if (method != null) { + MethodInfo mi = (MethodInfo) method; + + type = mi.ReturnType; + return this; + } else { + overload_failed = true; + } } - } + } // // Step 2: Default operations on CLI native types. @@ -1927,14 +2270,45 @@ namespace Mono.CSharp { return new StringConstant ( ls.Value + rs.Value); } - + + if (left is Binary){ + Binary b = (Binary) left; + + // + // Call String.Concat (string, string, string) or + // String.Concat (string, string, string, string) + // if possible. + // + if (b.oper == Operator.Addition && + (b.method == TypeManager.string_concat_string_string_string || + b.method == TypeManager.string_concat_string_string_string_string)){ + ArrayList bargs = b.Arguments; + int count = bargs.Count; + + if (count == 2){ + Arguments = bargs; + Arguments.Add (new Argument (right, Argument.AType.Expression)); + type = TypeManager.string_type; + method = TypeManager.string_concat_string_string_string; + + return this; + } else if (count == 3){ + Arguments = bargs; + Arguments.Add (new Argument (right, Argument.AType.Expression)); + type = TypeManager.string_type; + method = TypeManager.string_concat_string_string_string_string; + return this; + } + } + } + // string + string method = TypeManager.string_concat_string_string; } else { // string + object method = TypeManager.string_concat_object_object; - right = ConvertImplicit (ec, right, - TypeManager.object_type, loc); + right = Convert.ImplicitConversion ( + ec, right, TypeManager.object_type, loc); if (right == null){ Error_OperatorCannotBeApplied (loc, OperName (oper), l, r); return null; @@ -1957,7 +2331,7 @@ namespace Mono.CSharp { } method = TypeManager.string_concat_object_object; - left = ConvertImplicit (ec, left, TypeManager.object_type, loc); + left = Convert.ImplicitConversion (ec, left, TypeManager.object_type, loc); if (left == null){ Error_OperatorCannotBeApplied (loc, OperName (oper), l, r); return null; @@ -2018,8 +2392,8 @@ namespace Mono.CSharp { // // Also, a standard conversion must exist from either one // - if (!(StandardConversionExists (left, r) || - StandardConversionExists (right, l))){ + if (!(Convert.ImplicitStandardConversionExists (left, r) || + Convert.ImplicitStandardConversionExists (right, l))){ Error_OperatorCannotBeApplied (); return null; } @@ -2144,7 +2518,7 @@ namespace Mono.CSharp { } if (!rie){ - temp = ConvertImplicit (ec, right, l, loc); + temp = Convert.ImplicitConversion (ec, right, l, loc); if (temp != null) right = temp; else { @@ -2152,7 +2526,7 @@ namespace Mono.CSharp { return null; } } if (!lie){ - temp = ConvertImplicit (ec, left, r, loc); + temp = Convert.ImplicitConversion (ec, left, r, loc); if (temp != null){ left = temp; l = r; @@ -2165,6 +2539,10 @@ namespace Mono.CSharp { if (oper == Operator.Equality || oper == Operator.Inequality || oper == Operator.LessThanOrEqual || oper == Operator.LessThan || oper == Operator.GreaterThanOrEqual || oper == Operator.GreaterThan){ + if (left.Type != right.Type){ + Error_OperatorCannotBeApplied (); + return null; + } type = TypeManager.bool_type; return this; } @@ -2229,7 +2607,8 @@ namespace Mono.CSharp { // // This will leave left or right set to null if there is an error // - DoNumericPromotions (ec, l, r); + bool check_user_conv = is_user_defined (l) && is_user_defined (r); + DoNumericPromotions (ec, l, r, check_user_conv); if (left == null || right == null){ Error_OperatorCannotBeApplied (loc, OperName (oper), l, r); return null; @@ -2276,22 +2655,16 @@ namespace Mono.CSharp { if (left == null || right == null) return null; - if (left.Type == null) - throw new Exception ( - "Resolve returned non null, but did not set the type! (" + - left + ") at Line: " + loc.Row); - if (right.Type == null) - throw new Exception ( - "Resolve returned non null, but did not set the type! (" + - right + ") at Line: "+ loc.Row); - eclass = ExprClass.Value; - if (left is Constant && right is Constant){ + Constant rc = right as Constant; + Constant lc = left as Constant; + + if (rc != null & lc != null){ Expression e = ConstantFold.BinaryFold ( - ec, oper, (Constant) left, (Constant) right, loc); - if (e != null) - return e; + ec, oper, lc, rc, loc); + if (e != null) + return e; } return ResolveOperator (ec); @@ -2320,7 +2693,7 @@ namespace Mono.CSharp { // if (oper == Operator.Equality || oper == Operator.Inequality){ bool my_on_true = oper == Operator.Inequality ? onTrue : !onTrue; - + if (left is NullLiteral){ right.Emit (ec); if (my_on_true) @@ -2335,19 +2708,118 @@ namespace Mono.CSharp { else ig.Emit (OpCodes.Brfalse, target); return true; - } - } else if (!(oper == Operator.LessThan || - oper == Operator.GreaterThan || - oper == Operator.LessThanOrEqual || - oper == Operator.GreaterThanOrEqual)) + } else if (left is BoolConstant){ + right.Emit (ec); + if (my_on_true != ((BoolConstant) left).Value) + ig.Emit (OpCodes.Brtrue, target); + else + ig.Emit (OpCodes.Brfalse, target); + return true; + } else if (right is BoolConstant){ + left.Emit (ec); + if (my_on_true != ((BoolConstant) right).Value) + ig.Emit (OpCodes.Brtrue, target); + else + ig.Emit (OpCodes.Brfalse, target); + return true; + } + + } else if (oper == Operator.LogicalAnd){ + if (left is Binary){ + Binary left_binary = (Binary) left; + + if (onTrue){ + Label tests_end = ig.DefineLabel (); + + if (left_binary.EmitBranchable (ec, tests_end, false)){ + if (right is Binary){ + Binary right_binary = (Binary) right; + + if (right_binary.EmitBranchable (ec, target, true)){ + ig.MarkLabel (tests_end); + return true; + } + } + right.Emit (ec); + ig.Emit (OpCodes.Brtrue, target); + ig.MarkLabel (tests_end); + return true; + } + } else { + if (left_binary.EmitBranchable (ec, target, false)){ + if (right is Binary){ + Binary right_binary = (Binary) right; + + if (right_binary.EmitBranchable (ec, target, false)) + return true; + } + right.Emit (ec); + if (onTrue) + ig.Emit (OpCodes.Brtrue, target); + else + ig.Emit (OpCodes.Brfalse, target); + return true; + } + } + // + // Give up, and let the regular Emit work, but we could + // also optimize the left-non-Branchable, but-right-Branchable + // + } return false; - + } else if (oper == Operator.LogicalOr){ + if (left is Binary){ + Binary left_binary = (Binary) left; + if (onTrue){ + if (left_binary.EmitBranchable (ec, target, true)){ + if (right is Binary){ + Binary right_binary = (Binary) right; + + if (right_binary.EmitBranchable (ec, target, true)) + return true; + } + right.Emit (ec); + ig.Emit (OpCodes.Brtrue, target); + return true; + } + + // + // Give up, and let the regular Emit work, but we could + // also optimize the left-non-Branchable, but-right-Branchable + // + } else { + Label tests_end = ig.DefineLabel (); + + if (left_binary.EmitBranchable (ec, tests_end, true)){ + if (right is Binary){ + Binary right_binary = (Binary) right; + + if (right_binary.EmitBranchable (ec, target, false)){ + ig.MarkLabel (tests_end); + return true; + } + } + right.Emit (ec); + ig.Emit (OpCodes.Brfalse, target); + ig.MarkLabel (tests_end); + return true; + } + } + } + + return false; + } else if (!(oper == Operator.LessThan || + oper == Operator.GreaterThan || + oper == Operator.LessThanOrEqual || + oper == Operator.GreaterThanOrEqual)) + return false; left.Emit (ec); right.Emit (ec); - bool isUnsigned = is_unsigned (left.Type); + Type t = left.Type; + bool isUnsigned = is_unsigned (t); switch (oper){ case Operator.Equality: @@ -2391,6 +2863,9 @@ namespace Mono.CSharp { break; case Operator.LessThanOrEqual: + if (t == TypeManager.double_type || t == TypeManager.float_type) + isUnsigned = true; + if (onTrue) if (isUnsigned) ig.Emit (OpCodes.Ble_Un, target); @@ -2405,6 +2880,8 @@ namespace Mono.CSharp { case Operator.GreaterThanOrEqual: + if (t == TypeManager.double_type || t == TypeManager.float_type) + isUnsigned = true; if (onTrue) if (isUnsigned) ig.Emit (OpCodes.Bge_Un, target); @@ -2456,11 +2933,24 @@ namespace Mono.CSharp { if (oper == Operator.LogicalAnd){ Label load_zero = ig.DefineLabel (); Label end = ig.DefineLabel (); - - left.Emit (ec); - ig.Emit (OpCodes.Brfalse, load_zero); - right.Emit (ec); - ig.Emit (OpCodes.Br, end); + bool process = true; + + if (left is Binary){ + Binary left_binary = (Binary) left; + + if (left_binary.EmitBranchable (ec, load_zero, false)){ + right.Emit (ec); + ig.Emit (OpCodes.Br, end); + process = false; + } + } + + if (process){ + left.Emit (ec); + ig.Emit (OpCodes.Brfalse, load_zero); + right.Emit (ec); + ig.Emit (OpCodes.Br, end); + } ig.MarkLabel (load_zero); ig.Emit (OpCodes.Ldc_I4_0); ig.MarkLabel (end); @@ -2468,11 +2958,24 @@ namespace Mono.CSharp { } else if (oper == Operator.LogicalOr){ Label load_one = ig.DefineLabel (); Label end = ig.DefineLabel (); + bool process = true; - left.Emit (ec); - ig.Emit (OpCodes.Brtrue, load_one); - right.Emit (ec); - ig.Emit (OpCodes.Br, end); + if (left is Binary){ + Binary left_binary = (Binary) left; + + if (left_binary.EmitBranchable (ec, load_one, true)){ + right.Emit (ec); + ig.Emit (OpCodes.Br, end); + process = false; + } + } + + if (process){ + left.Emit (ec); + ig.Emit (OpCodes.Brtrue, load_one); + right.Emit (ec); + ig.Emit (OpCodes.Br, end); + } ig.MarkLabel (load_one); ig.Emit (OpCodes.Ldc_I4_1); ig.MarkLabel (end); @@ -2482,12 +2985,14 @@ namespace Mono.CSharp { left.Emit (ec); right.Emit (ec); + bool isUnsigned = is_unsigned (left.Type); + switch (oper){ case Operator.Multiply: if (ec.CheckState){ if (l == TypeManager.int32_type || l == TypeManager.int64_type) opcode = OpCodes.Mul_Ovf; - else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type) + else if (isUnsigned) opcode = OpCodes.Mul_Ovf_Un; else opcode = OpCodes.Mul; @@ -2497,14 +3002,14 @@ namespace Mono.CSharp { break; case Operator.Division: - if (l == TypeManager.uint32_type || l == TypeManager.uint64_type) + if (isUnsigned) opcode = OpCodes.Div_Un; else opcode = OpCodes.Div; break; case Operator.Modulus: - if (l == TypeManager.uint32_type || l == TypeManager.uint64_type) + if (isUnsigned) opcode = OpCodes.Rem_Un; else opcode = OpCodes.Rem; @@ -2514,7 +3019,7 @@ namespace Mono.CSharp { if (ec.CheckState){ if (l == TypeManager.int32_type || l == TypeManager.int64_type) opcode = OpCodes.Add_Ovf; - else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type) + else if (isUnsigned) opcode = OpCodes.Add_Ovf_Un; else opcode = OpCodes.Add; @@ -2526,7 +3031,7 @@ namespace Mono.CSharp { if (ec.CheckState){ if (l == TypeManager.int32_type || l == TypeManager.int64_type) opcode = OpCodes.Sub_Ovf; - else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type) + else if (isUnsigned) opcode = OpCodes.Sub_Ovf_Un; else opcode = OpCodes.Sub; @@ -2535,7 +3040,7 @@ namespace Mono.CSharp { break; case Operator.RightShift: - if (l == TypeManager.uint32_type || l == TypeManager.uint64_type) + if (isUnsigned) opcode = OpCodes.Shr_Un; else opcode = OpCodes.Shr; @@ -2550,30 +3055,47 @@ namespace Mono.CSharp { break; case Operator.Inequality: - ec.ig.Emit (OpCodes.Ceq); - ec.ig.Emit (OpCodes.Ldc_I4_0); + ig.Emit (OpCodes.Ceq); + ig.Emit (OpCodes.Ldc_I4_0); opcode = OpCodes.Ceq; break; case Operator.LessThan: - opcode = OpCodes.Clt; + if (isUnsigned) + opcode = OpCodes.Clt_Un; + else + opcode = OpCodes.Clt; break; case Operator.GreaterThan: - opcode = OpCodes.Cgt; + if (isUnsigned) + opcode = OpCodes.Cgt_Un; + else + opcode = OpCodes.Cgt; break; case Operator.LessThanOrEqual: - ec.ig.Emit (OpCodes.Cgt); - ec.ig.Emit (OpCodes.Ldc_I4_0); + Type lt = left.Type; + + if (isUnsigned || (lt == TypeManager.double_type || lt == TypeManager.float_type)) + ig.Emit (OpCodes.Cgt_Un); + else + ig.Emit (OpCodes.Cgt); + ig.Emit (OpCodes.Ldc_I4_0); opcode = OpCodes.Ceq; break; case Operator.GreaterThanOrEqual: - ec.ig.Emit (OpCodes.Clt); - ec.ig.Emit (OpCodes.Ldc_I4_1); + Type le = left.Type; + + if (isUnsigned || (le == TypeManager.double_type || le == TypeManager.float_type)) + ig.Emit (OpCodes.Clt_Un); + else + ig.Emit (OpCodes.Clt); + + ig.Emit (OpCodes.Ldc_I4_1); opcode = OpCodes.Sub; break; @@ -2612,8 +3134,7 @@ namespace Mono.CSharp { // // We assume that `l' is always a pointer // - public PointerArithmetic (bool is_addition, Expression l, Expression r, Type t, - Location loc) + public PointerArithmetic (bool is_addition, Expression l, Expression r, Type t, Location loc) { type = t; eclass = ExprClass.Variable; @@ -2636,8 +3157,9 @@ namespace Mono.CSharp { Type op_type = left.Type; ILGenerator ig = ec.ig; int size = GetTypeSize (op_type.GetElementType ()); + Type rtype = right.Type; - if (right.Type.IsPointer){ + if (rtype.IsPointer){ // // handle (pointer - pointer) // @@ -2665,7 +3187,12 @@ namespace Mono.CSharp { ig.Emit (OpCodes.Sizeof, op_type); else IntLiteral.EmitInt (ig, size); + if (rtype == TypeManager.int64_type) + ig.Emit (OpCodes.Conv_I8); + else if (rtype == TypeManager.uint64_type) + ig.Emit (OpCodes.Conv_U8); ig.Emit (OpCodes.Mul); + ig.Emit (OpCodes.Conv_I); } if (is_add) ig.Emit (OpCodes.Add); @@ -2714,9 +3241,13 @@ namespace Mono.CSharp { if (expr == null) return null; - if (expr.Type != TypeManager.bool_type) - expr = Expression.ConvertImplicitRequired ( - ec, expr, TypeManager.bool_type, loc); + if (expr.Type != TypeManager.bool_type){ + expr = Expression.ResolveBoolean ( + ec, expr, loc); + + if (expr == null) + return null; + } trueExpr = trueExpr.Resolve (ec); falseExpr = falseExpr.Resolve (ec); @@ -2744,12 +3275,12 @@ namespace Mono.CSharp { // First, if an implicit conversion exists from trueExpr // to falseExpr, then the result type is of type falseExpr.Type // - conv = ConvertImplicit (ec, trueExpr, false_type, loc); + conv = Convert.ImplicitConversion (ec, trueExpr, false_type, loc); if (conv != null){ // // Check if both can convert implicitl to each other's type // - if (ConvertImplicit (ec, falseExpr, true_type, loc) != null){ + if (Convert.ImplicitConversion (ec, falseExpr, true_type, loc) != null){ Error (172, "Can not compute type of conditional expression " + "as `" + TypeManager.CSharpName (trueExpr.Type) + @@ -2759,7 +3290,7 @@ namespace Mono.CSharp { } type = false_type; trueExpr = conv; - } else if ((conv = ConvertImplicit(ec, falseExpr, true_type,loc))!= null){ + } else if ((conv = Convert.ImplicitConversion(ec, falseExpr, true_type,loc))!= null){ type = true_type; falseExpr = conv; } else { @@ -2805,6 +3336,7 @@ namespace Mono.CSharp { public class LocalVariableReference : Expression, IAssignMethod, IMemoryLocation, IVariable { public readonly string Name; public readonly Block Block; + LocalInfo local_info; VariableInfo variable_info; bool is_readonly; @@ -2819,77 +3351,57 @@ namespace Mono.CSharp { // Setting `is_readonly' to false will allow you to create a writable // reference to a read-only variable. This is used by foreach and using. public LocalVariableReference (Block block, string name, Location l, - VariableInfo variable_info, bool is_readonly) + LocalInfo local_info, bool is_readonly) : this (block, name, l) { - this.variable_info = variable_info; + this.local_info = local_info; this.is_readonly = is_readonly; } public VariableInfo VariableInfo { - get { - if (variable_info == null) { - variable_info = Block.GetVariableInfo (Name); - is_readonly = variable_info.ReadOnly; - } - return variable_info; - } - } - - public bool IsAssigned (EmitContext ec, Location loc) - { - return VariableInfo.IsAssigned (ec, loc); - } - - public bool IsFieldAssigned (EmitContext ec, string name, Location loc) - { - return VariableInfo.IsFieldAssigned (ec, name, loc); - } - - public void SetAssigned (EmitContext ec) - { - VariableInfo.SetAssigned (ec); - } - - public void SetFieldAssigned (EmitContext ec, string name) - { - VariableInfo.SetFieldAssigned (ec, name); + get { return variable_info; } } public bool IsReadOnly { get { - if (variable_info == null) { - variable_info = Block.GetVariableInfo (Name); - is_readonly = variable_info.ReadOnly; - } return is_readonly; } } + + protected void DoResolveBase (EmitContext ec) + { + if (local_info == null) { + local_info = Block.GetLocalInfo (Name); + is_readonly = local_info.ReadOnly; + } + + variable_info = Block.GetVariableInfo (local_info); + type = local_info.VariableType; + } public override Expression DoResolve (EmitContext ec) { - VariableInfo vi = VariableInfo; + DoResolveBase (ec); - if (Block.IsConstant (Name)) { - Expression e = Block.GetConstantExpression (Name); - - vi.Used = true; + Expression e = Block.GetConstantExpression (Name); + if (e != null) { + local_info.Used = true; + eclass = ExprClass.Value; return e; } - if (ec.DoFlowAnalysis && !IsAssigned (ec, loc)) + if ((variable_info != null) && !variable_info.IsAssigned (ec, loc)) return null; - type = vi.VariableType; return this; } override public Expression DoResolveLValue (EmitContext ec, Expression right_side) { - VariableInfo vi = VariableInfo; + DoResolveBase (ec); - if (ec.DoFlowAnalysis) - ec.SetVariableAssigned (vi); + if (variable_info != null) + variable_info.SetAssigned (ec); Expression e = DoResolve (ec); @@ -2906,30 +3418,40 @@ namespace Mono.CSharp { public override void Emit (EmitContext ec) { - VariableInfo vi = VariableInfo; ILGenerator ig = ec.ig; - ig.Emit (OpCodes.Ldloc, vi.LocalBuilder); - vi.Used = true; + if (local_info.LocalBuilder == null){ + ec.EmitThis (); + ig.Emit (OpCodes.Ldfld, local_info.FieldBuilder); + } else + ig.Emit (OpCodes.Ldloc, local_info.LocalBuilder); + + local_info.Used = true; } public void EmitAssign (EmitContext ec, Expression source) { ILGenerator ig = ec.ig; - VariableInfo vi = VariableInfo; - vi.Assigned = true; + local_info.Assigned = true; - source.Emit (ec); - - ig.Emit (OpCodes.Stloc, vi.LocalBuilder); + if (local_info.LocalBuilder == null){ + ec.EmitThis (); + source.Emit (ec); + ig.Emit (OpCodes.Stfld, local_info.FieldBuilder); + } else { + source.Emit (ec); + ig.Emit (OpCodes.Stloc, local_info.LocalBuilder); + } } public void AddressOf (EmitContext ec, AddressOp mode) { - VariableInfo vi = VariableInfo; - - ec.ig.Emit (OpCodes.Ldloca, vi.LocalBuilder); + if (local_info.LocalBuilder == null){ + ec.EmitThis (); + ec.ig.Emit (OpCodes.Ldflda, local_info.FieldBuilder); + } else + ec.ig.Emit (OpCodes.Ldloca, local_info.LocalBuilder); } } @@ -2941,59 +3463,68 @@ namespace Mono.CSharp { Parameters pars; String name; int idx; + Block block; + VariableInfo vi; public Parameter.Modifier mod; public bool is_ref, is_out; - public ParameterReference (Parameters pars, int idx, string name, Location loc) + public ParameterReference (Parameters pars, Block block, int idx, string name, Location loc) { this.pars = pars; + this.block = block; this.idx = idx; this.name = name; this.loc = loc; eclass = ExprClass.Variable; } + public VariableInfo VariableInfo { + get { return vi; } + } + public bool IsAssigned (EmitContext ec, Location loc) { - if (!is_out || !ec.DoFlowAnalysis) + if (!ec.DoFlowAnalysis || !is_out || + ec.CurrentBranching.IsAssigned (vi)) return true; - if (!ec.CurrentBranching.IsParameterAssigned (idx)) { - Report.Error (165, loc, - "Use of unassigned local variable `" + name + "'"); - return false; - } - - return true; + Report.Error (165, loc, + "Use of unassigned parameter `" + name + "'"); + return false; } public bool IsFieldAssigned (EmitContext ec, string field_name, Location loc) { - if (!is_out || !ec.DoFlowAnalysis) - return true; - - if (ec.CurrentBranching.IsParameterAssigned (idx)) + if (!ec.DoFlowAnalysis || !is_out || + ec.CurrentBranching.IsFieldAssigned (vi, field_name)) return true; - if (!ec.CurrentBranching.IsParameterAssigned (idx, field_name)) { - Report.Error (170, loc, - "Use of possibly unassigned field `" + field_name + "'"); - return false; - } - - return true; + Report.Error (170, loc, + "Use of possibly unassigned field `" + field_name + "'"); + return false; } public void SetAssigned (EmitContext ec) { if (is_out && ec.DoFlowAnalysis) - ec.CurrentBranching.SetParameterAssigned (idx); + ec.CurrentBranching.SetAssigned (vi); } public void SetFieldAssigned (EmitContext ec, string field_name) { if (is_out && ec.DoFlowAnalysis) - ec.CurrentBranching.SetParameterAssigned (idx, field_name); + ec.CurrentBranching.SetFieldAssigned (vi, field_name); + } + + protected void DoResolveBase (EmitContext ec) + { + type = pars.GetParameterInfo (ec.DeclSpace, idx, out mod); + is_ref = (mod & Parameter.Modifier.ISBYREF) != 0; + is_out = (mod & Parameter.Modifier.OUT) != 0; + eclass = ExprClass.Variable; + + if (is_out) + vi = block.ParameterMap [idx]; } // @@ -3010,10 +3541,7 @@ namespace Mono.CSharp { // public override Expression DoResolve (EmitContext ec) { - type = pars.GetParameterInfo (ec.DeclSpace, idx, out mod); - is_ref = (mod & Parameter.Modifier.ISBYREF) != 0; - is_out = (mod & Parameter.Modifier.OUT) != 0; - eclass = ExprClass.Variable; + DoResolveBase (ec); if (is_out && ec.DoFlowAnalysis && !IsAssigned (ec, loc)) return null; @@ -3023,18 +3551,14 @@ namespace Mono.CSharp { override public Expression DoResolveLValue (EmitContext ec, Expression right_side) { - type = pars.GetParameterInfo (ec.DeclSpace, idx, out mod); - is_ref = (mod & Parameter.Modifier.ISBYREF) != 0; - is_out = (mod & Parameter.Modifier.OUT) != 0; - eclass = ExprClass.Variable; + DoResolveBase (ec); - if (is_out && ec.DoFlowAnalysis) - ec.SetParameterAssigned (idx); + SetAssigned (ec); return this; } - static void EmitLdArg (ILGenerator ig, int x) + static public void EmitLdArg (ILGenerator ig, int x) { if (x <= 255){ switch (x){ @@ -3068,6 +3592,13 @@ namespace Mono.CSharp { public override void Emit (EmitContext ec) { ILGenerator ig = ec.ig; + + if (ec.RemapToProxy){ + ig.Emit (OpCodes.Ldarg_0); + ec.EmitArgument (idx); + return; + } + int arg_idx = idx; if (!ec.IsStatic) @@ -3088,6 +3619,14 @@ namespace Mono.CSharp { public void EmitAssign (EmitContext ec, Expression source) { ILGenerator ig = ec.ig; + + if (ec.RemapToProxy){ + ig.Emit (OpCodes.Ldarg_0); + source.Emit (ec); + ec.EmitStoreArgument (idx); + return; + } + int arg_idx = idx; if (!ec.IsStatic) @@ -3110,6 +3649,11 @@ namespace Mono.CSharp { public void AddressOf (EmitContext ec, AddressOp mode) { + if (ec.RemapToProxy){ + Report.Error (-1, "Report this: Taking the address of a remapped parameter not supported"); + return; + } + int arg_idx = idx; if (!ec.IsStatic) @@ -3127,6 +3671,7 @@ namespace Mono.CSharp { ec.ig.Emit (OpCodes.Ldarga, arg_idx); } } + } /// @@ -3151,7 +3696,7 @@ namespace Mono.CSharp { public Type Type { get { if (ArgType == AType.Ref || ArgType == AType.Out) - return TypeManager.LookupType (Expr.Type.ToString () + "&"); + return TypeManager.GetReferenceType (Expr.Type); else return Expr.Type; } @@ -3375,18 +3920,27 @@ namespace Mono.CSharp { if (argument_expr is IntConstant){ IntConstant ei = (IntConstant) argument_expr; int value = ei.Value; - + if (p == TypeManager.sbyte_type){ if (value >= SByte.MinValue && value <= SByte.MaxValue) return 1; } else if (p == TypeManager.byte_type){ - if (Byte.MinValue >= 0 && value <= Byte.MaxValue) + if (q == TypeManager.sbyte_type && + value >= SByte.MinValue && value <= SByte.MaxValue) + return 0; + else if (Byte.MinValue >= 0 && value <= Byte.MaxValue) return 1; } else if (p == TypeManager.short_type){ if (value >= Int16.MinValue && value <= Int16.MaxValue) return 1; } else if (p == TypeManager.ushort_type){ - if (value >= UInt16.MinValue && value <= UInt16.MaxValue) + if (q == TypeManager.short_type && + value >= Int16.MinValue && value <= Int16.MaxValue) + return 0; + else if (value >= UInt16.MinValue && value <= UInt16.MaxValue) + return 1; + } else if (p == TypeManager.int32_type){ + if (value >= Int32.MinValue && value <= Int32.MaxValue) return 1; } else if (p == TypeManager.uint32_type){ // @@ -3400,8 +3954,12 @@ namespace Mono.CSharp { // we can optimize this case: a positive int32 // always fits on a uint64 // - if (value >= 0) + if (q == TypeManager.int64_type) + return 0; + else if (value >= 0) return 1; + } else if (p == TypeManager.int64_type){ + return 1; } } else if (argument_type == TypeManager.int64_type && argument_expr is LongConstant){ LongConstant lc = (LongConstant) argument_expr; @@ -3413,7 +3971,7 @@ namespace Mono.CSharp { } if (q == null) { - Expression tmp = ConvertImplicit (ec, argument_expr, p, loc); + Expression tmp = Convert.ImplicitConversion (ec, argument_expr, p, loc); if (tmp != null) return 1; @@ -3424,8 +3982,8 @@ namespace Mono.CSharp { Expression p_tmp = new EmptyExpression (p); Expression q_tmp = new EmptyExpression (q); - if (StandardConversionExists (p_tmp, q) == true && - StandardConversionExists (q_tmp, p) == false) + if (Convert.ImplicitConversionExists (ec, p_tmp, q) == true && + Convert.ImplicitConversionExists (ec, q_tmp, p) == false) return 1; if (p == TypeManager.sbyte_type) @@ -3668,14 +4226,14 @@ namespace Mono.CSharp { if (a_mod == p_mod) { if (a_mod == Parameter.Modifier.NONE) - if (!ImplicitConversionExists (ec, a.Expr, pd.ParameterType (i))) + if (!Convert.ImplicitConversionExists (ec, a.Expr, pd.ParameterType (i))) return false; if ((a_mod & Parameter.Modifier.ISBYREF) != 0) { Type pt = pd.ParameterType (i); if (!pt.IsByRef) - pt = TypeManager.LookupType (pt.FullName + "&"); + pt = TypeManager.GetReferenceType (pt); if (pt != a.Type) return false; @@ -3690,7 +4248,7 @@ namespace Mono.CSharp { for (int i = pd_count - 1; i < arg_count; i++) { Argument a = (Argument) arguments [i]; - if (!StandardConversionExists (a.Expr, element_type)) + if (!Convert.ImplicitStandardConversionExists (a.Expr, element_type)) return false; } @@ -3730,14 +4288,14 @@ namespace Mono.CSharp { if (a_mod == p_mod || (a_mod == Parameter.Modifier.NONE && p_mod == Parameter.Modifier.PARAMS)) { if (a_mod == Parameter.Modifier.NONE) - if (!ImplicitConversionExists (ec, a.Expr, pd.ParameterType (i))) + if (!Convert.ImplicitConversionExists (ec, a.Expr, pd.ParameterType (i))) return false; if ((a_mod & Parameter.Modifier.ISBYREF) != 0) { Type pt = pd.ParameterType (i); if (!pt.IsByRef) - pt = TypeManager.LookupType (pt.FullName + "&"); + pt = TypeManager.GetReferenceType (pt); if (pt != a.Type) return false; @@ -3770,7 +4328,6 @@ namespace Mono.CSharp { public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me, ArrayList Arguments, Location loc) { - ArrayList afm = new ArrayList (); MethodBase method = null; Type current_type = null; int argument_count; @@ -3891,6 +4448,23 @@ namespace Mono.CSharp { return method; } + static void Error_InvalidArguments (Location loc, int idx, MethodBase method, + Type delegate_type, string arg_sig, string par_desc) + { + if (delegate_type == null) + Report.Error (1502, loc, + "The best overloaded match for method '" + + FullMethodDesc (method) + + "' has some invalid arguments"); + else + Report.Error (1594, loc, + "Delegate '" + delegate_type.ToString () + + "' has some invalid arguments."); + Report.Error (1503, loc, + String.Format ("Argument {0}: Cannot convert from '{1}' to '{2}'", + idx, arg_sig, par_desc)); + } + public static bool VerifyArgumentsCompat (EmitContext ec, ArrayList Arguments, int argument_count, MethodBase method, @@ -3905,33 +4479,37 @@ namespace Mono.CSharp { Argument a = (Argument) Arguments [j]; Expression a_expr = a.Expr; Type parameter_type = pd.ParameterType (j); + Parameter.Modifier pm = pd.ParameterModifier (j); + + if (pm == Parameter.Modifier.PARAMS){ + if (chose_params_expanded) + parameter_type = TypeManager.TypeToCoreType (parameter_type.GetElementType ()); + } else { + // + // Check modifiers + // + if (pd.ParameterModifier (j) != a.GetParameterModifier ()){ + if (!Location.IsNull (loc)) + Error_InvalidArguments ( + loc, j, method, delegate_type, + Argument.FullDesc (a), pd.ParameterDesc (j)); + return false; + } + } - if (pd.ParameterModifier (j) == Parameter.Modifier.PARAMS && - chose_params_expanded) - parameter_type = TypeManager.TypeToCoreType (parameter_type.GetElementType ()); - + // + // Check Type + // if (a.Type != parameter_type){ Expression conv; - conv = ConvertImplicit (ec, a_expr, parameter_type, loc); + conv = Convert.ImplicitConversion (ec, a_expr, parameter_type, loc); if (conv == null) { - if (!Location.IsNull (loc)) { - if (delegate_type == null) - Report.Error (1502, loc, - "The best overloaded match for method '" + - FullMethodDesc (method) + - "' has some invalid arguments"); - else - Report.Error (1594, loc, - "Delegate '" + delegate_type.ToString () + - "' has some invalid arguments."); - Report.Error (1503, loc, - "Argument " + (j+1) + - ": Cannot convert from '" + Argument.FullDesc (a) - + "' to '" + pd.ParameterDesc (j) + "'"); - } - + if (!Location.IsNull (loc)) + Error_InvalidArguments ( + loc, j, method, delegate_type, + Argument.FullDesc (a), pd.ParameterDesc (j)); return false; } @@ -3969,7 +4547,7 @@ namespace Mono.CSharp { return true; } - + public override Expression DoResolve (EmitContext ec) { // @@ -3979,6 +4557,8 @@ namespace Mono.CSharp { if (expr is BaseAccess) is_base = true; + Expression old = expr; + expr = expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup); if (expr == null) return null; @@ -3995,7 +4575,7 @@ namespace Mono.CSharp { } if (!(expr is MethodGroupExpr)){ - expr.Error118 (ResolveFlags.MethodGroup); + expr.Error_UnexpectedKind (ResolveFlags.MethodGroup); return null; } @@ -4041,6 +4621,11 @@ namespace Mono.CSharp { return null; } + if ((method.Attributes & MethodAttributes.SpecialName) != 0){ + if (TypeManager.IsSpecialMethod (method)) + Report.Error (571, loc, method.Name + ": can not call operator or accessor"); + } + eclass = ExprClass.Value; return this; } @@ -4068,9 +4653,18 @@ namespace Mono.CSharp { ig.Emit (OpCodes.Ldloc, array); IntConstant.EmitInt (ig, j - idx); + + bool is_stobj; + OpCode op = ArrayAccess.GetStoreOpcode (t, out is_stobj); + if (is_stobj) + ig.Emit (OpCodes.Ldelema, t); + a.Emit (ec); - - ArrayAccess.EmitStoreOpcode (ig, t); + + if (is_stobj) + ig.Emit (OpCodes.Stobj, t); + else + ig.Emit (op); } ig.Emit (OpCodes.Ldloc, array); } @@ -4258,8 +4852,7 @@ namespace Mono.CSharp { { MethodGroupExpr mg = (MethodGroupExpr) this.expr; - EmitCall ( - ec, is_base, method.IsStatic, mg.InstanceExpression, method, Arguments, loc); + EmitCall (ec, is_base, method.IsStatic, mg.InstanceExpression, method, Arguments, loc); } public override void EmitStatement (EmitContext ec) @@ -4291,7 +4884,7 @@ namespace Mono.CSharp { /// /// Implements the new expression /// - public class New : ExpressionStatement { + public class New : ExpressionStatement, IMemoryLocation { public readonly ArrayList Arguments; public readonly Expression RequestedType; @@ -4348,9 +4941,20 @@ namespace Mono.CSharp { // instead of Call + Push-temp on value types. // value_target = MyEmptyExpression; } - + public override Expression DoResolve (EmitContext ec) { + // + // The New DoResolve might be called twice when initializing field + // expressions (see EmitFieldInitializers, the call to + // GetInitializerExpression will perform a resolve on the expression, + // and later the assign will trigger another resolution + // + // This leads to bugs (#37014) + // + if (type != null) + return this; + type = ec.DeclSpace.ResolveType (RequestedType, false, loc); if (type == null) @@ -4362,9 +4966,7 @@ namespace Mono.CSharp { return (new NewDelegate (type, Arguments, loc)).Resolve (ec); if (type.IsInterface || type.IsAbstract){ - Error ( - 144, "It is not possible to create instances of interfaces " + - "or abstract classes"); + Error (144, "It is not possible to create instances of interfaces or abstract classes"); return null; } @@ -4380,7 +4982,7 @@ namespace Mono.CSharp { return this; Expression ml; - ml = MemberLookupFinal (ec, type, ".ctor", + ml = MemberLookupFinal (ec, null, type, ".ctor", MemberTypes.Constructor, AllBindingFlags | BindingFlags.DeclaredOnly, loc); @@ -4389,7 +4991,7 @@ namespace Mono.CSharp { if (! (ml is MethodGroupExpr)){ if (!is_struct){ - ml.Error118 ("method group"); + ml.Error_UnexpectedKind ("method group"); return null; } } @@ -4409,10 +5011,10 @@ namespace Mono.CSharp { if (method == null) { if (!is_struct || Arguments.Count > 0) { - Error (1501, - "New invocation: Can not find a constructor for " + - "this argument list"); - return null; + Error (1501, String.Format ( + "New invocation: Can not find a constructor in `{0}' for this argument list", + TypeManager.CSharpName (type))); + return null; } } return this; @@ -4489,6 +5091,34 @@ namespace Mono.CSharp { if (DoEmit (ec, false)) ec.ig.Emit (OpCodes.Pop); } + + public void AddressOf (EmitContext ec, AddressOp Mode) + { + if (!type.IsValueType){ + // + // We throw an exception. So far, I believe we only need to support + // value types: + // foreach (int j in new StructType ()) + // see bug 42390 + // + throw new Exception ("AddressOf should not be used for classes"); + } + + if (!value_target_set) + value_target = new LocalTemporary (ec, type); + + IMemoryLocation ml = (IMemoryLocation) value_target; + ml.AddressOf (ec, AddressOp.Store); + if (method != null) + Invocation.EmitArguments (ec, method, Arguments); + + if (method == null) + ec.ig.Emit (OpCodes.Initobj, type); + else + ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method); + + ((IMemoryLocation) value_target).AddressOf (ec, Mode); + } } /// @@ -4534,6 +5164,8 @@ namespace Mono.CSharp { // via the InitializeArray method - through EmitStaticInitializers // int num_automatic_initializers; + + const int max_automatic_initializers = 6; public ArrayCreation (Expression requested_base_type, ArrayList exprs, string rank, ArrayList initializers, Location l) { @@ -4618,6 +5250,11 @@ namespace Mono.CSharp { Error_IncorrectArrayInitializer (); return false; } + if (specified_dims && (idx + 1 >= arguments.Count)){ + Error (623, "Array initializers can only be used in a variable or field initializer, try using the new expression"); + return false; + } + bool ret = CheckIndices (ec, (ArrayList) o, idx + 1, specified_dims); if (!ret) return false; @@ -4635,7 +5272,7 @@ namespace Mono.CSharp { // Console.WriteLine ("I got: " + tmp); // Handle initialization from vars, fields etc. - Expression conv = ConvertImplicitRequired ( + Expression conv = Convert.ImplicitConversionRequired ( ec, tmp, underlying_type, loc); if (conv == null) @@ -4735,15 +5372,15 @@ namespace Mono.CSharp { bool old_checked = ec.CheckState; ec.CheckState = true; - target = ConvertImplicit (ec, source, TypeManager.int32_type, loc); + target = Convert.ImplicitConversion (ec, source, TypeManager.int32_type, loc); if (target == null){ - target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc); + target = Convert.ImplicitConversion (ec, source, TypeManager.uint32_type, loc); if (target == null){ - target = ConvertImplicit (ec, source, TypeManager.int64_type, loc); + target = Convert.ImplicitConversion (ec, source, TypeManager.int64_type, loc); if (target == null){ - target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc); + target = Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, loc); if (target == null) - Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type); + Convert.Error_CannotImplicitConversion (loc, source.Type, TypeManager.int32_type); } } } @@ -4855,7 +5492,7 @@ namespace Mono.CSharp { AllBindingFlags, loc); if (!(ml is MethodGroupExpr)) { - ml.Error118 ("method group"); + ml.Error_UnexpectedKind ("method group"); return null; } @@ -5112,8 +5749,12 @@ namespace Mono.CSharp { // Basically we do this for string literals and // other non-literal expressions // + if (e is EnumConstant){ + e = ((EnumConstant) e).Child; + } + if (e is StringConstant || !(e is Constant) || - num_automatic_initializers <= 2) { + num_automatic_initializers <= max_automatic_initializers) { Type etype = e.Type; ig.Emit (OpCodes.Ldloc, temp); @@ -5138,17 +5779,18 @@ namespace Mono.CSharp { // n.DisableTemporaryValueType (); } - + ig.Emit (OpCodes.Ldelema, etype); } e.Emit (ec); - - if (dims == 1) - ArrayAccess.EmitStoreOpcode (ig, array_element_type); - else - ig.Emit (OpCodes.Call, set); - } + + if (dims == 1) + ArrayAccess.EmitStoreOpcode (ig, array_element_type); + else + ig.Emit (OpCodes.Call, set); + + } } // @@ -5203,7 +5845,7 @@ namespace Mono.CSharp { if (underlying_type != TypeManager.string_type && underlying_type != TypeManager.object_type) { - if (num_automatic_initializers > 2) + if (num_automatic_initializers > max_automatic_initializers) EmitStaticInitializers (ec, dynamic_initializers || !is_statement); } @@ -5221,7 +5863,34 @@ namespace Mono.CSharp { { DoEmit (ec, true); } - + + public object EncodeAsAttribute () + { + if (!is_one_dimensional){ + Report.Error (-211, Location, "attribute can not encode multi-dimensional arrays"); + return null; + } + + if (array_data == null){ + Report.Error (-212, Location, "array should be initialized when passing it to an attribute"); + return null; + } + + object [] ret = new object [array_data.Count]; + int i = 0; + foreach (Expression e in array_data){ + object v; + + if (e is NullLiteral) + v = null; + else { + if (!Attribute.GetAttributeArgumentExpression (e, Location, out v)) + return null; + } + ret [i++] = v; + } + return ret; + } } /// @@ -5230,7 +5899,7 @@ namespace Mono.CSharp { public class This : Expression, IAssignMethod, IMemoryLocation, IVariable { Block block; - VariableInfo vi; + VariableInfo variable_info; public This (Block block, Location loc) { @@ -5243,57 +5912,54 @@ namespace Mono.CSharp { this.loc = loc; } - public bool IsAssigned (EmitContext ec, Location loc) - { - if (vi == null) - return true; - - return vi.IsAssigned (ec, loc); + public VariableInfo VariableInfo { + get { return variable_info; } } - public bool IsFieldAssigned (EmitContext ec, string field_name, Location loc) + public bool ResolveBase (EmitContext ec) { - if (vi == null) - return true; + eclass = ExprClass.Variable; + type = ec.ContainerType; - return vi.IsFieldAssigned (ec, field_name, loc); - } + if (ec.IsStatic) { + Error (26, "Keyword this not valid in static code"); + return false; + } - public void SetAssigned (EmitContext ec) - { - if (vi != null) - vi.SetAssigned (ec); - } + if ((block != null) && (block.ThisVariable != null)) + variable_info = block.GetVariableInfo (block.ThisVariable); - public void SetFieldAssigned (EmitContext ec, string field_name) - { - if (vi != null) - vi.SetFieldAssigned (ec, field_name); + return true; } public override Expression DoResolve (EmitContext ec) { - eclass = ExprClass.Variable; - type = ec.ContainerType; - - if (ec.IsStatic){ - Error (26, "Keyword this not valid in static code"); + if (!ResolveBase (ec)) return null; + + if ((variable_info != null) && !variable_info.IsAssigned (ec)) { + Error (188, "The this object cannot be used before all " + + "of its fields are assigned to"); + variable_info.SetAssigned (ec); + return this; } - if (block != null) - vi = block.ThisVariable; + if (ec.IsFieldInitializer) { + Error (27, "Keyword `this' can't be used outside a constructor, " + + "a method or a property."); + return null; + } return this; } override public Expression DoResolveLValue (EmitContext ec, Expression right_side) { - DoResolve (ec); + if (!ResolveBase (ec)) + return null; - VariableInfo vi = ec.CurrentBlock.ThisVariable; - if (vi != null) - vi.SetAssigned (ec); + if (variable_info != null) + variable_info.SetAssigned (ec); if (ec.TypeContainer is Class){ Error (1604, "Cannot assign to `this'"); @@ -5393,8 +6059,9 @@ namespace Mono.CSharp { public override Expression DoResolve (EmitContext ec) { if (!ec.InUnsafe) { - Error (233, "Sizeof may only be used in an unsafe context " + - "(consider using System.Runtime.InteropServices.Marshal.Sizeof"); + Report.Error ( + 233, loc, "Sizeof may only be used in an unsafe context " + + "(consider using System.Runtime.InteropServices.Marshal.Sizeof"); return null; } @@ -5403,7 +6070,7 @@ namespace Mono.CSharp { return null; if (!TypeManager.IsUnmanagedType (type_queried)){ - Report.Error (208, "Cannot take the size of an unmanaged type (" + TypeManager.CSharpName (type_queried) + ")"); + Report.Error (208, loc, "Cannot take the size of an unmanaged type (" + TypeManager.CSharpName (type_queried) + ")"); return null; } @@ -5426,10 +6093,9 @@ namespace Mono.CSharp { /// /// Implements the member access expression /// - public class MemberAccess : Expression, ITypeExpression { + public class MemberAccess : Expression { public readonly string Identifier; Expression expr; - Expression member_lookup; public MemberAccess (Expression expr, string id, Location l) { @@ -5498,7 +6164,10 @@ namespace Mono.CSharp { Const c = TypeManager.LookupConstant ((FieldBuilder) fi); if (c != null) { - object o = c.LookupConstantValue (ec); + object o = c.LookupConstantValue (); + if (o == null) + return null; + object real_value = ((Constant) c.Expr).GetValue (); return Constantify (real_value, fi.FieldType); @@ -5640,11 +6309,6 @@ namespace Mono.CSharp { return member_lookup; } - if (member_lookup is TypeExpr){ - member_lookup.Resolve (ec, ResolveFlags.Type); - return member_lookup; - } - Console.WriteLine ("Left is: " + left); Report.Error (-100, loc, "Support for [" + member_lookup + "] is not present yet"); Environment.Exit (0); @@ -5655,6 +6319,7 @@ namespace Mono.CSharp { { if (type != null) throw new Exception (); + // // Resolve the expression with flow analysis turned off, we'll do the definite // assignment checks later. This is because we don't know yet what the expression @@ -5664,14 +6329,13 @@ namespace Mono.CSharp { Expression original = expr; expr = expr.Resolve (ec, flags | ResolveFlags.DisableFlowAnalysis); - if (expr == null) return null; if (expr is SimpleName){ SimpleName child_expr = (SimpleName) expr; - - Expression new_expr = new SimpleName (child_expr.Name + "." + Identifier, loc); + + Expression new_expr = new SimpleName (child_expr.Name, Identifier, loc); return new_expr.Resolve (ec, flags); } @@ -5688,79 +6352,40 @@ namespace Mono.CSharp { int errors = Report.Errors; Type expr_type = expr.Type; - if ((expr is TypeExpr) && (expr_type.IsSubclassOf (TypeManager.enum_type))){ - - Enum en = TypeManager.LookupEnum (expr_type); - - if (en != null) { - object value = en.LookupEnumValue (ec, Identifier, loc); - - if (value != null){ - Constant c = Constantify (value, en.UnderlyingType); - return new EnumConstant (c, expr_type); + if (expr is TypeExpr){ + if (!ec.DeclSpace.CheckAccessLevel (expr_type)){ + Error (122, "`" + expr_type + "' " + + "is inaccessible because of its protection level"); + return null; + } + + if (expr_type == TypeManager.enum_type || expr_type.IsSubclassOf (TypeManager.enum_type)){ + Enum en = TypeManager.LookupEnum (expr_type); + + if (en != null) { + object value = en.LookupEnumValue (ec, Identifier, loc); + + if (value != null){ + Constant c = Constantify (value, en.UnderlyingType); + return new EnumConstant (c, expr_type); + } } } } - + if (expr_type.IsPointer){ Error (23, "The `.' operator can not be applied to pointer operands (" + TypeManager.CSharpName (expr_type) + ")"); return null; } - member_lookup = MemberLookup (ec, expr_type, Identifier, loc); - - if (member_lookup == null){ - // Error has already been reported. - if (errors < Report.Errors) - return null; - - // - // Try looking the member up from the same type, if we find - // it, we know that the error was due to limited visibility - // - object lookup = TypeManager.MemberLookup ( - expr_type, expr_type, AllMemberTypes, AllBindingFlags | - BindingFlags.NonPublic, Identifier); - if (lookup == null) - Error (117, "`" + expr_type + "' does not contain a " + - "definition for `" + Identifier + "'"); - else if ((expr_type != ec.ContainerType) && - ec.ContainerType.IsSubclassOf (expr_type)){ - - // Although a derived class can access protected members of - // its base class it cannot do so through an instance of the - // base class (CS1540). If the expr_type is a parent of the - // ec.ContainerType and the lookup succeeds with the latter one, - // then we are in this situation. - - lookup = TypeManager.MemberLookup ( - ec.ContainerType, ec.ContainerType, AllMemberTypes, - AllBindingFlags, Identifier); - - if (lookup != null) - Error (1540, "Cannot access protected member `" + - expr_type + "." + Identifier + "' " + - "via a qualifier of type `" + - TypeManager.CSharpName (expr_type) + "'; the " + - "qualifier must be of type `" + - TypeManager.CSharpName (ec.ContainerType) + "' " + - "(or derived from it)"); - else - Error (122, "`" + expr_type + "." + Identifier + "' " + - "is inaccessible because of its protection level"); - } else - Error (122, "`" + expr_type + "." + Identifier + "' " + - "is inaccessible because of its protection level"); - + Expression member_lookup; + member_lookup = MemberLookupFinal (ec, expr_type, expr_type, Identifier, loc); + if (member_lookup == null) return null; - } - if (member_lookup is TypeExpr){ - member_lookup.Resolve (ec, ResolveFlags.Type); + if (member_lookup is TypeExpr) return member_lookup; - } else if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type) - return null; member_lookup = ResolveMemberAccess (ec, member_lookup, expr, loc, original); if (member_lookup == null) @@ -5789,9 +6414,40 @@ namespace Mono.CSharp { ResolveFlags.SimpleName | ResolveFlags.Type); } - public Expression DoResolveType (EmitContext ec) + public override Expression ResolveAsTypeStep (EmitContext ec) { - return DoResolve (ec, null, ResolveFlags.Type); + Expression new_expr = expr.ResolveAsTypeStep (ec); + + if (new_expr == null) + return null; + + if (new_expr is SimpleName){ + SimpleName child_expr = (SimpleName) new_expr; + + new_expr = new SimpleName (child_expr.Name, Identifier, loc); + + return new_expr.ResolveAsTypeStep (ec); + } + + Type expr_type = new_expr.Type; + + if (expr_type.IsPointer){ + Error (23, "The `.' operator can not be applied to pointer operands (" + + TypeManager.CSharpName (expr_type) + ")"); + return null; + } + + Expression member_lookup; + member_lookup = MemberLookupFinal (ec, expr_type, expr_type, Identifier, loc); + if (member_lookup == null) + return null; + + if (member_lookup is TypeExpr){ + member_lookup.Resolve (ec, ResolveFlags.Type); + return member_lookup; + } + + return null; } public override void Emit (EmitContext ec) @@ -5820,10 +6476,13 @@ namespace Mono.CSharp { public override Expression DoResolve (EmitContext ec) { + bool last_check = ec.CheckState; bool last_const_check = ec.ConstantCheckState; + ec.CheckState = true; ec.ConstantCheckState = true; Expr = Expr.Resolve (ec); + ec.CheckState = last_check; ec.ConstantCheckState = last_const_check; if (Expr == null) @@ -5866,10 +6525,13 @@ namespace Mono.CSharp { public override Expression DoResolve (EmitContext ec) { + bool last_check = ec.CheckState; bool last_const_check = ec.ConstantCheckState; + ec.CheckState = false; ec.ConstantCheckState = false; Expr = Expr.Resolve (ec); + ec.CheckState = last_check; ec.ConstantCheckState = last_const_check; if (Expr == null) @@ -5901,7 +6563,7 @@ namespace Mono.CSharp { /// An Element Access expression. /// /// During semantic analysis these are transformed into - /// IndexerAccess or ArrayAccess + /// IndexerAccess, ArrayAccess or a PointerArithmetic. /// public class ElementAccess : Expression { public ArrayList Arguments; @@ -5945,19 +6607,16 @@ namespace Mono.CSharp { Type t = Expr.Type; if (t == TypeManager.void_ptr_type){ - Error ( - 242, - "The array index operation is not valid for void pointers"); + Error (242, "The array index operation is not valid for void pointers"); return null; } if (Arguments.Count != 1){ - Error ( - 196, - "A pointer must be indexed by a single value"); + Error (196, "A pointer must be indexed by a single value"); return null; } - Expression p = new PointerArithmetic (true, Expr, ((Argument)Arguments [0]).Expr, - t, loc); + Expression p; + + p = new PointerArithmetic (true, Expr, ((Argument)Arguments [0]).Expr, t, loc); return new Indirection (p, loc); } @@ -6028,7 +6687,7 @@ namespace Mono.CSharp { // As long as the type is valid if (!(eclass == ExprClass.Variable || eclass == ExprClass.PropertyAccess || eclass == ExprClass.Value)) { - ea.Expr.Error118 ("variable or value"); + ea.Expr.Error_UnexpectedKind ("variable or value"); return null; } #endif @@ -6113,28 +6772,47 @@ namespace Mono.CSharp { /// static public void EmitStoreOpcode (ILGenerator ig, Type t) { + bool is_stobj; + OpCode op = GetStoreOpcode (t, out is_stobj); + if (is_stobj) + ig.Emit (OpCodes.Stobj, t); + else + ig.Emit (op); + } + + /// + /// Returns the right opcode to store an object of Type `t' + /// from an array of T. + /// + static public OpCode GetStoreOpcode (Type t, out bool is_stobj) + { + //Console.WriteLine (new System.Diagnostics.StackTrace ()); + is_stobj = false; t = TypeManager.TypeToCoreType (t); if (TypeManager.IsEnumType (t) && t != TypeManager.enum_type) t = TypeManager.EnumToUnderlying (t); if (t == TypeManager.byte_type || t == TypeManager.sbyte_type || t == TypeManager.bool_type) - ig.Emit (OpCodes.Stelem_I1); - else if (t == TypeManager.short_type || t == TypeManager.ushort_type || t == TypeManager.char_type) - ig.Emit (OpCodes.Stelem_I2); + return OpCodes.Stelem_I1; + else if (t == TypeManager.short_type || t == TypeManager.ushort_type || + t == TypeManager.char_type) + return OpCodes.Stelem_I2; else if (t == TypeManager.int32_type || t == TypeManager.uint32_type) - ig.Emit (OpCodes.Stelem_I4); + return OpCodes.Stelem_I4; else if (t == TypeManager.int64_type || t == TypeManager.uint64_type) - ig.Emit (OpCodes.Stelem_I8); + return OpCodes.Stelem_I8; else if (t == TypeManager.float_type) - ig.Emit (OpCodes.Stelem_R4); + return OpCodes.Stelem_R4; else if (t == TypeManager.double_type) - ig.Emit (OpCodes.Stelem_R8); - else if (t == TypeManager.intptr_type) - ig.Emit (OpCodes.Stelem_I); - else if (t.IsValueType){ - ig.Emit (OpCodes.Stobj, t); + return OpCodes.Stelem_R8; + else if (t == TypeManager.intptr_type) { + is_stobj = true; + return OpCodes.Stobj; + } else if (t.IsValueType) { + is_stobj = true; + return OpCodes.Stobj; } else - ig.Emit (OpCodes.Stelem_Ref); + return OpCodes.Stelem_Ref; } MethodInfo FetchGetMethod () @@ -6164,19 +6842,10 @@ namespace Mono.CSharp { int arg_count = ea.Arguments.Count; Type [] args = new Type [arg_count]; MethodInfo address; - string ptr_type_name; Type ret_type; - ptr_type_name = type.FullName + "&"; - ret_type = Type.GetType (ptr_type_name); + ret_type = TypeManager.GetReferenceType (type); - // - // It is a type defined by the source code we are compiling - // - if (ret_type == null){ - ret_type = mb.GetType (ptr_type_name); - } - for (int i = 0; i < arg_count; i++){ //args [i++] = a.Type; args [i] = TypeManager.int32_type; @@ -6332,7 +7001,7 @@ namespace Mono.CSharp { class Indexers { - public ArrayList getters, setters; + public ArrayList properties; static Hashtable map; static Indexers () @@ -6340,48 +7009,35 @@ namespace Mono.CSharp { map = new Hashtable (); } - Indexers (MemberInfo [] mi) + Indexers () + { + properties = new ArrayList (); + } + + void Append (MemberInfo [] mi) { foreach (PropertyInfo property in mi){ MethodInfo get, set; get = property.GetGetMethod (true); - if (get != null){ - if (getters == null) - getters = new ArrayList (); - - getters.Add (get); - } - set = property.GetSetMethod (true); - if (set != null){ - if (setters == null) - setters = new ArrayList (); - setters.Add (set); - } + properties.Add (new Pair (get, set)); } } - static private Indexers GetIndexersForTypeOrInterface (Type caller_type, Type lookup_type) + static private MemberInfo [] GetIndexersForTypeOrInterface (Type caller_type, Type lookup_type) { - Indexers ix = (Indexers) map [lookup_type]; - - if (ix != null) - return ix; - string p_name = TypeManager.IndexerPropertyName (lookup_type); MemberInfo [] mi = TypeManager.MemberLookup ( - caller_type, lookup_type, MemberTypes.Property, - BindingFlags.Public | BindingFlags.Instance, p_name); + caller_type, caller_type, lookup_type, MemberTypes.Property, + BindingFlags.Public | BindingFlags.Instance | + BindingFlags.DeclaredOnly, p_name); if (mi == null || mi.Length == 0) return null; - ix = new Indexers (mi); - map [lookup_type] = ix; - - return ix; + return mi; } static public Indexers GetIndexersForType (Type caller_type, Type lookup_type, Location loc) @@ -6391,23 +7047,34 @@ namespace Mono.CSharp { if (ix != null) return ix; - ix = GetIndexersForTypeOrInterface (caller_type, lookup_type); - if (ix != null) - return ix; + Type copy = lookup_type; + while (copy != TypeManager.object_type && copy != null){ + MemberInfo [] mi = GetIndexersForTypeOrInterface (caller_type, copy); + + if (mi != null){ + if (ix == null) + ix = new Indexers (); + + ix.Append (mi); + } + + copy = copy.BaseType; + } Type [] ifaces = TypeManager.GetInterfaces (lookup_type); if (ifaces != null) { foreach (Type itype in ifaces) { - ix = GetIndexersForTypeOrInterface (caller_type, itype); - if (ix != null) - return ix; + MemberInfo [] mi = GetIndexersForTypeOrInterface (caller_type, itype); + if (mi != null){ + if (ix == null) + ix = new Indexers (); + + ix.Append (mi); + } } } - Report.Error (21, loc, - "Type `" + TypeManager.CSharpName (lookup_type) + - "' does not have any indexers defined"); - return null; + return ix; } } @@ -6419,7 +7086,6 @@ namespace Mono.CSharp { // Points to our "data" repository // MethodInfo get, set; - Indexers ilist; ArrayList set_arguments; bool is_base_indexer; @@ -6453,6 +7119,7 @@ namespace Mono.CSharp { public override Expression DoResolve (EmitContext ec) { + ArrayList AllGetters = new ArrayList(); if (!CommonResolve (ec)) return null; @@ -6462,22 +7129,46 @@ namespace Mono.CSharp { // // This is a group of properties, piles of them. - if (ilist == null) - ilist = Indexers.GetIndexersForType ( - current_type, indexer_type, loc); + bool found_any = false, found_any_getters = false; + Type lookup_type = indexer_type; + + Indexers ilist; + ilist = Indexers.GetIndexersForType (current_type, lookup_type, loc); + if (ilist != null) { + found_any = true; + if (ilist.properties != null) { + foreach (Pair o in ilist.properties) { + if (o.First != null) + AllGetters.Add(o.First); + } + } + } - // - // Step 2: find the proper match - // - if (ilist != null && ilist.getters != null && ilist.getters.Count > 0) + if (AllGetters.Count > 0) { + found_any_getters = true; get = (MethodInfo) Invocation.OverloadResolve ( - ec, new MethodGroupExpr (ilist.getters, loc), arguments, loc); + ec, new MethodGroupExpr (AllGetters, loc), arguments, loc); + } + + if (!found_any) { + Report.Error (21, loc, + "Type `" + TypeManager.CSharpName (indexer_type) + + "' does not have any indexers defined"); + return null; + } - if (get == null){ + if (!found_any_getters) { Error (154, "indexer can not be used in this context, because " + "it lacks a `get' accessor"); return null; } + + if (get == null) { + Error (1501, "No Overload for method `this' takes `" + + arguments.Count + "' arguments"); + return null; + } + // // Only base will allow this invocation to happen. // @@ -6498,26 +7189,49 @@ namespace Mono.CSharp { public override Expression DoResolveLValue (EmitContext ec, Expression right_side) { + ArrayList AllSetters = new ArrayList(); if (!CommonResolve (ec)) return null; Type right_type = right_side.Type; - if (ilist == null) - ilist = Indexers.GetIndexersForType ( - current_type, indexer_type, loc); + bool found_any = false, found_any_setters = false; - if (ilist != null && ilist.setters != null && ilist.setters.Count > 0){ + Indexers ilist = Indexers.GetIndexersForType (current_type, indexer_type, loc); + if (ilist != null) { + found_any = true; + if (ilist.properties != null) { + foreach (Pair o in ilist.properties) { + if (o.Second != null) + AllSetters.Add(o.Second); + } + } + } + if (AllSetters.Count > 0) { + found_any_setters = true; set_arguments = (ArrayList) arguments.Clone (); set_arguments.Add (new Argument (right_side, Argument.AType.Expression)); - set = (MethodInfo) Invocation.OverloadResolve ( - ec, new MethodGroupExpr (ilist.setters, loc), set_arguments, loc); + ec, new MethodGroupExpr (AllSetters, loc), + set_arguments, loc); } - - if (set == null){ - Error (200, "indexer X.this [" + TypeManager.CSharpName (right_type) + - "] lacks a `set' accessor"); + + if (!found_any) { + Report.Error (21, loc, + "Type `" + TypeManager.CSharpName (indexer_type) + + "' does not have any indexers defined"); + return null; + } + + if (!found_any_setters) { + Error (154, "indexer can not be used in this context, because " + + "it lacks a `set' accessor"); + return null; + } + + if (set == null) { + Error (1501, "No Overload for method `this' takes `" + + arguments.Count + "' arguments"); return null; } @@ -6528,14 +7242,26 @@ namespace Mono.CSharp { Report.Error (205, loc, "Cannot call an abstract base indexer: " + Invocation.FullMethodDesc (set)); return null; } - type = TypeManager.void_type; + + // + // Now look for the actual match in the list of indexers to set our "return" type + // + type = TypeManager.void_type; // default value + foreach (Pair t in ilist.properties){ + if (t.Second == set){ + if (t.First != null) + type = ((MethodInfo) t.First).ReturnType; + break; + } + } + eclass = ExprClass.IndexerAccess; return this; } public override void Emit (EmitContext ec) { - Invocation.EmitCall (ec, false, false, instance_expr, get, arguments, loc); + Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, get, arguments, loc); } // @@ -6545,7 +7271,7 @@ namespace Mono.CSharp { // public void EmitAssign (EmitContext ec, Expression source) { - Invocation.EmitCall (ec, false, false, instance_expr, set, set_arguments, loc); + Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, set, set_arguments, loc); } } @@ -6604,11 +7330,10 @@ namespace Mono.CSharp { return null; } - member_lookup = MemberLookup (ec, base_type, base_type, member, + member_lookup = MemberLookup (ec, ec.ContainerType, null, base_type, member, AllMemberTypes, AllBindingFlags, loc); if (member_lookup == null) { - Error (117, TypeManager.CSharpName (base_type) + " does not " + - "contain a definition for `" + member + "'"); + MemberLookupFailed (ec, base_type, base_type, member, null, loc); return null; } @@ -6702,7 +7427,7 @@ namespace Mono.CSharp { // // This is just because we might want to reuse this bad boy // instead of creating gazillions of EmptyExpressions. - // (CanConvertImplicit uses it) + // (CanImplicitConversion uses it) // public void SetType (Type t) { @@ -6751,7 +7476,7 @@ namespace Mono.CSharp { // the type specification, we just use this to construct the type // one bit at a time. // - public class ComposedCast : Expression, ITypeExpression { + public class ComposedCast : Expression { Expression left; string dim; @@ -6762,7 +7487,7 @@ namespace Mono.CSharp { loc = l; } - public Expression DoResolveType (EmitContext ec) + public override Expression ResolveAsTypeStep (EmitContext ec) { Type ltype = ec.DeclSpace.ResolveType (left, false, loc); if (ltype == null) @@ -6805,7 +7530,7 @@ namespace Mono.CSharp { public override Expression DoResolve (EmitContext ec) { - return DoResolveType (ec); + return ResolveAsTypeStep (ec); } public override void Emit (EmitContext ec) @@ -6831,16 +7556,8 @@ namespace Mono.CSharp { Type array_type = array.Type.GetElementType (); this.array = array; - - string array_ptr_type_name = array_type.FullName + "*"; - - type = Type.GetType (array_ptr_type_name); - if (type == null){ - ModuleBuilder mb = CodeGen.ModuleBuilder; - - type = mb.GetType (array_ptr_type_name); - } + type = TypeManager.GetPointerType (array_type); eclass = ExprClass.Value; loc = l; } @@ -6918,7 +7635,7 @@ namespace Mono.CSharp { return null; if (count.Type != TypeManager.int32_type){ - count = ConvertImplicitRequired (ec, count, TypeManager.int32_type, loc); + count = Convert.ImplicitConversionRequired (ec, count, TypeManager.int32_type, loc); if (count == null) return null; } @@ -6937,13 +7654,7 @@ namespace Mono.CSharp { if (!TypeManager.VerifyUnManaged (otype, loc)) return null; - string ptr_name = otype.FullName + "*"; - type = Type.GetType (ptr_name); - if (type == null){ - ModuleBuilder mb = CodeGen.ModuleBuilder; - - type = mb.GetType (ptr_name); - } + type = TypeManager.GetPointerType (otype); eclass = ExprClass.Value; return this;