X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fgmcs%2Fecore.cs;h=3d4786437d3a1dc56537c96eb66eb7a1f6286b47;hb=bd316288ae3fa0cb6f03b367716d04d5244c5d04;hp=3518f56a14e43ab38d9a32570992f98d4df20adc;hpb=35c139e3eba84b5e3a33d242964b6c5fdce90035;p=mono.git diff --git a/mcs/gmcs/ecore.cs b/mcs/gmcs/ecore.cs index 3518f56a14e..3d4786437d3 100644 --- a/mcs/gmcs/ecore.cs +++ b/mcs/gmcs/ecore.cs @@ -59,7 +59,12 @@ namespace Mono.CSharp { DisableFlowAnalysis = 8, // Set if this is resolving the first part of a MemberAccess. - Intermediate = 16 + Intermediate = 16, + + // Disable control flow analysis _of struct_ while resolving the expression. + // This is used when resolving the instance expression of a field expression. + DisableStructFlowAnalysis = 32, + } // @@ -90,23 +95,6 @@ namespace Mono.CSharp { void AddressOf (EmitContext ec, AddressOp mode); } - /// - /// We are either a namespace or a type. - /// If we're a type, `IsType' is true and we may use `Type' to get - /// a TypeExpr representing that type. - /// - public interface IAlias { - bool IsType { - get; - } - - string Name { - get; - } - - TypeExpr ResolveAsType (EmitContext ec); - } - /// /// This interface is implemented by variables /// @@ -115,7 +103,7 @@ namespace Mono.CSharp { get; } - bool VerifyFixed (bool is_expression); + bool VerifyFixed (); } /// @@ -127,19 +115,12 @@ namespace Mono.CSharp { protected Location loc; public Type Type { - get { - return type; - } - - set { - type = value; - } + get { return type; } + set { type = value; } } public Location Location { - get { - return loc; - } + get { return loc; } } /// @@ -147,10 +128,10 @@ namespace Mono.CSharp { /// public void Error (int error, string s) { - if (!Location.IsNull (loc)) - Report.Error (error, loc, s); - else + if (loc.IsNull) Report.Error (error, s); + else + Report.Error (error, loc, s); } /// @@ -176,6 +157,11 @@ namespace Mono.CSharp { AttributeTester.Report_ObsoleteMessage (obsolete_attr, type.FullName, loc); } + public virtual string GetSignatureForError () + { + return TypeManager.CSharpName (type); + } + public static bool IsAccessorAccessible (Type invocation_type, MethodInfo mi, out bool must_do_cs1540_check) { MethodAttributes ma = mi.Attributes & MethodAttributes.MemberAccessMask; @@ -186,7 +172,7 @@ namespace Mono.CSharp { // If only accessible to the current class or children // if (ma == MethodAttributes.Private) - return invocation_type == mi.DeclaringType || + return TypeManager.IsPrivateAccessible (invocation_type, mi.DeclaringType) || TypeManager.IsNestedChildOf (invocation_type, mi.DeclaringType); if (mi.DeclaringType.Assembly == invocation_type.Assembly) { @@ -252,7 +238,12 @@ namespace Mono.CSharp { // This is used if the expression should be resolved as a type or namespace name. // the default implementation fails. // - public virtual FullNamedExpression ResolveAsTypeStep (EmitContext ec) + public FullNamedExpression ResolveAsTypeStep (EmitContext ec) + { + return ResolveAsTypeStep (ec, false); + } + + public virtual FullNamedExpression ResolveAsTypeStep (EmitContext ec, bool silent) { return null; } @@ -262,29 +253,30 @@ namespace Mono.CSharp { // value will be returned if the expression is not a type // reference // - public virtual TypeExpr ResolveAsTypeTerminal (EmitContext ec) + public TypeExpr ResolveAsTypeTerminal (EmitContext ec) + { + return ResolveAsTypeTerminal (ec, false); + } + + public virtual TypeExpr ResolveAsTypeTerminal (EmitContext ec, bool silent) { int errors = Report.Errors; - FullNamedExpression fne = ResolveAsTypeStep (ec); + FullNamedExpression fne = ResolveAsTypeStep (ec, silent); - if (fne == null) { - if (errors == Report.Errors) - Report.Error (246, Location, "Cannot find type '{0}'", ToString ()); + if (fne == null) return null; - } if (fne.eclass != ExprClass.Type) { - if (errors == Report.Errors) - Report.Error (118, Location, "'{0}' denotes a '{1}', where a type was expected", - fne.FullName, fne.ExprClassName ()); + if (!silent && (errors == Report.Errors)) + fne.Error_UnexpectedKind (null, "type", loc); return null; } TypeExpr te = fne as TypeExpr; if (!te.CheckAccessLevel (ec.DeclSpace)) { - Report.Error (122, Location, "'{0}' is inaccessible due to its protection level", te.Name); + ErrorIsInaccesible (loc, TypeManager.CSharpName (te.Type)); return null; } @@ -294,6 +286,46 @@ namespace Mono.CSharp { return te; } + + public static void ErrorIsInaccesible (Location loc, string name) + { + Report.Error (122, loc, "`{0}' is inaccessible due to its protection level", name); + } + + public virtual void Error_ValueCannotBeConverted (Location loc, Type t) + { + Convert.Error_CannotImplicitConversion (loc, Type, t); + } + + protected static void Error_TypeDoesNotContainDefinition (Location loc, Type type, string name) + { + Report.Error (117, loc, "`{0}' does not contain a definition for `{1}'", + TypeManager.CSharpName (type), name); + } + + ResolveFlags ExprClassToResolveFlags () + { + switch (eclass) { + case ExprClass.Type: + case ExprClass.Namespace: + return ResolveFlags.Type; + + case ExprClass.MethodGroup: + return ResolveFlags.MethodGroup; + + case ExprClass.Value: + case ExprClass.Variable: + case ExprClass.PropertyAccess: + case ExprClass.EventAccess: + case ExprClass.IndexerAccess: + return ResolveFlags.VariableOrValue; + + default: + throw new Exception ("Expression " + GetType () + + " ExprClass is Invalid after resolve"); + } + + } /// /// Resolves an expression and performs semantic analysis on it. @@ -306,11 +338,14 @@ namespace Mono.CSharp { public Expression Resolve (EmitContext ec, ResolveFlags flags) { if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type) - return ResolveAsTypeStep (ec); + return ResolveAsTypeStep (ec, false); bool old_do_flow_analysis = ec.DoFlowAnalysis; + bool old_omit_struct_analysis = ec.OmitStructFlowAnalysis; if ((flags & ResolveFlags.DisableFlowAnalysis) != 0) ec.DoFlowAnalysis = false; + if ((flags & ResolveFlags.DisableStructFlowAnalysis) != 0) + ec.OmitStructFlowAnalysis = true; Expression e; bool intermediate = (flags & ResolveFlags.Intermediate) == ResolveFlags.Intermediate; @@ -321,56 +356,14 @@ namespace Mono.CSharp { e = DoResolve (ec); ec.DoFlowAnalysis = old_do_flow_analysis; + ec.OmitStructFlowAnalysis = old_omit_struct_analysis; if (e == null) return null; - if ((e is TypeExpr) || (e is ComposedCast) || (e is Namespace)) { - if ((flags & ResolveFlags.Type) == 0) { - e.Error_UnexpectedKind (flags, loc); - return null; - } - - return e; - } - - switch (e.eclass) { - case ExprClass.Type: - case ExprClass.Namespace: - if ((flags & ResolveFlags.VariableOrValue) == 0) { - e.Error_UnexpectedKind (flags, loc); - return null; - } - break; - - case ExprClass.MethodGroup: - if (RootContext.Version == LanguageVersion.ISO_1){ - if ((flags & ResolveFlags.MethodGroup) == 0) { - ((MethodGroupExpr) e).ReportUsageError (); - return null; - } - } - break; - - case ExprClass.Value: - case ExprClass.Variable: - case ExprClass.PropertyAccess: - case ExprClass.EventAccess: - case ExprClass.IndexerAccess: - if ((flags & ResolveFlags.VariableOrValue) == 0) { - Console.WriteLine ("I got: {0} and {1}", e.GetType (), e); - Console.WriteLine ("I am {0} and {1}", this.GetType (), this); - FieldInfo fi = ((FieldExpr) e).FieldInfo; - - Console.WriteLine ("{0} and {1}", fi.DeclaringType, fi.Name); - e.Error_UnexpectedKind (flags, loc); - return null; - } - break; - - default: - throw new Exception ("Expression " + e.GetType () + - " ExprClass is Invalid after resolve"); + if ((flags & e.ExprClassToResolveFlags ()) == 0) { + e.Error_UnexpectedKind (flags, loc); + return null; } if (e.type == null && !(e is Namespace)) { @@ -388,7 +381,38 @@ namespace Mono.CSharp { /// public Expression Resolve (EmitContext ec) { - return Resolve (ec, ResolveFlags.VariableOrValue); + Expression e = Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup); + + if (e != null && e.eclass == ExprClass.MethodGroup && RootContext.Version == LanguageVersion.ISO_1) { + ((MethodGroupExpr) e).ReportUsageError (); + return null; + } + return e; + } + + public Constant ResolveAsConstant (EmitContext ec, MemberCore mc) + { + Expression e = Resolve (ec); + if (e != null) { + Constant c = e as Constant; + if (c != null) + return c; + + EmptyCast empty = e as EmptyCast; + if (empty != null) { + c = empty.Child as Constant; + if (c != null) { + // TODO: not sure about this maybe there is easier way how to use EmptyCast + if (e.Type.IsEnum) + c.Type = e.Type; + + return c; + } + } + } + + Const.Error_ExpressionMustBeConstant (loc, mc.GetSignatureForError ()); + return null; } /// @@ -399,14 +423,14 @@ namespace Mono.CSharp { /// Currently ResolveLValue wraps DoResolveLValue to perform sanity /// checking and assertion checking on what we expect from Resolve /// - public Expression ResolveLValue (EmitContext ec, Expression right_side) + public Expression ResolveLValue (EmitContext ec, Expression right_side, Location loc) { int errors = Report.Errors; Expression e = DoResolveLValue (ec, right_side); if (e == null) { if (errors == Report.Errors) - Report.Error (131, Location, "The left-hand side of an assignment or mutating operation must be a variable, property or indexer"); + Report.Error (131, loc, "The left-hand side of an assignment or mutating operation must be a variable, property or indexer"); return null; } @@ -516,6 +540,8 @@ namespace Mono.CSharp { /// /// Returns a fully formed expression after a MemberLookup /// + /// + // TODO: This can be heavily cached public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc) { if (mi is EventInfo) @@ -676,69 +702,66 @@ namespace Mono.CSharp { Location loc) { if (almostMatchedMembers.Count != 0) { - if (qualifier_type == null) { - foreach (MemberInfo m in almostMatchedMembers) - Report.Error (38, loc, - "Cannot access non-static member `{0}' via nested type `{1}'", - TypeManager.GetFullNameSignature (m), - TypeManager.CSharpName (ec.ContainerType)); - return; - } - - - if (qualifier_type != ec.ContainerType) { - // 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 qualifier_type is a base of the - // ec.ContainerType and the lookup succeeds with the latter one, - // then we are in this situation. - for (int i = 0; i < almostMatchedMembers.Count; ++i) { - MemberInfo m = (MemberInfo) almostMatchedMembers [i]; - for (int j = 0; j < i; ++j) { - if (m == almostMatchedMembers [j]) { - m = null; - break; - } + for (int i = 0; i < almostMatchedMembers.Count; ++i) { + MemberInfo m = (MemberInfo) almostMatchedMembers [i]; + for (int j = 0; j < i; ++j) { + if (m == almostMatchedMembers [j]) { + m = null; + break; } - if (m == null) - continue; - - Report.SymbolRelatedToPreviousError (m); + } + if (m == null) + continue; + + Type declaring_type = m.DeclaringType; + + Report.SymbolRelatedToPreviousError (m); + if (qualifier_type == null) { + Report.Error (38, loc, "Cannot access a nonstatic member of outer type `{0}' via nested type `{1}'", + TypeManager.CSharpName (m.DeclaringType), + TypeManager.CSharpName (ec.ContainerType)); + } else if (qualifier_type != ec.ContainerType && + TypeManager.IsNestedFamilyAccessible (ec.ContainerType, declaring_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 qualifier_type is a base of the + // ec.ContainerType and the lookup succeeds with the latter one, + // then we are in this situation. Report.Error (1540, loc, "Cannot access protected member `{0}' via a qualifier of type `{1}';" + " the qualifier must be of type `{2}' (or derived from it)", TypeManager.GetFullNameSignature (m), TypeManager.CSharpName (qualifier_type), TypeManager.CSharpName (ec.ContainerType)); + } else { + ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (m)); } - return; } almostMatchedMembers.Clear (); + return; } - MemberInfo[] mi = TypeManager.MemberLookup (queried_type, null, queried_type, - AllMemberTypes, AllBindingFlags | - BindingFlags.NonPublic, name, null); + MemberInfo[] lookup = TypeManager.MemberLookup (queried_type, null, queried_type, + AllMemberTypes, AllBindingFlags | + BindingFlags.NonPublic, name, null); - if (mi == null) { + if (lookup == null) { if (!complain_if_none_found) return; if (class_name != null) - Report.Error (103, loc, "The name `" + name + "' could not be " + - "found in `" + class_name + "'"); + Report.Error (103, loc, "The name `{0}' does not exist in the context of `{1}'", + name, class_name); else - Report.Error ( - 117, loc, "`" + queried_type + "' does not contain a " + - "definition for `" + name + "'"); + Error_TypeDoesNotContainDefinition (loc, queried_type, name); return; } if (TypeManager.MemberLookup (queried_type, null, queried_type, AllMemberTypes, AllBindingFlags | BindingFlags.NonPublic, name, null) == null) { - if ((mi.Length == 1) && (mi [0] is Type)) { - Type t = (Type) mi [0]; + if ((lookup.Length == 1) && (lookup [0] is Type)) { + Type t = (Type) lookup [0]; Report.Error (305, loc, "Using the generic type `{0}' " + @@ -749,18 +772,15 @@ namespace Mono.CSharp { } } - if (name == ".ctor" && TypeManager.FindMembers (qualifier_type, MemberTypes.Constructor, - BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, null, null).Count == 0) + MemberList ml = TypeManager.FindMembers (queried_type, MemberTypes.Constructor, + BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, null, null); + if (name == ".ctor" && ml.Count == 0) { - Report.Error (143, loc, String.Format ("The type '{0}' has no constructors defined", TypeManager.CSharpName (queried_type))); + Report.Error (143, loc, String.Format ("The type `{0}' has no constructors defined", TypeManager.CSharpName (queried_type))); return; } - if (qualifier_type != null) { - Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", TypeManager.CSharpName (qualifier_type) + "." + name); - } else { - Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", name); - } + ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (lookup [0])); } /// @@ -833,7 +853,7 @@ namespace Mono.CSharp { return operator_true; } - public string ExprClassName () + string ExprClassName () { switch (eclass){ case ExprClass.Invalid: @@ -863,326 +883,57 @@ namespace Mono.CSharp { /// /// Reports that we were expecting `expr' to be of class `expected' /// - public void Error_UnexpectedKind (string expected, Location loc) + public void Error_UnexpectedKind (EmitContext ec, string expected, Location loc) + { + Error_UnexpectedKind (ec, expected, ExprClassName (), loc); + } + + public void Error_UnexpectedKind (EmitContext ec, string expected, string was, Location loc) { - Report.Error (118, loc, "Expression denotes a `" + ExprClassName () + - "' where a `" + expected + "' was expected"); + string name = GetSignatureForError (); + if (ec != null) + name = ec.DeclSpace.GetSignatureForError () + '.' + name; + + Report.Error (118, loc, "`{0}' is a `{1}' but a `{2}' was expected", + name, was, expected); } public void Error_UnexpectedKind (ResolveFlags flags, Location loc) { - ArrayList valid = new ArrayList (10); + string [] valid = new string [4]; + int count = 0; if ((flags & ResolveFlags.VariableOrValue) != 0) { - valid.Add ("variable"); - valid.Add ("value"); + valid [count++] = "variable"; + valid [count++] = "value"; } if ((flags & ResolveFlags.Type) != 0) - valid.Add ("type"); + valid [count++] = "type"; if ((flags & ResolveFlags.MethodGroup) != 0) - valid.Add ("method group"); + valid [count++] = "method group"; - if (valid.Count == 0) - valid.Add ("unknown"); + if (count == 0) + valid [count++] = "unknown"; - StringBuilder sb = new StringBuilder (); - for (int i = 0; i < valid.Count; i++) { - if (i > 0) - sb.Append (", "); - else if (i == valid.Count) - sb.Append (" or "); + StringBuilder sb = new StringBuilder (valid [0]); + for (int i = 1; i < count - 1; i++) { + sb.Append ("', `"); sb.Append (valid [i]); } + if (count > 1) { + sb.Append ("' or `"); + sb.Append (valid [count - 1]); + } - Report.Error (119, loc, "Expression denotes a `" + ExprClassName () + "' where " + - "a `" + sb.ToString () + "' was expected"); + Report.Error (119, loc, + "Expression denotes a `{0}', where a `{1}' was expected", ExprClassName (), sb); } - static public void Error_ConstantValueCannotBeConverted (Location l, string val, Type t) - { - Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " + - TypeManager.CSharpName (t)); - } - public static void UnsafeError (Location loc) { - Report.Error (214, loc, "Pointers may only be used in an unsafe context"); - } - - /// - /// Converts the IntConstant, UIntConstant, LongConstant or - /// ULongConstant into the integral target_type. Notice - /// that we do not return an `Expression' we do return - /// a boxed integral type. - /// - /// FIXME: Since I added the new constants, we need to - /// also support conversions from CharConstant, ByteConstant, - /// SByteConstant, UShortConstant, ShortConstant - /// - /// This is used by the switch statement, so the domain - /// of work is restricted to the literals above, and the - /// targets are int32, uint32, char, byte, sbyte, ushort, - /// short, uint64 and int64 - /// - public static object ConvertIntLiteral (Constant c, Type target_type, Location loc) - { - if (!Convert.ImplicitStandardConversionExists (Convert.ConstantEC, c, target_type)){ - Convert.Error_CannotImplicitConversion (loc, c.Type, target_type); - return null; - } - - string s = ""; - - if (c.Type == target_type) - return ((Constant) c).GetValue (); - - // - // Make into one of the literals we handle, we dont really care - // about this value as we will just return a few limited types - // - if (c is EnumConstant) - c = ((EnumConstant)c).WidenToCompilerConstant (); - - if (c is IntConstant){ - int v = ((IntConstant) c).Value; - - if (target_type == TypeManager.uint32_type){ - if (v >= 0) - return (uint) v; - } else if (target_type == TypeManager.char_type){ - if (v >= Char.MinValue && v <= Char.MaxValue) - return (char) v; - } else if (target_type == TypeManager.byte_type){ - if (v >= Byte.MinValue && v <= Byte.MaxValue) - return (byte) v; - } else if (target_type == TypeManager.sbyte_type){ - if (v >= SByte.MinValue && v <= SByte.MaxValue) - return (sbyte) v; - } else if (target_type == TypeManager.short_type){ - if (v >= Int16.MinValue && v <= UInt16.MaxValue) - return (short) v; - } else if (target_type == TypeManager.ushort_type){ - if (v >= UInt16.MinValue && v <= UInt16.MaxValue) - return (ushort) v; - } else if (target_type == TypeManager.int64_type) - return (long) v; - else if (target_type == TypeManager.uint64_type){ - if (v > 0) - return (ulong) v; - } - - s = v.ToString (); - } else if (c is UIntConstant){ - uint v = ((UIntConstant) c).Value; - - if (target_type == TypeManager.int32_type){ - if (v <= Int32.MaxValue) - return (int) v; - } else if (target_type == TypeManager.char_type){ - if (v >= Char.MinValue && v <= Char.MaxValue) - return (char) v; - } else if (target_type == TypeManager.byte_type){ - if (v <= Byte.MaxValue) - return (byte) v; - } else if (target_type == TypeManager.sbyte_type){ - if (v <= SByte.MaxValue) - return (sbyte) v; - } else if (target_type == TypeManager.short_type){ - if (v <= UInt16.MaxValue) - return (short) v; - } else if (target_type == TypeManager.ushort_type){ - if (v <= UInt16.MaxValue) - return (ushort) v; - } else if (target_type == TypeManager.int64_type) - return (long) v; - else if (target_type == TypeManager.uint64_type) - return (ulong) v; - s = v.ToString (); - } else if (c is LongConstant){ - long v = ((LongConstant) c).Value; - - if (target_type == TypeManager.int32_type){ - if (v >= UInt32.MinValue && v <= UInt32.MaxValue) - return (int) v; - } else if (target_type == TypeManager.uint32_type){ - if (v >= 0 && v <= UInt32.MaxValue) - return (uint) v; - } else if (target_type == TypeManager.char_type){ - if (v >= Char.MinValue && v <= Char.MaxValue) - return (char) v; - } else if (target_type == TypeManager.byte_type){ - if (v >= Byte.MinValue && v <= Byte.MaxValue) - return (byte) v; - } else if (target_type == TypeManager.sbyte_type){ - if (v >= SByte.MinValue && v <= SByte.MaxValue) - return (sbyte) v; - } else if (target_type == TypeManager.short_type){ - if (v >= Int16.MinValue && v <= UInt16.MaxValue) - return (short) v; - } else if (target_type == TypeManager.ushort_type){ - if (v >= UInt16.MinValue && v <= UInt16.MaxValue) - return (ushort) v; - } else if (target_type == TypeManager.uint64_type){ - if (v > 0) - return (ulong) v; - } - s = v.ToString (); - } else if (c is ULongConstant){ - ulong v = ((ULongConstant) c).Value; - - if (target_type == TypeManager.int32_type){ - if (v <= Int32.MaxValue) - return (int) v; - } else if (target_type == TypeManager.uint32_type){ - if (v <= UInt32.MaxValue) - return (uint) v; - } else if (target_type == TypeManager.char_type){ - if (v >= Char.MinValue && v <= Char.MaxValue) - return (char) v; - } else if (target_type == TypeManager.byte_type){ - if (v >= Byte.MinValue && v <= Byte.MaxValue) - return (byte) v; - } else if (target_type == TypeManager.sbyte_type){ - if (v <= (int) SByte.MaxValue) - return (sbyte) v; - } else if (target_type == TypeManager.short_type){ - if (v <= UInt16.MaxValue) - return (short) v; - } else if (target_type == TypeManager.ushort_type){ - if (v <= UInt16.MaxValue) - return (ushort) v; - } else if (target_type == TypeManager.int64_type){ - if (v <= Int64.MaxValue) - return (long) v; - } - s = v.ToString (); - } else if (c is ByteConstant){ - byte v = ((ByteConstant) c).Value; - - if (target_type == TypeManager.int32_type) - return (int) v; - else if (target_type == TypeManager.uint32_type) - return (uint) v; - else if (target_type == TypeManager.char_type) - return (char) v; - else if (target_type == TypeManager.sbyte_type){ - if (v <= SByte.MaxValue) - return (sbyte) v; - } else if (target_type == TypeManager.short_type) - return (short) v; - else if (target_type == TypeManager.ushort_type) - return (ushort) v; - else if (target_type == TypeManager.int64_type) - return (long) v; - else if (target_type == TypeManager.uint64_type) - return (ulong) v; - s = v.ToString (); - } else if (c is SByteConstant){ - sbyte v = ((SByteConstant) c).Value; - - if (target_type == TypeManager.int32_type) - return (int) v; - else if (target_type == TypeManager.uint32_type){ - if (v >= 0) - return (uint) v; - } else if (target_type == TypeManager.char_type){ - if (v >= 0) - return (char) v; - } else if (target_type == TypeManager.byte_type){ - if (v >= 0) - return (byte) v; - } else if (target_type == TypeManager.short_type) - return (short) v; - else if (target_type == TypeManager.ushort_type){ - if (v >= 0) - return (ushort) v; - } else if (target_type == TypeManager.int64_type) - return (long) v; - else if (target_type == TypeManager.uint64_type){ - if (v >= 0) - return (ulong) v; - } - s = v.ToString (); - } else if (c is ShortConstant){ - short v = ((ShortConstant) c).Value; - - if (target_type == TypeManager.int32_type){ - return (int) v; - } else if (target_type == TypeManager.uint32_type){ - if (v >= 0) - return (uint) v; - } else if (target_type == TypeManager.char_type){ - if (v >= 0) - return (char) v; - } else if (target_type == TypeManager.byte_type){ - if (v >= Byte.MinValue && v <= Byte.MaxValue) - return (byte) v; - } else if (target_type == TypeManager.sbyte_type){ - if (v >= SByte.MinValue && v <= SByte.MaxValue) - return (sbyte) v; - } else if (target_type == TypeManager.ushort_type){ - if (v >= 0) - return (ushort) v; - } else if (target_type == TypeManager.int64_type) - return (long) v; - else if (target_type == TypeManager.uint64_type) - return (ulong) v; - - s = v.ToString (); - } else if (c is UShortConstant){ - ushort v = ((UShortConstant) c).Value; - - if (target_type == TypeManager.int32_type) - return (int) v; - else if (target_type == TypeManager.uint32_type) - return (uint) v; - else if (target_type == TypeManager.char_type){ - if (v >= Char.MinValue && v <= Char.MaxValue) - return (char) v; - } else if (target_type == TypeManager.byte_type){ - if (v >= Byte.MinValue && v <= Byte.MaxValue) - return (byte) v; - } else if (target_type == TypeManager.sbyte_type){ - if (v <= SByte.MaxValue) - return (byte) v; - } else if (target_type == TypeManager.short_type){ - if (v <= Int16.MaxValue) - return (short) v; - } else if (target_type == TypeManager.int64_type) - return (long) v; - else if (target_type == TypeManager.uint64_type) - return (ulong) v; - - s = v.ToString (); - } else if (c is CharConstant){ - char v = ((CharConstant) c).Value; - - if (target_type == TypeManager.int32_type) - return (int) v; - else if (target_type == TypeManager.uint32_type) - return (uint) v; - else if (target_type == TypeManager.byte_type){ - if (v >= Byte.MinValue && v <= Byte.MaxValue) - return (byte) v; - } else if (target_type == TypeManager.sbyte_type){ - if (v <= SByte.MaxValue) - return (sbyte) v; - } else if (target_type == TypeManager.short_type){ - if (v <= Int16.MaxValue) - return (short) v; - } else if (target_type == TypeManager.ushort_type) - return (short) v; - else if (target_type == TypeManager.int64_type) - return (long) v; - else if (target_type == TypeManager.uint64_type) - return (ulong) v; - - s = v.ToString (); - } - Error_ConstantValueCannotBeConverted (loc, s, target_type); - return null; + Report.Error (214, loc, "Pointers and fixed size buffers may only be used in an unsafe context"); } // @@ -1290,6 +1041,11 @@ namespace Mono.CSharp { { Report.Error (248, loc, "Cannot create an array with a negative size"); } + + protected void Error_CannotCallAbstractBase (string name) + { + Report.Error (205, loc, "Cannot call an abstract base member `{0}'", name); + } // // Converts `source' to an int, uint, long or ulong. @@ -1414,6 +1170,104 @@ namespace Mono.CSharp { child.Emit (ec); } } + /// + /// This is a numeric cast to a Decimal + /// + public class CastToDecimal : EmptyCast { + + MethodInfo conversion_operator; + + public CastToDecimal (EmitContext ec, Expression child) + : this (ec, child, false) + { + } + + public CastToDecimal (EmitContext ec, Expression child, bool find_explicit) + : base (child, TypeManager.decimal_type) + { + conversion_operator = GetConversionOperator (ec, find_explicit); + + if (conversion_operator == null) + Convert.Error_CannotImplicitConversion (loc, child.Type, type); + } + + // Returns the implicit operator that converts from + // 'child.Type' to System.Decimal. + MethodInfo GetConversionOperator (EmitContext ec, bool find_explicit) + { + string operator_name = "op_Implicit"; + + if (find_explicit) + operator_name = "op_Explicit"; + + MethodGroupExpr opers = Expression.MethodLookup ( + ec, type, operator_name, loc) as MethodGroupExpr; + + if (opers == null) + Convert.Error_CannotImplicitConversion (loc, child.Type, type); + + foreach (MethodInfo oper in opers.Methods) { + ParameterData pd = TypeManager.GetParameterData (oper); + + if (pd.ParameterType (0) == child.Type && oper.ReturnType == type) + return oper; + } + + return null; + } + public override void Emit (EmitContext ec) + { + ILGenerator ig = ec.ig; + child.Emit (ec); + + ig.Emit (OpCodes.Call, conversion_operator); + } + } + /// + /// This is an explicit numeric cast from a Decimal + /// + public class CastFromDecimal : EmptyCast + { + MethodInfo conversion_operator; + public CastFromDecimal (EmitContext ec, Expression child, Type return_type) + : base (child, return_type) + { + if (child.Type != TypeManager.decimal_type) + throw new InternalErrorException ( + "The expected type is Decimal, instead it is " + child.Type.FullName); + + conversion_operator = GetConversionOperator (ec); + if (conversion_operator == null) + Convert.Error_CannotImplicitConversion (loc, child.Type, type); + } + + // Returns the explicit operator that converts from an + // express of type System.Decimal to 'type'. + MethodInfo GetConversionOperator (EmitContext ec) + { + MethodGroupExpr opers = Expression.MethodLookup ( + ec, child.Type, "op_Explicit", loc) as MethodGroupExpr; + + if (opers == null) + Convert.Error_CannotImplicitConversion (loc, child.Type, type); + + foreach (MethodInfo oper in opers.Methods) { + ParameterData pd = TypeManager.GetParameterData (oper); + + if (pd.ParameterType (0) == child.Type && oper.ReturnType == type) + return oper; + } + + return null; + } + public override void Emit (EmitContext ec) + { + ILGenerator ig = ec.ig; + child.Emit (ec); + + ig.Emit (OpCodes.Call, conversion_operator); + } + } // // We need to special case this since an empty cast of @@ -1452,6 +1306,11 @@ namespace Mono.CSharp { child.Emit (ec); } + public override Constant Increment () + { + throw new NotSupportedException (); + } + public override bool IsDefaultValue { get { throw new NotImplementedException (); @@ -1497,69 +1356,21 @@ namespace Mono.CSharp { return Child.GetValue (); } - public object GetValueAsEnumType () + public override object GetTypedValue () { + // FIXME: runtime is not ready to work with just emited enums + if (!RootContext.StdLib) { + return Child.GetValue (); + } + return System.Enum.ToObject (type, Child.GetValue ()); } - - // - // Converts from one of the valid underlying types for an enumeration - // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to - // one of the internal compiler literals: Int/UInt/Long/ULong Literals. - // - public Constant WidenToCompilerConstant () + + public override void Error_ValueCannotBeConverted (Location loc, Type t) { - Type t = TypeManager.EnumToUnderlying (Child.Type); - object v = ((Constant) Child).GetValue ();; - - if (t == TypeManager.int32_type) - return new IntConstant ((int) v); - if (t == TypeManager.uint32_type) - return new UIntConstant ((uint) v); - if (t == TypeManager.int64_type) - return new LongConstant ((long) v); - if (t == TypeManager.uint64_type) - return new ULongConstant ((ulong) v); - if (t == TypeManager.short_type) - return new ShortConstant ((short) v); - if (t == TypeManager.ushort_type) - return new UShortConstant ((ushort) v); - if (t == TypeManager.byte_type) - return new ByteConstant ((byte) v); - if (t == TypeManager.sbyte_type) - return new SByteConstant ((sbyte) v); - - throw new Exception ("Invalid enumeration underlying type: " + t); + Convert.Error_CannotImplicitConversion (loc, Type, t); } - // - // Extracts the value in the enumeration on its native representation - // - public object GetPlainValue () - { - Type t = TypeManager.EnumToUnderlying (Child.Type); - object v = ((Constant) Child).GetValue ();; - - if (t == TypeManager.int32_type) - return (int) v; - if (t == TypeManager.uint32_type) - return (uint) v; - if (t == TypeManager.int64_type) - return (long) v; - if (t == TypeManager.uint64_type) - return (ulong) v; - if (t == TypeManager.short_type) - return (short) v; - if (t == TypeManager.ushort_type) - return (ushort) v; - if (t == TypeManager.byte_type) - return (byte) v; - if (t == TypeManager.sbyte_type) - return (sbyte) v; - - return null; - } - public override string AsString () { return Child.AsString (); @@ -1595,6 +1406,11 @@ namespace Mono.CSharp { return Child.ConvertToInt (); } + public override Constant Increment() + { + return new EnumConstant (Child.Increment (), type); + } + public override bool IsDefaultValue { get { return Child.IsDefaultValue; @@ -1610,6 +1426,27 @@ namespace Mono.CSharp { return Child.IsNegative; } } + + public override Constant ToType (Type type, Location loc) + { + if (Type == type) { + // This is workaround of mono bug. It can be removed when the latest corlib spreads enough + if (TypeManager.IsEnumType (type.UnderlyingSystemType)) + return this; + + if (type.UnderlyingSystemType != Child.Type) + Child = Child.ToType (type.UnderlyingSystemType, loc); + return this; + } + + if (!Convert.ImplicitStandardConversionExists (Convert.ConstantEC, this, type)){ + Error_ValueCannotBeConverted (loc, type); + return null; + } + + return Child.ToType (type, loc); + } + } /// @@ -1620,12 +1457,6 @@ namespace Mono.CSharp { /// public class BoxedCast : EmptyCast { - public BoxedCast (Expression expr) - : base (expr, TypeManager.object_type) - { - eclass = ExprClass.Value; - } - public BoxedCast (Expression expr, Type target_type) : base (expr, target_type) { @@ -2030,15 +1861,17 @@ namespace Mono.CSharp { public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name) { if (ec.IsFieldInitializer) + Report.Error (236, l, + "A field initializer cannot reference the nonstatic field, method, or property `{0}'", + name); + else { + if (name.LastIndexOf ('.') > 0) + name = name.Substring (name.LastIndexOf ('.') + 1); + Report.Error ( - 236, l, - "A field initializer cannot reference the non-static field, " + - "method or property `"+name+"'"); - else - Report.Error ( - 120, l, - "An object reference is required " + - "for the non-static field `"+name+"'"); + 120, l, "`{0}': An object reference is required for the nonstatic field, method or property", + name); + } } public bool IdenticalNameAndTypeName (EmitContext ec, Expression resolved_to, Location loc) @@ -2118,30 +1951,56 @@ namespace Mono.CSharp { return null; } - public override FullNamedExpression ResolveAsTypeStep (EmitContext ec) + public override FullNamedExpression ResolveAsTypeStep (EmitContext ec, bool silent) { - FullNamedExpression dt = ec.DeclSpace.LookupGeneric (Name, loc); - if (dt != null) - return dt.ResolveAsTypeStep (ec); + FullNamedExpression fne = ec.DeclSpace.LookupGeneric (Name, loc); + if (fne != null) + return fne.ResolveAsTypeStep (ec, silent); int errors = Report.Errors; - dt = ec.DeclSpace.LookupType (Name, loc, /*ignore_cs0104=*/ false); - if (Report.Errors != errors) - return null; + fne = ec.DeclSpace.LookupType (Name, loc, /*ignore_cs0104=*/ false); + + if (fne != null) { + if (fne.Type == null) + return fne; - if ((dt == null) || (dt.Type == null)) - return dt; + FullNamedExpression nested = ResolveNested (ec, fne.Type); + if (nested != null) + return nested.ResolveAsTypeStep (ec); - FullNamedExpression nested = ResolveNested (ec, dt.Type); - if (nested != null) - return nested.ResolveAsTypeStep (ec); + if (Arguments != null) { + ConstructedType ct = new ConstructedType (fne, Arguments, loc); + return ct.ResolveAsTypeStep (ec); + } - if (Arguments != null) { - ConstructedType ct = new ConstructedType (dt, Arguments, loc); - return ct.ResolveAsTypeStep (ec); + return fne; } - return dt; + if (silent || errors != Report.Errors) + return null; + + MemberCore mc = ec.DeclSpace.GetDefinition (Name); + if (mc != null) { + Error_UnexpectedKind (ec, "type", GetMemberType (mc), loc); + } else { + NamespaceEntry.Error_NamespaceNotFound (loc, Name); + } + + return null; + } + + // TODO: I am still not convinced about this. If someone else will need it + // implement this as virtual property in MemberCore hierarchy + string GetMemberType (MemberCore mc) + { + if (mc is PropertyBase) + return "property"; + if (mc is Indexer) + return "indexer"; + if (mc is FieldBase) + return "field"; + + return "type"; } Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool intermediate) @@ -2188,20 +2047,21 @@ namespace Mono.CSharp { if (current_block != null){ LocalInfo vi = current_block.GetLocalInfo (Name); if (vi != null){ - Expression var; - - var = new LocalVariableReference (ec.CurrentBlock, Name, loc); - - if (right_side != null) - return var.ResolveLValue (ec, right_side); - else - return var.Resolve (ec); + LocalVariableReference var = new LocalVariableReference (ec.CurrentBlock, Name, loc); + if (right_side != null) { + return var.ResolveLValue (ec, right_side, loc); + } else { + ResolveFlags rf = ResolveFlags.VariableOrValue; + if (intermediate) + rf |= ResolveFlags.DisableFlowAnalysis; + return var.Resolve (ec, rf); + } } ParameterReference pref = current_block.Toplevel.GetParameterReference (Name, loc); if (pref != null) { if (right_side != null) - return pref.ResolveLValue (ec, right_side); + return pref.ResolveLValue (ec, right_side, loc); else return pref.Resolve (ec); } @@ -2238,7 +2098,7 @@ namespace Mono.CSharp { almost_matched_type = ec.ContainerType; almost_matched = (ArrayList) almostMatchedMembers.Clone (); } - e = ResolveAsTypeStep (ec); + e = ResolveAsTypeStep (ec, false); } if (e == null) { @@ -2268,7 +2128,7 @@ namespace Mono.CSharp { if (!me.IsStatic && (!intermediate || !IdenticalNameAndTypeName (ec, me, loc))) { - Error_ObjectRefRequired (ec, loc, Name); + Error_ObjectRefRequired (ec, loc, me.GetSignatureForError ()); return null; } @@ -2304,9 +2164,8 @@ namespace Mono.CSharp { me.InstanceExpression.Type != me.DeclaringType && !TypeManager.IsFamilyAccessible (me.InstanceExpression.Type, me.DeclaringType) && (!intermediate || !IdenticalNameAndTypeName (ec, e, loc))) { - Error (38, "Cannot access nonstatic member `" + me.Name + "' of " + - "outer type `" + me.DeclaringType + "' via nested type `" + - me.InstanceExpression.Type + "'"); + Report.Error (38, loc, "Cannot access a nonstatic member of outer type `{0}' via nested type `{1}'", + TypeManager.CSharpName (me.DeclaringType), TypeManager.CSharpName (me.InstanceExpression.Type)); return null; } @@ -2334,6 +2193,11 @@ namespace Mono.CSharp { { return Name; } + + public override string GetSignatureForError () + { + return Name; + } } /// @@ -2341,7 +2205,7 @@ namespace Mono.CSharp { /// section 10.8.1 (Fully Qualified Names). /// public abstract class FullNamedExpression : Expression { - public override FullNamedExpression ResolveAsTypeStep (EmitContext ec) + public override FullNamedExpression ResolveAsTypeStep (EmitContext ec, bool silent) { return this; } @@ -2355,7 +2219,7 @@ namespace Mono.CSharp { /// Fully resolved expression that evaluates to a type /// public abstract class TypeExpr : FullNamedExpression { - override public FullNamedExpression ResolveAsTypeStep (EmitContext ec) + override public FullNamedExpression ResolveAsTypeStep (EmitContext ec, bool silent) { TypeExpr t = DoResolveAsTypeStep (ec); if (t == null) @@ -2462,15 +2326,11 @@ namespace Mono.CSharp { } public override string Name { - get { - return Type.ToString (); - } + get { return Type.ToString (); } } public override string FullName { - get { - return TypeManager.GetTypeName (Type); - } + get { return Type.FullName; } } } @@ -2487,36 +2347,73 @@ namespace Mono.CSharp { this.name = name; } + static readonly char [] dot_array = { '.' }; protected override TypeExpr DoResolveAsTypeStep (EmitContext ec) { - if (type == null) { - FullNamedExpression t = ec.DeclSpace.LookupType (name, Location.Null, /*ignore_cs0104=*/ false); - if (t == null) { - Report.Error (246, loc, "Cannot find type `" + name + "'"); - return null; + if (type != null) + return this; + + // If name is of the form `N.I', first lookup `N', then search a member `I' in it. + string rest = null; + string lookup_name = name; + int pos = name.IndexOf ('.'); + if (pos >= 0) { + rest = name.Substring (pos + 1); + lookup_name = name.Substring (0, pos); + } + + FullNamedExpression resolved = Namespace.Root.Lookup (ec.DeclSpace, lookup_name, Location.Null); + + if (resolved != null && rest != null) { + // Now handle the rest of the the name. + string [] elements = rest.Split (dot_array); + string element; + int count = elements.Length; + int i = 0; + while (i < count && resolved != null && resolved is Namespace) { + Namespace ns = resolved as Namespace; + element = elements [i++]; + lookup_name += "." + element; + resolved = ns.Lookup (ec.DeclSpace, element, Location.Null); } - if (!(t is TypeExpr)) { - Report.Error (118, Location, "'{0}' denotes a '{1}', where a type was expected", - t.FullName, t.ExprClassName ()); - return null; + if (resolved != null && resolved is TypeExpr) { + Type t = ((TypeExpr) resolved).Type; + while (t != null) { + if (!ec.DeclSpace.CheckAccessLevel (t)) { + resolved = null; + lookup_name = t.FullName; + break; + } + if (i == count) { + type = t; + return this; + } + t = TypeManager.GetNestedType (t, elements [i++]); + } } - type = ((TypeExpr) t).ResolveType (ec); } + if (resolved == null) { + NamespaceEntry.Error_NamespaceNotFound (loc, lookup_name); + return null; + } + + if (!(resolved is TypeExpr)) { + resolved.Error_UnexpectedKind (ec, "type", loc); + return null; + } + + type = ((TypeExpr) resolved).ResolveType (ec); return this; } public override string Name { - get { - return name; - } + get { return name; } } public override string FullName { - get { - return name; - } + get { return name; } } } @@ -2524,10 +2421,41 @@ namespace Mono.CSharp { /// Represents an "unbound generic type", ie. typeof (Foo<>). /// See 14.5.11. /// - public class UnboundTypeExpression : TypeLookupExpression { - public UnboundTypeExpression (string name) - : base (name) - { } + public class UnboundTypeExpression : TypeExpr + { + MemberName name; + + public UnboundTypeExpression (MemberName name, Location l) + { + this.name = name; + loc = l; + } + + protected override TypeExpr DoResolveAsTypeStep (EmitContext ec) + { + Expression expr; + if (name.Left != null) { + Expression lexpr = name.Left.GetTypeExpression (); + expr = new MemberAccess (lexpr, name.Basename, loc); + } else { + expr = new SimpleName (name.Basename, loc); + } + + FullNamedExpression fne = expr.ResolveAsTypeStep (ec); + if (fne == null) + return null; + + type = fne.Type; + return new TypeExpression (type, loc); + } + + public override string Name { + get { return name.FullName; } + } + + public override string FullName { + get { return name.FullName; } + } } public class TypeAliasExpression : TypeExpr { @@ -2657,8 +2585,8 @@ namespace Mono.CSharp { public static void error176 (Location loc, string name) { - Report.Error (176, loc, "Static member `" + name + "' cannot be accessed " + - "with an instance reference, qualify with a type name instead"); + Report.Error (176, loc, "Static member `{0}' cannot be accessed " + + "with an instance reference, qualify it with a type name instead", name); } @@ -2685,7 +2613,7 @@ namespace Mono.CSharp { if (original != null && original.IdenticalNameAndTypeName (ec, left, loc)) return this; - error176 (loc, Name); + error176 (loc, GetSignatureForError ()); return null; } @@ -2693,6 +2621,32 @@ namespace Mono.CSharp { return this; } + + protected void EmitInstance (EmitContext ec, bool prepare_for_load) + { + if (IsStatic) + return; + + if (InstanceExpression == EmptyExpression.Null) { + SimpleName.Error_ObjectRefRequired (ec, loc, Name); + return; + } + + if (InstanceExpression.Type.IsValueType) { + if (InstanceExpression is IMemoryLocation) { + ((IMemoryLocation) InstanceExpression).AddressOf (ec, AddressOp.LoadStore); + } else { + LocalTemporary t = new LocalTemporary (ec, InstanceExpression.Type); + InstanceExpression.Emit (ec); + t.Store (ec); + t.AddressOf (ec, AddressOp.Store); + } + } else + InstanceExpression.Emit (ec); + + if (prepare_for_load) + ec.ig.Emit (OpCodes.Dup); + } } /// @@ -2775,10 +2729,14 @@ namespace Mono.CSharp { } } + public override string GetSignatureForError () + { + return TypeManager.CSharpSignature (Methods [0]); + } + public override string Name { get { - //return Methods [0].Name; - return Methods [Methods.Length - 1].Name; + return Methods [0].Name; } } @@ -2899,7 +2857,7 @@ namespace Mono.CSharp { if (gen_params.Length != atypes.Length) continue; - list.Add (mi.BindGenericParameters (atypes)); + list.Add (mi.MakeGenericMethod (atypes)); } if (list.Count > 0) { @@ -2972,6 +2930,11 @@ namespace Mono.CSharp { } } + public override string GetSignatureForError () + { + return TypeManager.GetFullNameSignature (FieldInfo); + } + public VariableInfo VariableInfo { get { return variable_info; @@ -2981,39 +2944,31 @@ namespace Mono.CSharp { public override Expression ResolveMemberAccess (EmitContext ec, Expression left, Location loc, SimpleName original) { - bool left_is_type = left is TypeExpr; - FieldInfo fi = FieldInfo.Mono_GetGenericFieldDefinition (); - Type decl_type = fi.DeclaringType; - - bool is_emitted = fi is FieldBuilder; - Type t = fi.FieldType; - - if (is_emitted) { - Const c = TypeManager.LookupConstant ((FieldBuilder) fi); - - if (c != null) { - object o; - if (!c.LookupConstantValue (out o)) - return null; - - c.SetMemberIsUsed (); - object real_value = ((Constant) c.Expr).GetValue (); + if (fi.IsLiteral) { + IConstant ic = TypeManager.GetConstant (fi); + if (ic == null) { + ic = new ExternalConstant (fi); + TypeManager.RegisterConstant (fi, ic); + } - Expression exp = Constantify (real_value, t); - - if (!left_is_type && - (original == null || !original.IdenticalNameAndTypeName (ec, left, loc))) { - Report.SymbolRelatedToPreviousError (c); - error176 (loc, c.GetSignatureForError ()); - return null; - } - - return exp; + bool left_is_type = left is TypeExpr; + if (!left_is_type && (original == null || !original.IdenticalNameAndTypeName (ec, left, loc))) { + Report.SymbolRelatedToPreviousError (FieldInfo); + error176 (loc, TypeManager.GetFullNameSignature (FieldInfo)); + return null; } + + if (ic.ResolveValue ()) + ic.CheckObsoleteness (loc); + + return ic.Value; } + bool is_emitted = fi is FieldBuilder; + Type t = fi.FieldType; + // // Decimal constants cannot be encoded in the constant blob, and thus are marked // as IsInitOnly ('readonly' in C# parlance). We get its value from the @@ -3025,46 +2980,6 @@ namespace Mono.CSharp { return new DecimalConstant (((System.Runtime.CompilerServices.DecimalConstantAttribute) attrs [0]).Value); } - if (fi.IsLiteral) { - object o; - - if (is_emitted) - o = TypeManager.GetValue ((FieldBuilder) fi); - else - o = fi.GetValue (fi); - - if (decl_type.IsSubclassOf (TypeManager.enum_type)) { - if (!left_is_type && - (original == null || !original.IdenticalNameAndTypeName (ec, left, loc))) { - error176 (loc, fi.Name); - return null; - } - - Expression enum_member = MemberLookup ( - ec, decl_type, "value__", MemberTypes.Field, - AllBindingFlags | BindingFlags.NonPublic, loc); - - Enum en = TypeManager.LookupEnum (decl_type); - - Constant c; - if (en != null) - c = Constantify (o, en.UnderlyingType); - else - c = Constantify (o, enum_member.Type); - - return new EnumConstant (c, decl_type); - } - - Expression exp = Constantify (o, t); - - if (!left_is_type) { - error176 (loc, fi.Name); - return null; - } - - return exp; - } - if (t.IsPointer && !ec.InUnsafe) { UnsafeError (loc); return null; @@ -3078,17 +2993,17 @@ namespace Mono.CSharp { if (ec.InRefOutArgumentResolving && FieldInfo.IsInitOnly && !ec.IsConstructor && FieldInfo.FieldType.IsValueType) { if (FieldInfo.FieldType is TypeBuilder) { if (FieldInfo.IsStatic) - Report.Error (1651, loc, "Members of readonly static field '{0}.{1}' cannot be passed ref or out (except in a constructor)", - TypeManager.CSharpName (DeclaringType), Name); + Report.Error (1651, loc, "Fields of static readonly field `{0}' cannot be passed ref or out (except in a static constructor)", + GetSignatureForError ()); else - Report.Error (1649, loc, "Members of readonly field '{0}.{1}' cannot be passed ref or out (except in a constructor)", + Report.Error (1649, loc, "Members of readonly field `{0}.{1}' cannot be passed ref or out (except in a constructor)", TypeManager.CSharpName (DeclaringType), Name); } else { if (FieldInfo.IsStatic) - Report.Error (199, loc, "A static readonly field '{0}' cannot be passed ref or out (except in a static constructor)", + Report.Error (199, loc, "A static readonly field `{0}' cannot be passed ref or out (except in a static constructor)", Name); else - Report.Error (192, loc, "A readonly field '{0}' cannot be passed ref or out (except in a constructor)", + Report.Error (192, loc, "A readonly field `{0}' cannot be passed ref or out (except in a constructor)", Name); } return null; @@ -3107,8 +3022,8 @@ namespace Mono.CSharp { // Resolve the field's instance expression while flow analysis is turned // off: when accessing a field "a.b", we must check whether the field // "a.b" is initialized, not whether the whole struct "a" is initialized. - InstanceExpression = InstanceExpression.Resolve (ec, ResolveFlags.VariableOrValue | - ResolveFlags.DisableFlowAnalysis); + InstanceExpression = InstanceExpression.Resolve ( + ec, ResolveFlags.VariableOrValue | ResolveFlags.DisableFlowAnalysis); if (InstanceExpression == null) return null; } @@ -3117,9 +3032,7 @@ namespace Mono.CSharp { ObsoleteAttribute oa; FieldBase f = TypeManager.GetField (FieldInfo); if (f != null) { - oa = f.GetObsoleteAttribute (f.Parent); - if (oa != null) - AttributeTester.Report_ObsoleteMessage (oa, f.GetSignatureForError (), loc); + f.CheckObsoleteness (loc); // To be sure that type is external because we do not register generated fields } else if (!(FieldInfo.DeclaringType is TypeBuilder)) { oa = AttributeTester.GetMemberObsoleteAttribute (FieldInfo); @@ -3128,14 +3041,18 @@ namespace Mono.CSharp { } } - if (ec.CurrentAnonymousMethod != null){ + AnonymousContainer am = ec.CurrentAnonymousMethod; + if (am != null){ if (!FieldInfo.IsStatic){ - if (!ec.CurrentAnonymousMethod.IsIterator && (ec.TypeContainer is Struct)){ - Report.Error (1673, loc, "Can not reference instance variables in anonymous methods hosted in structs"); + if (!am.IsIterator && (ec.TypeContainer is Struct)){ + Report.Error (1673, loc, + "Anonymous methods inside structs cannot access instance members of `{0}'. Consider copying `{0}' to a local variable outside the anonymous method and using the local instead", + "this"); return null; } - ec.CaptureField (this); - } + if ((am.ContainerAnonymousMethod == null) && (InstanceExpression is This)) + ec.CaptureField (this); + } } // If the instance expression is a local variable or parameter. @@ -3156,11 +3073,9 @@ namespace Mono.CSharp { string msg; if (is_instance) - msg = "Readonly field can not be assigned outside " + - "of constructor or variable initializer"; + msg = "A readonly field cannot be assigned to (except in a constructor or a variable initializer)"; else - msg = "A static readonly field can only be assigned in " + - "a static constructor"; + msg = "A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)"; Report.Error (is_instance ? 191 : 198, loc, msg); } @@ -3177,8 +3092,8 @@ namespace Mono.CSharp { return null; if (!FieldInfo.IsStatic && (InstanceExpression.Type.IsValueType && !(InstanceExpression is IMemoryLocation))) { - // FIXME: Provide better error reporting. - Error (1612, "Cannot modify expression because it is not a variable."); + Report.Error (1612, loc, "Cannot modify the return value of `{0}' because it is not a variable", + InstanceExpression.GetSignatureForError ()); return null; } @@ -3216,17 +3131,18 @@ namespace Mono.CSharp { { if (!IsStatic && Type.IsValueType && !container.IsSubclassOf (TypeManager.mbr_type) && DeclaringType.IsSubclassOf (TypeManager.mbr_type)) { Report.SymbolRelatedToPreviousError (DeclaringType); - Report.Error (1690, loc, "Cannot call '{0}' method, property, or indexer because it is a value type member of a marshal-by-reference class", Name); + Report.Error (1690, loc, "Cannot call methods, properties, or indexers on `{0}' because it is a value type member of a marshal-by-reference class", + GetSignatureForError ()); } } - public bool VerifyFixed (bool is_expression) + public bool VerifyFixed () { IVariable variable = InstanceExpression as IVariable; - if ((variable == null) || !variable.VerifyFixed (true)) - return false; - - return true; + // A variable of the form V.I is fixed when V is a fixed variable of a struct type. + // We defer the InstanceExpression check after the variable check to avoid a + // separate null check on InstanceExpression. + return variable != null && InstanceExpression.Type.IsValueType && variable.VerifyFixed (); } public override int GetHashCode() @@ -3272,7 +3188,7 @@ namespace Mono.CSharp { ig.Emit (OpCodes.Ldsfld, FieldInfo); } else { if (!prepared) - EmitInstance (ec); + EmitInstance (ec, false); if (is_volatile) ig.Emit (OpCodes.Volatile); @@ -3310,11 +3226,7 @@ namespace Mono.CSharp { return; } - if (!is_static) { - EmitInstance (ec); - if (prepare_for_load) - ig.Emit (OpCodes.Dup); - } + EmitInstance (ec, prepare_for_load); source.Emit (ec); if (leave_copy) { @@ -3331,7 +3243,7 @@ namespace Mono.CSharp { if ((f.ModFlags & Modifiers.VOLATILE) != 0) ig.Emit (OpCodes.Volatile); - f.status |= Field.Status.ASSIGNED; + f.SetAssigned (); } } @@ -3344,21 +3256,6 @@ namespace Mono.CSharp { temp.Emit (ec); } - void EmitInstance (EmitContext ec) - { - if (InstanceExpression.Type.IsValueType) { - if (InstanceExpression is IMemoryLocation) { - ((IMemoryLocation) InstanceExpression).AddressOf (ec, AddressOp.LoadStore); - } else { - LocalTemporary t = new LocalTemporary (ec, InstanceExpression.Type); - InstanceExpression.Emit (ec); - t.Store (ec); - t.AddressOf (ec, AddressOp.Store); - } - } else - InstanceExpression.Emit (ec); - } - public override void Emit (EmitContext ec) { Emit (ec, false); @@ -3372,12 +3269,13 @@ namespace Mono.CSharp { FieldBase f = TypeManager.GetField (FieldInfo); if (f != null){ if ((f.ModFlags & Modifiers.VOLATILE) != 0){ - Error (676, "volatile variable: can not take its address, or pass as ref/out parameter"); + Report.Warning (420, 1, loc, "`{0}': A volatile fields cannot be passed using a ref or out parameter", + f.GetSignatureForError ()); return; } if ((mode & AddressOp.Store) != 0) - f.status |= Field.Status.ASSIGNED; + f.SetAssigned (); if ((mode & AddressOp.Load) != 0) f.SetMemberIsUsed (); } @@ -3413,7 +3311,7 @@ namespace Mono.CSharp { if (FieldInfo.IsStatic){ ig.Emit (OpCodes.Ldsflda, FieldInfo); } else { - EmitInstance (ec); + EmitInstance (ec, false); ig.Emit (OpCodes.Ldflda, FieldInfo); } } @@ -3493,16 +3391,9 @@ namespace Mono.CSharp { } } - public bool VerifyAssignable () + public override string GetSignatureForError () { - if (setter == null) { - Report.Error (200, loc, - "The property `" + PropertyInfo.Name + - "' can not be assigned to, as it has not set accessor"); - return false; - } - - return true; + return TypeManager.GetFullNameSignature (PropertyInfo); } void FindAccessors (Type invocation_type) @@ -3568,23 +3459,23 @@ namespace Mono.CSharp { bool InstanceResolve (EmitContext ec, bool must_do_cs1540_check) { - if ((InstanceExpression == null) && ec.IsStatic && !is_static) { + if (is_static) { + InstanceExpression = null; + return true; + } + + if (InstanceExpression == null) { SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name); return false; } - if (!IsInstance || InstanceExpression == EmptyExpression.Null) - InstanceExpression = null; - - if (InstanceExpression != null) { - InstanceExpression = InstanceExpression.DoResolve (ec); - if (InstanceExpression == null) - return false; - - InstanceExpression.CheckMarshallByRefAccess (ec.ContainerType); - } + InstanceExpression = InstanceExpression.DoResolve (ec); + if (InstanceExpression == null) + return false; + + InstanceExpression.CheckMarshallByRefAccess (ec.ContainerType); - if (must_do_cs1540_check && (InstanceExpression != null)) { + if (must_do_cs1540_check && InstanceExpression != EmptyExpression.Null) { if ((InstanceExpression.Type != ec.ContainerType) && ec.ContainerType.IsSubclassOf (InstanceExpression.Type)) { Report.Error (1540, loc, "Cannot access protected member `" + @@ -3600,6 +3491,26 @@ namespace Mono.CSharp { return true; } + + void Error_PropertyNotFound (MethodInfo mi, bool getter) + { + // TODO: correctly we should compare arguments but it will lead to bigger changes + if (mi is MethodBuilder) { + Error_TypeDoesNotContainDefinition (loc, PropertyInfo.DeclaringType, Name); + return; + } + + StringBuilder sig = new StringBuilder (TypeManager.CSharpName (mi.DeclaringType)); + sig.Append ('.'); + ParameterData iparams = TypeManager.GetParameterData (mi); + sig.Append (getter ? "get_" : "set_"); + sig.Append (Name); + sig.Append (iparams.GetSignatureForError ()); + + Report.SymbolRelatedToPreviousError (mi); + Report.Error (1546, loc, "Property `{0}' is not supported by the C# language. Try to call the accessor method `{1}' directly", + Name, sig.ToString ()); + } override public Expression DoResolve (EmitContext ec) { @@ -3608,10 +3519,7 @@ namespace Mono.CSharp { if (getter != null){ if (TypeManager.GetArgumentTypes (getter).Length != 0){ - Report.Error ( - 117, loc, "`{0}' does not contain a " + - "definition for `{1}'.", getter.DeclaringType, - Name); + Error_PropertyNotFound (getter, true); return null; } } @@ -3627,10 +3535,8 @@ namespace Mono.CSharp { return null; if (InstanceExpression != EmptyExpression.Null) { - Report.Error (154, loc, - "The property `" + PropertyInfo.Name + - "' can not be used in " + - "this context because it lacks a get accessor"); + Report.Error (154, loc, "The property or indexer `{0}' cannot be used in this context because it lacks the `get' accessor", + TypeManager.GetFullNameSignature (PropertyInfo)); return null; } } @@ -3641,12 +3547,11 @@ namespace Mono.CSharp { PropertyBase.PropertyMethod pm = TypeManager.GetMethod (getter) as PropertyBase.PropertyMethod; if (pm != null && pm.HasCustomAccessModifier) { Report.SymbolRelatedToPreviousError (pm); - Report.Error (271, loc, "The property or indexer '{0}' cannot be used in this context because the get accessor is inaccessible", + Report.Error (271, loc, "The property or indexer `{0}' cannot be used in this context because the get accessor is inaccessible", TypeManager.CSharpSignature (getter)); } else - Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", - TypeManager.CSharpSignature (getter)); + ErrorIsInaccesible (loc, TypeManager.CSharpSignature (getter)); return null; } @@ -3656,9 +3561,8 @@ namespace Mono.CSharp { // // Only base will allow this invocation to happen. // - if (IsBase && getter.IsAbstract){ - Report.Error (205, loc, "Cannot call an abstract base property: " + - PropertyInfo.DeclaringType + "." +PropertyInfo.Name); + if (IsBase && getter.IsAbstract) { + Error_CannotCallAbstractBase (TypeManager.GetFullNameSignature (PropertyInfo)); return null; } @@ -3684,17 +3588,13 @@ namespace Mono.CSharp { if (getter == null) return null; - // TODO: Print better property name - Report.Error (200, loc, "Property or indexer '{0}' cannot be assigned to -- it is read only", - PropertyInfo.Name); + Report.Error (200, loc, " Property or indexer `{0}' cannot be assigned to (it is read only)", + TypeManager.GetFullNameSignature (PropertyInfo)); return null; } if (TypeManager.GetArgumentTypes (setter).Length != 1){ - Report.Error ( - 117, loc, "`{0}' does not contain a " + - "definition for `{1}'.", getter.DeclaringType, - Name); + Error_PropertyNotFound (setter, false); return null; } @@ -3703,12 +3603,11 @@ namespace Mono.CSharp { PropertyBase.PropertyMethod pm = TypeManager.GetMethod (setter) as PropertyBase.PropertyMethod; if (pm != null && pm.HasCustomAccessModifier) { Report.SymbolRelatedToPreviousError (pm); - Report.Error (272, loc, "The property or indexer '{0}' cannot be used in this context because the set accessor is inaccessible", + Report.Error (272, loc, "The property or indexer `{0}' cannot be used in this context because the set accessor is inaccessible", TypeManager.CSharpSignature (setter)); } else - Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", - TypeManager.CSharpSignature (setter)); + ErrorIsInaccesible (loc, TypeManager.CSharpSignature (setter)); return null; } @@ -3719,8 +3618,7 @@ namespace Mono.CSharp { // Only base will allow this invocation to happen. // if (IsBase && setter.IsAbstract){ - Report.Error (205, loc, "Cannot call an abstract base property: " + - PropertyInfo.DeclaringType + "." +PropertyInfo.Name); + Error_CannotCallAbstractBase (TypeManager.GetFullNameSignature (PropertyInfo)); return null; } @@ -3728,47 +3626,23 @@ namespace Mono.CSharp { // Check that we are not making changes to a temporary memory location // if (InstanceExpression != null && InstanceExpression.Type.IsValueType && !(InstanceExpression is IMemoryLocation)) { - // FIXME: Provide better error reporting. - Error (1612, "Cannot modify expression because it is not a variable."); + Report.Error (1612, loc, "Cannot modify the return value of `{0}' because it is not a variable", + InstanceExpression.GetSignatureForError ()); return null; } return this; } - - public override void Emit (EmitContext ec) { Emit (ec, false); } - void EmitInstance (EmitContext ec) - { - if (is_static) - return; - - if (InstanceExpression.Type.IsValueType) { - if (InstanceExpression is IMemoryLocation) { - ((IMemoryLocation) InstanceExpression).AddressOf (ec, AddressOp.LoadStore); - } else { - LocalTemporary t = new LocalTemporary (ec, InstanceExpression.Type); - InstanceExpression.Emit (ec); - t.Store (ec); - t.AddressOf (ec, AddressOp.Store); - } - } else - InstanceExpression.Emit (ec); - - if (prepared) - ec.ig.Emit (OpCodes.Dup); - } - - public void Emit (EmitContext ec, bool leave_copy) { if (!prepared) - EmitInstance (ec); + EmitInstance (ec, false); // // Special case: length of single dimension array property is turned into ldlen @@ -3807,7 +3681,7 @@ namespace Mono.CSharp { { prepared = prepare_for_load; - EmitInstance (ec); + EmitInstance (ec, prepare_for_load); source.Emit (ec); if (leave_copy) { @@ -3912,30 +3786,28 @@ namespace Mono.CSharp { bool InstanceResolve (EmitContext ec, bool must_do_cs1540_check) { - if ((InstanceExpression == null) && ec.IsStatic && !is_static) { + if (is_static) { + InstanceExpression = null; + return true; + } + + if (InstanceExpression == null) { SimpleName.Error_ObjectRefRequired (ec, loc, EventInfo.Name); return false; } - if (!IsInstance || InstanceExpression == EmptyExpression.Null) - InstanceExpression = null; - - if (InstanceExpression != null) { - InstanceExpression = InstanceExpression.DoResolve (ec); - if (InstanceExpression == null) - return false; - } + InstanceExpression = InstanceExpression.DoResolve (ec); + if (InstanceExpression == null) + return false; // // This is using the same mechanism as the CS1540 check in PropertyExpr. // However, in the Event case, we reported a CS0122 instead. // - if (must_do_cs1540_check && (InstanceExpression != null)) { + if (must_do_cs1540_check && InstanceExpression != EmptyExpression.Null) { if ((InstanceExpression.Type != ec.ContainerType) && ec.ContainerType.IsSubclassOf (InstanceExpression.Type)) { - Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", - DeclaringType.Name + "." + EventInfo.Name); - + ErrorIsInaccesible (loc, TypeManager.CSharpSignature (EventInfo)); return false; } } @@ -3950,22 +3822,11 @@ namespace Mono.CSharp { public override Expression DoResolve (EmitContext ec) { - if (!IsInstance) - InstanceExpression = null; - - if (InstanceExpression != null) { - InstanceExpression = InstanceExpression.DoResolve (ec); - if (InstanceExpression == null) - return null; - } - bool must_do_cs1540_check; if (!(IsAccessorAccessible (ec.ContainerType, add_accessor, out must_do_cs1540_check) && IsAccessorAccessible (ec.ContainerType, remove_accessor, out must_do_cs1540_check))) { - - Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", - DeclaringType.Name + "." + EventInfo.Name); - return null; + ErrorIsInaccesible (loc, TypeManager.CSharpSignature (EventInfo)); + return null; } if (!InstanceResolve (ec, must_do_cs1540_check)) @@ -3977,12 +3838,17 @@ namespace Mono.CSharp { public override void Emit (EmitContext ec) { if (InstanceExpression is This) - Report.Error (79, loc, "The event `{0}' can only appear on the left hand side of += or -=, try calling the actual delegate", Name); + Report.Error (79, loc, "The event `{0}' can only appear on the left hand side of += or -=", GetSignatureForError ()); else Report.Error (70, loc, "The event `{0}' can only appear on the left hand side of += or -= "+ "(except on the defining type)", Name); } + public override string GetSignatureForError () + { + return TypeManager.CSharpSignature (EventInfo); + } + public void EmitAddOrRemove (EmitContext ec, Expression source) { BinaryDelegate source_del = (BinaryDelegate) source;