2006-09-21 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / gmcs / ecore.cs
index 064b81cb90d93b17ea3ab33ca5859d9ef83a5689..d40ee8a8ad912394d19b7ae55059d46e94a45a5d 100644 (file)
@@ -3,6 +3,7 @@
 //
 // Author:
 //   Miguel de Icaza (miguel@ximian.com)
+//   Marek Safar (marek.safar@seznam.cz)
 //
 // (C) 2001, 2002, 2003 Ximian, Inc.
 //
@@ -128,14 +129,20 @@ namespace Mono.CSharp {
                /// </summary>
                public void Error (int error, string s)
                {
-                       if (loc.IsNull)
-                               Report.Error (error, s);
-                       else
-                               Report.Error (error, loc, s);
+                       Report.Error (error, loc, s);
+               }
+
+               // Not nice but we have broken hierarchy.
+               public virtual void CheckMarshalByRefAccess ()
+               {
                }
 
-               // Not nice but we have broken hierarchy
-               public virtual void CheckMarshallByRefAccess (Type container) {}
+               public virtual bool GetAttributableValue (Type valueType, out object value)
+               {
+                       Attribute.Error_AttributeArgumentNotValid (loc);
+                       value = null;
+                       return false;
+               }
 
                public virtual string GetSignatureForError ()
                {
@@ -216,12 +223,7 @@ namespace Mono.CSharp {
                // This is used if the expression should be resolved as a type or namespace name.
                // the default implementation fails.   
                //
-               public FullNamedExpression ResolveAsTypeStep (EmitContext ec)
-               {
-                       return ResolveAsTypeStep (ec, false);
-               }
-
-               public virtual FullNamedExpression ResolveAsTypeStep (EmitContext ec,  bool silent)
+               public virtual FullNamedExpression ResolveAsTypeStep (IResolveContext ec,  bool silent)
                {
                        return null;
                }
@@ -231,12 +233,32 @@ namespace Mono.CSharp {
                // value will be returned if the expression is not a type
                // reference
                //
-               public TypeExpr ResolveAsTypeTerminal (EmitContext ec)
+               public virtual TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
                {
-                       return ResolveAsTypeTerminal (ec, false);
+                       TypeExpr te = ResolveAsBaseTerminal (ec, silent);
+                       if (te == null)
+                               return null;
+
+                       ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (te.Type);
+                       if (obsolete_attr != null && !ec.IsInObsoleteScope) {
+                               AttributeTester.Report_ObsoleteMessage (obsolete_attr, te.GetSignatureForError (), Location);
+                       }
+
+                       // Constrains don't need to be checked for overrides
+                       GenericMethod gm = ec.GenericDeclContainer as GenericMethod;
+                       if (gm != null && (gm.ModFlags & Modifiers.OVERRIDE) != 0) {
+                               te.loc = loc;
+                               return te;
+                       }
+
+                       ConstructedType ct = te as ConstructedType;
+                       if ((ct != null) && !ct.CheckConstraints (ec))
+                               return null;
+
+                       return te;
                }
 
-               public virtual TypeExpr ResolveAsTypeTerminal (EmitContext ec, bool silent)
+               public TypeExpr ResolveAsBaseTerminal (IResolveContext ec, bool silent)
                {
                        int errors = Report.Errors;
 
@@ -249,23 +271,19 @@ namespace Mono.CSharp {
                        }
 
                        if (fne.eclass != ExprClass.Type) {
-                               if (!silent && (errors == Report.Errors))
+                               if (!silent && errors == Report.Errors)
                                        fne.Error_UnexpectedKind (null, "type", loc);
                                return null;
                        }
 
                        TypeExpr te = fne as TypeExpr;
 
-                       if (!te.CheckAccessLevel (ec.DeclSpace)) {
+                       if (!te.CheckAccessLevel (ec.DeclContainer)) {
+                               Report.SymbolRelatedToPreviousError (te.Type);
                                ErrorIsInaccesible (loc, TypeManager.CSharpName (te.Type));
                                return null;
                        }
 
-                       ConstructedType ct = te as ConstructedType;
-                       if ((ct != null) && !ec.ResolvingTypeTree && !ec.ResolvingGenericMethod &&
-                           !ct.CheckConstraints (ec))
-                               return null;
-
                        te.loc = loc;
                        return te;
                }
@@ -285,13 +303,24 @@ namespace Mono.CSharp {
 
                }
 
+               protected void Error_CannotAssign (string to, string roContext)
+               {
+                       Report.Error (1656, loc, "Cannot assign to `{0}' because it is a `{1}'",
+                               to, roContext);
+               }
+
+               public static void Error_VoidInvalidInTheContext (Location loc)
+               {
+                       Report.Error (1547, loc, "Keyword `void' cannot be used in this context");
+               }
+
                public virtual void Error_ValueCannotBeConverted (Location loc, Type target, bool expl)
                {
-                       if (Type.Name == target.Name){
+                       if (Type.FullName == target.FullName){
                                Report.ExtraInformation (loc,
                                        String.Format (
                                        "The type {0} has two conflicting definitions, one comes from {1} and the other from {2}",
-                                       Type.Name, Type.Assembly.FullName, target.Assembly.FullName));
+                                       Type.FullName, Type.Assembly.FullName, target.Assembly.FullName));
                                                         
                        }
 
@@ -328,28 +357,29 @@ namespace Mono.CSharp {
                                TypeManager.CSharpName (type), name);
                }
 
-               ResolveFlags ExprClassToResolveFlags ()
+               ResolveFlags ExprClassToResolveFlags
                {
-                       switch (eclass) {
-                       case ExprClass.Type:
-                       case ExprClass.Namespace:
-                               return ResolveFlags.Type;
+                       get {
+                               switch (eclass) {
+                                       case ExprClass.Type:
+                                       case ExprClass.Namespace:
+                                               return ResolveFlags.Type;
 
-                       case ExprClass.MethodGroup:
-                               return ResolveFlags.MethodGroup;
+                                       case ExprClass.MethodGroup:
+                                               return ResolveFlags.MethodGroup;
 
-                       case ExprClass.Value:
-                       case ExprClass.Variable:
-                       case ExprClass.PropertyAccess:
-                       case ExprClass.EventAccess:
-                       case ExprClass.IndexerAccess:
-                               return ResolveFlags.VariableOrValue;
+                                       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");
+                                       default:
+                                               throw new Exception ("Expression " + GetType () +
+                                                       " ExprClass is Invalid after resolve");
+                               }
                        }
-
                }
               
                /// <summary>
@@ -365,28 +395,27 @@ namespace Mono.CSharp {
                        if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type) 
                                return ResolveAsTypeStep (ec, false);
 
-                       bool old_do_flow_analysis = ec.DoFlowAnalysis;
-                       bool old_omit_struct_analysis = ec.OmitStructFlowAnalysis;
+                       bool do_flow_analysis = ec.DoFlowAnalysis;
+                       bool omit_struct_analysis = ec.OmitStructFlowAnalysis;
                        if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
-                               ec.DoFlowAnalysis = false;
+                               do_flow_analysis = false;
                        if ((flags & ResolveFlags.DisableStructFlowAnalysis) != 0)
-                               ec.OmitStructFlowAnalysis = true;
+                               omit_struct_analysis = true;
 
                        Expression e;
-                       bool intermediate = (flags & ResolveFlags.Intermediate) == ResolveFlags.Intermediate;
-                       if (this is SimpleName)
-                               e = ((SimpleName) this).DoResolve (ec, intermediate);
-
-                       else 
-                               e = DoResolve (ec);
-
-                       ec.DoFlowAnalysis = old_do_flow_analysis;
-                       ec.OmitStructFlowAnalysis = old_omit_struct_analysis;
+                       using (ec.WithFlowAnalysis (do_flow_analysis, omit_struct_analysis)) {
+                               if (this is SimpleName) {
+                                       bool intermediate = (flags & ResolveFlags.Intermediate) == ResolveFlags.Intermediate;
+                                       e = ((SimpleName) this).DoResolve (ec, intermediate);
+                               } else {
+                                       e = DoResolve (ec);
+                               }
+                       }
 
                        if (e == null)
                                return null;
 
-                       if ((flags & e.ExprClassToResolveFlags ()) == 0) {
+                       if ((flags & e.ExprClassToResolveFlags) == 0) {
                                e.Error_UnexpectedKind (flags, loc);
                                return null;
                        }
@@ -425,18 +454,12 @@ namespace Mono.CSharp {
                        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;
-                               }
+                       Type constant_type = null;
+                       if (mc is MemberBase) {
+                               constant_type = ((MemberBase)mc).MemberType;
                        }
-                       Const.Error_ExpressionMustBeConstant (loc, mc.GetSignatureForError ());
+
+                       Const.Error_ExpressionMustBeConstant (constant_type, loc, mc.GetSignatureForError ());
                        return null;
                }
 
@@ -451,32 +474,43 @@ namespace Mono.CSharp {
                public Expression ResolveLValue (EmitContext ec, Expression right_side, Location loc)
                {
                        int errors = Report.Errors;
+                       bool out_access = right_side == EmptyExpression.OutAccess;
+
                        Expression e = DoResolveLValue (ec, right_side);
 
+                       if (e != null && out_access && !(e is IMemoryLocation)) {
+                               // FIXME: There's no problem with correctness, the 'Expr = null' handles that.
+                               //        Enabling this 'throw' will "only" result in deleting useless code elsewhere,
+
+                               //throw new InternalErrorException ("ResolveLValue didn't return an IMemoryLocation: " +
+                               //                                e.GetType () + " " + e.GetSignatureForError ());
+                               e = null;
+                       }
+
                        if (e == null) {
-                               if (errors == Report.Errors)
-                                       Report.Error (131, loc, "The left-hand side of an assignment or mutating operation must be a variable, property or indexer");
+                               if (errors == Report.Errors) {
+                                       if (out_access)
+                                               Report.Error (1510, loc, "A ref or out argument must be an assignable variable");
+                                       else
+                                               Report.Error (131, loc, "The left-hand side of an assignment or mutating operation must be a variable, property or indexer");
+                               }
                                return null;
                        }
 
-                       if (e != null){
-                               if (e.eclass == ExprClass.Invalid)
-                                       throw new Exception ("Expression " + e +
-                                                            " ExprClass is Invalid after resolve");
+                       if (e.eclass == ExprClass.Invalid)
+                               throw new Exception ("Expression " + e + " ExprClass is Invalid after resolve");
 
-                               if (e.eclass == ExprClass.MethodGroup) {
-                                       ((MethodGroupExpr) e).ReportUsageError ();
-                                       return null;
-                               }
-
-                               if ((e.type == null) && !(e is ConstructedType))
-                                       throw new Exception ("Expression " + e +
-                                                            " did not set its type after Resolve");
+                       if (e.eclass == ExprClass.MethodGroup) {
+                               ((MethodGroupExpr) e).ReportUsageError ();
+                               return null;
                        }
 
+                       if ((e.type == null) && !(e is ConstructedType))
+                               throw new Exception ("Expression " + e + " did not set its type after Resolve");
+
                        return e;
                }
-               
+
                /// <summary>
                ///   Emits the code for the expression
                /// </summary>
@@ -566,14 +600,14 @@ namespace Mono.CSharp {
                ///   Returns a fully formed expression after a MemberLookup
                /// </summary>
                /// 
-               public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
+               public static Expression ExprClassFromMemberInfo (Type containerType, MemberInfo mi, Location loc)
                {
                        if (mi is EventInfo)
                                return new EventExpr ((EventInfo) mi, loc);
                        else if (mi is FieldInfo)
                                return new FieldExpr ((FieldInfo) mi, loc);
                        else if (mi is PropertyInfo)
-                               return new PropertyExpr (ec, (PropertyInfo) mi, loc);
+                               return new PropertyExpr (containerType, (PropertyInfo) mi, loc);
                        else if (mi is Type){
                                return new TypeExpression ((System.Type) mi, loc);
                        }
@@ -611,10 +645,10 @@ namespace Mono.CSharp {
                // FIXME: Potential optimization, have a static ArrayList
                //
 
-               public static Expression MemberLookup (EmitContext ec, Type queried_type, string name,
+               public static Expression MemberLookup (Type container_type, Type queried_type, string name,
                                                       MemberTypes mt, BindingFlags bf, Location loc)
                {
-                       return MemberLookup (ec, ec.ContainerType, null, queried_type, name, mt, bf, loc);
+                       return MemberLookup (container_type, null, queried_type, name, mt, bf, loc);
                }
 
                //
@@ -622,29 +656,63 @@ namespace Mono.CSharp {
                // `qualifier_type' or null to lookup members in the current class.
                //
 
-               public static Expression MemberLookup (EmitContext ec, Type container_type,
+               public static Expression MemberLookup (Type container_type,
                                                       Type qualifier_type, Type queried_type,
                                                       string name, MemberTypes mt,
                                                       BindingFlags bf, Location loc)
                {
                        almostMatchedMembers.Clear ();
 
-                       MemberInfo [] mi = TypeManager.MemberLookup (
-                               container_type, qualifier_type, queried_type, mt, bf, name,
-                               almostMatchedMembers);
+                       MemberInfo [] mi = TypeManager.MemberLookup (container_type, qualifier_type,
+                                                                    queried_type, mt, bf, name, almostMatchedMembers);
 
                        if (mi == null)
                                return null;
 
-                       int count = mi.Length;
+                       if (mi.Length > 1) {
+                               bool is_interface = qualifier_type != null && qualifier_type.IsInterface;
+                               MemberInfo non_method = null;
+                               ArrayList methods = new ArrayList (2);
+
+                               foreach (MemberInfo m in mi) {
+                                       if (m is MethodBase) {
+                                               methods.Add (m);
+                                               continue;
+                                       }
+
+                                       if (non_method == null) {
+                                               non_method = m;
+                                               continue;
+                                       }
+
+                                       if (!is_interface)
+                                               continue;
+
+                                       Report.SymbolRelatedToPreviousError (m);
+                                       Report.SymbolRelatedToPreviousError (non_method);
+                                       Report.Error (229, loc, "Ambiguity between `{0}' and `{1}'",
+                                               TypeManager.GetFullNameSignature (m), TypeManager.GetFullNameSignature (non_method));
+                                       return null;
+                               }
+
+                               if (non_method != null && is_interface) {
+                                       MethodBase method = (MethodBase)methods[0];
+                                       Report.SymbolRelatedToPreviousError (method);
+                                       Report.SymbolRelatedToPreviousError (non_method);
+                                       Report.Warning (467, 2, loc, "Ambiguity between method `{0}' and non-method `{1}'. Using method `{0}'",
+                                               TypeManager.CSharpSignature (method), TypeManager.GetFullNameSignature (non_method));
+                               }
+
+                               if (methods.Count == 0)
+                                       return new MethodGroupExpr (mi, loc);
+
+                               return new MethodGroupExpr (methods, loc);
+                       }
 
                        if (mi [0] is MethodBase)
                                return new MethodGroupExpr (mi, loc);
 
-                       if (count > 1)
-                               return null;
-
-                       return ExprClassFromMemberInfo (ec, mi [0], loc);
+                       return ExprClassFromMemberInfo (container_type, mi [0], loc);
                }
 
                public const MemberTypes AllMemberTypes =
@@ -660,30 +728,24 @@ namespace Mono.CSharp {
                        BindingFlags.Static |
                        BindingFlags.Instance;
 
-               public static Expression MemberLookup (EmitContext ec, Type queried_type,
+               public static Expression MemberLookup (Type container_type, Type queried_type,
                                                       string name, Location loc)
                {
-                       return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
+                       return MemberLookup (container_type, null, queried_type, name,
                                             AllMemberTypes, AllBindingFlags, loc);
                }
 
-               public static Expression MemberLookup (EmitContext ec, Type qualifier_type,
+               public static Expression MemberLookup (Type container_type, Type qualifier_type,
                                                       Type queried_type, string name, Location loc)
                {
-                       if (ec.ResolvingTypeTree)
-                               return MemberLookup (ec, ec.ContainerType, qualifier_type,
-                                                    queried_type, name, MemberTypes.NestedType,
-                                                    AllBindingFlags, loc);
-                       else
-                               return MemberLookup (ec, ec.ContainerType, qualifier_type,
-                                                    queried_type, name, AllMemberTypes,
-                                                    AllBindingFlags, loc);
+                       return MemberLookup (container_type, qualifier_type, queried_type,
+                                            name, AllMemberTypes, AllBindingFlags, loc);
                }
 
                public static Expression MethodLookup (EmitContext ec, Type queried_type,
                                                       string name, Location loc)
                {
-                       return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
+                       return MemberLookup (ec.ContainerType, null, queried_type, name,
                                             MemberTypes.Method, AllBindingFlags, loc);
                }
 
@@ -694,8 +756,7 @@ namespace Mono.CSharp {
                ///   find it.
                /// </summary>
                public static Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
-                                                           Type queried_type, string name,
-                                                           Location loc)
+                                                           Type queried_type, string name, Location loc)
                {
                        return MemberLookupFinal (ec, qualifier_type, queried_type, name,
                                                  AllMemberTypes, AllBindingFlags, loc);
@@ -710,17 +771,16 @@ namespace Mono.CSharp {
 
                        int errors = Report.Errors;
 
-                       e = MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
-                                         name, mt, bf, loc);
+                       e = MemberLookup (ec.ContainerType, qualifier_type, queried_type, name, mt, bf, loc);
 
                        if (e == null && errors == Report.Errors)
                                // No errors were reported by MemberLookup, but there was an error.
-                               MemberLookupFailed (ec, qualifier_type, queried_type, name, null, true, loc);
+                               MemberLookupFailed (ec.ContainerType, qualifier_type, queried_type, name, null, true, loc);
 
                        return e;
                }
 
-               public static void MemberLookupFailed (EmitContext ec, Type qualifier_type,
+               public static void MemberLookupFailed (Type container_type, Type qualifier_type,
                                                       Type queried_type, string name,
                                                       string class_name, bool complain_if_none_found, 
                                                       Location loc)
@@ -743,16 +803,18 @@ namespace Mono.CSharp {
                                        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)) {
+                                                             TypeManager.CSharpName (container_type));
+                                               
+                                       } else if (qualifier_type != container_type &&
+                                                  TypeManager.IsNestedFamilyAccessible (container_type, 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.
-                                               Error_CannotAccessProtected (loc, m, qualifier_type, ec.ContainerType);
+                                               Error_CannotAccessProtected (loc, m, qualifier_type, container_type);
                                        } else {
+                                               Report.SymbolRelatedToPreviousError (m);
                                                ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (m));
                                        }
                                }
@@ -760,9 +822,14 @@ namespace Mono.CSharp {
                                return;
                        }
 
-                       MemberInfo[] lookup = TypeManager.MemberLookup (queried_type, null, queried_type,
-                                                                       AllMemberTypes, AllBindingFlags |
-                                                                       BindingFlags.NonPublic, name, null);
+                       MemberInfo[] lookup = null;
+                       if (queried_type == null) {
+                               class_name = "global::";
+                       } else {
+                               lookup = TypeManager.MemberLookup (queried_type, null, queried_type,
+                                       AllMemberTypes, AllBindingFlags |
+                                       BindingFlags.NonPublic, name, null);
+                       }
 
                        if (lookup == null) {
                                if (!complain_if_none_found)
@@ -799,6 +866,7 @@ namespace Mono.CSharp {
                                return;
                        }
 
+                       Report.SymbolRelatedToPreviousError (lookup [0]);
                        ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (lookup [0]));
                }
 
@@ -904,16 +972,16 @@ namespace Mono.CSharp {
                /// <summary>
                ///   Reports that we were expecting `expr' to be of class `expected'
                /// </summary>
-               public void Error_UnexpectedKind (EmitContext ec, string expected, Location loc)
+               public void Error_UnexpectedKind (DeclSpace ds, string expected, Location loc)
                {
-                       Error_UnexpectedKind (ec, expected, ExprClassName, loc);
+                       Error_UnexpectedKind (ds, expected, ExprClassName, loc);
                }
 
-               public void Error_UnexpectedKind (EmitContext ec, string expected, string was, Location loc)
+               public void Error_UnexpectedKind (DeclSpace ds, string expected, string was, Location loc)
                {
                        string name = GetSignatureForError ();
-                       if (ec != null)
-                               name = ec.DeclSpace.GetSignatureForError () + '.' + name;
+                       if (ds != null)
+                               name = ds.GetSignatureForError () + '.' + name;
 
                        Report.Error (118, loc, "`{0}' is a `{1}' but a `{2}' was expected",
                              name, was, expected);
@@ -1075,22 +1143,20 @@ namespace Mono.CSharp {
                {
                        Expression target;
                        
-                       bool old_checked = ec.CheckState;
-                       ec.CheckState = true;
-                       
-                       target = Convert.ImplicitConversion (ec, source, TypeManager.int32_type, loc);
-                       if (target == null){
-                               target = Convert.ImplicitConversion (ec, source, TypeManager.uint32_type, loc);
-                               if (target == null){
+                       using (ec.With (EmitContext.Flags.CheckState, true)) {
+                               target = Convert.ImplicitConversion (ec, source, TypeManager.int32_type, loc);
+                               if (target == null)
+                                       target = Convert.ImplicitConversion (ec, source, TypeManager.uint32_type, loc);
+                               if (target == null)
                                        target = Convert.ImplicitConversion (ec, source, TypeManager.int64_type, loc);
-                                       if (target == null){
-                                               target = Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, loc);
-                                               if (target == null)
-                                                       source.Error_ValueCannotBeConverted (loc, TypeManager.int32_type, false);
-                                       }
+                               if (target == null)
+                                       target = Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, loc);
+
+                               if (target == null) {
+                                       source.Error_ValueCannotBeConverted (loc, TypeManager.int32_type, false);
+                                       return null;
                                }
-                       } 
-                       ec.CheckState = old_checked;
+                       }
 
                        //
                        // Only positive constants are allowed at compile time
@@ -1162,13 +1228,7 @@ namespace Mono.CSharp {
        ///
        /// </summary>
        public class EmptyCast : Expression {
-               protected Expression child;
-               
-               public Expression Child {
-                       get {
-                               return child;
-                       }
-               }               
+               protected readonly Expression child;
 
                public EmptyCast (Expression child, Type return_type)
                {
@@ -1190,6 +1250,12 @@ namespace Mono.CSharp {
                {
                        child.Emit (ec);
                }
+
+               public override bool GetAttributableValue (Type valueType, out object value)
+               {
+                       return child.GetAttributableValue (valueType, out value);
+               }
+
        }
        /// <summary>
        ///     This is a numeric cast to a Decimal
@@ -1338,10 +1404,10 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override Constant Reduce (EmitContext ec, Type target_type)
+               public override Constant Reduce (bool inCheckedContext, Type target_type)
                {
                        if (type == target_type)
-                               return child.Reduce (ec, target_type);
+                               return child.Reduce (inCheckedContext, target_type);
 
                        return null;
                }
@@ -1376,6 +1442,12 @@ namespace Mono.CSharp {
                        Child.Emit (ec);
                }
 
+               public override bool GetAttributableValue (Type valueType, out object value)
+               {
+                       value = GetTypedValue ();
+                       return true;
+               }
+
                public override string GetSignatureForError()
                {
                        return TypeManager.CSharpName (Type);
@@ -1452,12 +1524,12 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override Constant Reduce(EmitContext ec, Type target_type)
+               public override Constant Reduce(bool inCheckedContext, Type target_type)
                {
                        if (Child.Type == target_type)
                                return Child;
 
-                       return Child.Reduce (ec, target_type);
+                       return Child.Reduce (inCheckedContext, target_type);
                }
 
                public override Constant ToType (Type type, Location loc)
@@ -1472,7 +1544,7 @@ namespace Mono.CSharp {
                                return this;
                        }
 
-                       if (!Convert.ImplicitStandardConversionExists (Convert.ConstantEC, this, type)){
+                       if (!Convert.ImplicitStandardConversionExists (this, type)){
                                Error_ValueCannotBeConverted (loc, type, false);
                                return null;
                        }
@@ -1528,7 +1600,7 @@ namespace Mono.CSharp {
 
                public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
                {
-                       if (right_side == EmptyExpression.LValueMemberAccess)
+                       if (right_side == EmptyExpression.LValueMemberAccess || right_side == EmptyExpression.LValueMemberOutAccess)
                                Report.Error (445, loc, "Cannot modify the result of an unboxing conversion");
                        return base.DoResolveLValue (ec, right_side);
                }
@@ -1539,7 +1611,7 @@ namespace Mono.CSharp {
                        ILGenerator ig = ec.ig;
                        
                        base.Emit (ec);
-                       if (t.IsGenericParameter)
+                       if (t.IsGenericParameter || t.IsGenericType && t.IsValueType)
                                ig.Emit (OpCodes.Unbox_Any, t);
                        else {
                                ig.Emit (OpCodes.Unbox, t);
@@ -1902,21 +1974,17 @@ namespace Mono.CSharp {
                                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);
-
+                       else
                                Report.Error (
                                        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)
                {
                        return resolved_to != null && resolved_to.Type != null && 
                                resolved_to.Type.Name == Name &&
-                               (ec.DeclSpace.LookupType (Name, loc, /* ignore_cs0104 = */ true) != null);
+                               (ec.DeclContainer.LookupType (Name, loc, /* ignore_cs0104 = */ true) != null);
                }
 
                public override Expression DoResolve (EmitContext ec)
@@ -1951,12 +2019,12 @@ namespace Mono.CSharp {
                        return false;
                }
 
-               FullNamedExpression ResolveNested (EmitContext ec, Type t)
+               FullNamedExpression ResolveNested (IResolveContext ec, Type t)
                {
                        if (!t.IsGenericTypeDefinition)
                                return null;
 
-                       DeclSpace ds = ec.DeclSpace;
+                       DeclSpace ds = ec.DeclContainer;
                        while (ds != null) {
                                if (IsNestedChild (t, ds.TypeBuilder))
                                        break;
@@ -1987,14 +2055,14 @@ namespace Mono.CSharp {
                        return null;
                }
 
-               public override FullNamedExpression ResolveAsTypeStep (EmitContext ec, bool silent)
+               public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
                {
-                       FullNamedExpression fne = ec.DeclSpace.LookupGeneric (Name, loc);
+                       FullNamedExpression fne = ec.GenericDeclContainer.LookupGeneric (Name, loc);
                        if (fne != null)
                                return fne.ResolveAsTypeStep (ec, silent);
 
                        int errors = Report.Errors;
-                       fne = ec.DeclSpace.LookupType (Name, loc, /*ignore_cs0104=*/ false);
+                       fne = ec.DeclContainer.LookupType (Name, loc, /*ignore_cs0104=*/ false);
 
                        if (fne != null) {
                                if (fne.Type == null)
@@ -2002,11 +2070,11 @@ namespace Mono.CSharp {
 
                                FullNamedExpression nested = ResolveNested (ec, fne.Type);
                                if (nested != null)
-                                       return nested.ResolveAsTypeStep (ec);
+                                       return nested.ResolveAsTypeStep (ec, false);
 
                                if (Arguments != null) {
                                        ConstructedType ct = new ConstructedType (fne, Arguments, loc);
-                                       return ct.ResolveAsTypeStep (ec);
+                                       return ct.ResolveAsTypeStep (ec, false);
                                }
 
                                return fne;
@@ -2015,13 +2083,24 @@ namespace Mono.CSharp {
                        if (silent || errors != Report.Errors)
                                return null;
 
-                       MemberCore mc = ec.DeclSpace.GetDefinition (Name);
+                       MemberCore mc = ec.DeclContainer.GetDefinition (Name);
                        if (mc != null) {
-                               Error_UnexpectedKind (ec, "type", GetMemberType (mc), loc);
-                       } else {
-                               NamespaceEntry.Error_NamespaceNotFound (loc, Name);
+                               Error_UnexpectedKind (ec.DeclContainer, "type", GetMemberType (mc), loc);
+                               return null;
                        }
 
+                       string ns = ec.DeclContainer.NamespaceEntry.NS.Name;
+                       string fullname = (ns.Length > 0) ? ns + "." + Name : Name;
+                       foreach (Assembly a in RootNamespace.Global.Assemblies) {
+                               Type type = a.GetType (fullname);
+                               if (type != null) {
+                                       Report.SymbolRelatedToPreviousError (type);
+                                       Expression.ErrorIsInaccesible (loc, fullname);
+                                       return null;
+                               }
+                       }
+
+                       NamespaceEntry.Error_NamespaceNotFound (loc, Name);
                        return null;
                }
 
@@ -2087,6 +2166,13 @@ namespace Mono.CSharp {
                        if (current_block != null){
                                LocalInfo vi = current_block.GetLocalInfo (Name);
                                if (vi != null){
+                                       if (Arguments != null) {
+                                               Report.Error (307, loc,
+                                                             "The variable `{0}' cannot be used with type arguments",
+                                                             Name);
+                                               return null;
+                                       }
+
                                        LocalVariableReference var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
                                        if (right_side != null) {
                                                return var.ResolveLValue (ec, right_side, loc);
@@ -2100,6 +2186,13 @@ namespace Mono.CSharp {
 
                                ParameterReference pref = current_block.Toplevel.GetParameterReference (Name, loc);
                                if (pref != null) {
+                                       if (Arguments != null) {
+                                               Report.Error (307, loc,
+                                                             "The variable `{0}' cannot be used with type arguments",
+                                                             Name);
+                                               return null;
+                                       }
+
                                        if (right_side != null)
                                                return pref.ResolveLValue (ec, right_side, loc);
                                        else
@@ -2111,14 +2204,14 @@ namespace Mono.CSharp {
                        // Stage 2: Lookup members 
                        //
 
-                       DeclSpace lookup_ds = ec.DeclSpace;
+                       DeclSpace lookup_ds = ec.DeclContainer;
                        Type almost_matched_type = null;
                        ArrayList almost_matched = null;
                        do {
                                if (lookup_ds.TypeBuilder == null)
                                        break;
 
-                               e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
+                               e = MemberLookup (ec.ContainerType, lookup_ds.TypeBuilder, Name, loc);
                                if (e != null)
                                        break;
 
@@ -2131,7 +2224,7 @@ namespace Mono.CSharp {
                        } while (lookup_ds != null);
 
                        if (e == null && ec.ContainerType != null)
-                               e = MemberLookup (ec, ec.ContainerType, Name, loc);
+                               e = MemberLookup (ec.ContainerType, ec.ContainerType, Name, loc);
 
                        if (e == null) {
                                if (almost_matched == null && almostMatchedMembers.Count > 0) {
@@ -2146,12 +2239,18 @@ namespace Mono.CSharp {
                                        almostMatchedMembers = almost_matched;
                                if (almost_matched_type == null)
                                        almost_matched_type = ec.ContainerType;
-                               MemberLookupFailed (ec, null, almost_matched_type, ((SimpleName) this).Name, ec.DeclSpace.Name, true, loc);
+                               MemberLookupFailed (ec.ContainerType, null, almost_matched_type, ((SimpleName) this).Name, ec.DeclContainer.Name, true, loc);
                                return null;
                        }
 
-                       if (e is TypeExpr)
-                               return e;
+                       if (e is TypeExpr) {
+                               if (Arguments == null)
+                                       return e;
+
+                               ConstructedType ct = new ConstructedType (
+                                       (FullNamedExpression) e, Arguments, loc);
+                               return ct.ResolveAsTypeStep (ec, false);
+                       }
 
                        if (e is MemberExpr) {
                                MemberExpr me = (MemberExpr) e;
@@ -2226,7 +2325,7 @@ namespace Mono.CSharp {
 
                        Error (103, "The name `" + Name +
                               "' does not exist in the class `" +
-                              ec.DeclSpace.Name + "'");
+                              ec.DeclContainer.Name + "'");
                }
 
                public override string ToString ()
@@ -2245,7 +2344,7 @@ namespace Mono.CSharp {
        ///   section 10.8.1 (Fully Qualified Names).
        /// </summary>
        public abstract class FullNamedExpression : Expression {
-               public override FullNamedExpression ResolveAsTypeStep (EmitContext ec, bool silent)
+               public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
                {
                        return this;
                }
@@ -2259,7 +2358,7 @@ namespace Mono.CSharp {
        ///   Expression that evaluates to a type
        /// </summary>
        public abstract class TypeExpr : FullNamedExpression {
-               override public FullNamedExpression ResolveAsTypeStep (EmitContext ec, bool silent)
+               override public FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
                {
                        TypeExpr t = DoResolveAsTypeStep (ec);
                        if (t == null)
@@ -2271,7 +2370,7 @@ namespace Mono.CSharp {
 
                override public Expression DoResolve (EmitContext ec)
                {
-                       return ResolveAsTypeTerminal (ec);
+                       return ResolveAsTypeTerminal (ec, false);
                }
 
                override public void Emit (EmitContext ec)
@@ -2317,23 +2416,7 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               protected abstract TypeExpr DoResolveAsTypeStep (EmitContext ec);
-
-               public Type ResolveType (EmitContext ec)
-               {
-                       TypeExpr t = ResolveAsTypeTerminal (ec);
-                       if (t == null)
-                               return null;
-
-                       if (ec.TestObsoleteMethodUsage) {
-                               ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (t.Type);
-                               if (obsolete_attr != null) {
-                                       AttributeTester.Report_ObsoleteMessage (obsolete_attr, Name, Location);
-                               }
-                       }
-
-                       return t.Type;
-               }
+               protected abstract TypeExpr DoResolveAsTypeStep (IResolveContext ec);
 
                public abstract string Name {
                        get;
@@ -2370,7 +2453,12 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               protected override TypeExpr DoResolveAsTypeStep (EmitContext ec)
+               protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
+               {
+                       return this;
+               }
+
+               public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
                {
                        return this;
                }
@@ -2389,20 +2477,27 @@ namespace Mono.CSharp {
        ///   by the parser to setup the core types.  A TypeLookupExpression is always
        ///   classified as a type.
        /// </summary>
-       public class TypeLookupExpression : TypeExpr {
-               string name;
+       public sealed class TypeLookupExpression : TypeExpr {
+               readonly string name;
                
                public TypeLookupExpression (string name)
                {
                        this.name = name;
+                       eclass = ExprClass.Type;
                }
 
-               static readonly char [] dot_array = { '.' };
-               protected override TypeExpr DoResolveAsTypeStep (EmitContext ec)
+               public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
                {
-                       if (type != null)
-                               return this;
+                       // It's null for corlib compilation only
+                       if (type == null)
+                               return DoResolveAsTypeStep (ec);
 
+                       return this;
+               }
+
+               static readonly char [] dot_array = { '.' };
+               protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
+               {
                        // 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;
@@ -2412,7 +2507,7 @@ namespace Mono.CSharp {
                                lookup_name = name.Substring (0, pos);
                        }
 
-                       FullNamedExpression resolved = RootNamespace.Global.Lookup (ec.DeclSpace, lookup_name, Location.Null);
+                       FullNamedExpression resolved = RootNamespace.Global.Lookup (ec.DeclContainer, lookup_name, Location.Null);
 
                        if (resolved != null && rest != null) {
                                // Now handle the rest of the the name.
@@ -2424,13 +2519,13 @@ namespace Mono.CSharp {
                                        Namespace ns = resolved as Namespace;
                                        element = elements [i++];
                                        lookup_name += "." + element;
-                                       resolved = ns.Lookup (ec.DeclSpace, element, Location.Null);
+                                       resolved = ns.Lookup (ec.DeclContainer, element, Location.Null);
                                }
 
                                if (resolved != null && resolved is TypeExpr) {
                                        Type t = ((TypeExpr) resolved).Type;
                                        while (t != null) {
-                                               if (!ec.DeclSpace.CheckAccessLevel (t)) {
+                                               if (!ec.DeclContainer.CheckAccessLevel (t)) {
                                                        resolved = null;
                                                        lookup_name = t.FullName;
                                                        break;
@@ -2450,11 +2545,11 @@ namespace Mono.CSharp {
                        }
 
                        if (!(resolved is TypeExpr)) {
-                               resolved.Error_UnexpectedKind (ec, "type", loc);
+                               resolved.Error_UnexpectedKind (ec.DeclContainer, "type", loc);
                                return null;
                        }
 
-                       type = ((TypeExpr) resolved).ResolveType (ec);
+                       type = resolved.Type;
                        return this;
                }
 
@@ -2481,17 +2576,17 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               protected override TypeExpr DoResolveAsTypeStep (EmitContext ec)
+               protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
                {
                        Expression expr;
                        if (name.Left != null) {
                                Expression lexpr = name.Left.GetTypeExpression ();
-                               expr = new MemberAccess (lexpr, name.Basename, loc);
+                               expr = new MemberAccess (lexpr, name.Basename);
                        } else {
                                expr = new SimpleName (name.Basename, loc);
                        }
 
-                       FullNamedExpression fne = expr.ResolveAsTypeStep (ec);
+                       FullNamedExpression fne = expr.ResolveAsTypeStep (ec, false);
                        if (fne == null)
                                return null;
 
@@ -2535,13 +2630,13 @@ namespace Mono.CSharp {
                        get { return name; }
                }
 
-               protected override TypeExpr DoResolveAsTypeStep (EmitContext ec)
+               protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
                {
-                       texpr = alias.ResolveAsTypeTerminal (ec);
+                       texpr = alias.ResolveAsTypeTerminal (ec, false);
                        if (texpr == null)
                                return null;
 
-                       Type type = texpr.ResolveType (ec);
+                       Type type = texpr.Type;
                        int num_args = TypeManager.GetNumberOfTypeArguments (type);
 
                        if (args != null) {
@@ -2554,7 +2649,7 @@ namespace Mono.CSharp {
                                }
 
                                ConstructedType ctype = new ConstructedType (type, args, loc);
-                               return ctype.ResolveAsTypeTerminal (ec);
+                               return ctype.ResolveAsTypeTerminal (ec, false);
                        } else if (num_args > 0) {
                                Report.Error (305, loc,
                                              "Using the generic type `{0}' " +
@@ -2563,7 +2658,7 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       return new TypeExpression (type, loc);
+                       return texpr;
                }
 
                public override bool CheckAccessLevel (DeclSpace ds)
@@ -2651,7 +2746,7 @@ namespace Mono.CSharp {
 
                        if (left is TypeExpr) {
                                if (!IsStatic) {
-                                       SimpleName.Error_ObjectRefRequired (ec, loc, Name);
+                                       SimpleName.Error_ObjectRefRequired (ec, loc, GetSignatureForError ());
                                        return null;
                                }
 
@@ -2677,7 +2772,7 @@ namespace Mono.CSharp {
                                return;
 
                        if (InstanceExpression == EmptyExpression.Null) {
-                               SimpleName.Error_ObjectRefRequired (ec, loc, Name);
+                               SimpleName.Error_ObjectRefRequired (ec, loc, GetSignatureForError ());
                                return;
                        }
                                
@@ -2685,7 +2780,7 @@ namespace Mono.CSharp {
                                if (InstanceExpression is IMemoryLocation) {
                                        ((IMemoryLocation) InstanceExpression).AddressOf (ec, AddressOp.LoadStore);
                                } else {
-                                       LocalTemporary t = new LocalTemporary (ec, InstanceExpression.Type);
+                                       LocalTemporary t = new LocalTemporary (InstanceExpression.Type);
                                        InstanceExpression.Emit (ec);
                                        t.Store (ec);
                                        t.AddressOf (ec, AddressOp.Store);
@@ -2893,7 +2988,7 @@ namespace Mono.CSharp {
                        ArrayList list = new ArrayList ();
                        foreach (MethodBase mb in Methods) {
                                MethodInfo mi = mb as MethodInfo;
-                               if ((mi == null) || !mi.HasGenericParameters)
+                               if ((mi == null) || !mi.IsGenericMethod)
                                        continue;
 
                                Type[] gen_params = mi.GetGenericArguments ();
@@ -2913,6 +3008,7 @@ namespace Mono.CSharp {
                                MethodGroupExpr new_mg = new MethodGroupExpr (list, Location);
                                new_mg.InstanceExpression = InstanceExpression;
                                new_mg.HasTypeArguments = true;
+                               new_mg.IsBase = IsBase;
                                return new_mg;
                        }
 
@@ -2993,7 +3089,7 @@ namespace Mono.CSharp {
                public override Expression ResolveMemberAccess (EmitContext ec, Expression left, Location loc,
                                                                SimpleName original)
                {
-                       FieldInfo fi = FieldInfo.Mono_GetGenericFieldDefinition ();
+                       FieldInfo fi = TypeManager.GetGenericFieldDefinition (FieldInfo);
 
                        Type t = fi.FieldType;
 
@@ -3019,7 +3115,7 @@ namespace Mono.CSharp {
                                }
 
                                if (ic.ResolveValue ()) {
-                                       if (ec.TestObsoleteMethodUsage)
+                                       if (!ec.IsInObsoleteScope)
                                                ic.CheckObsoleteness (loc);
                                }
 
@@ -3036,37 +3132,18 @@ namespace Mono.CSharp {
 
                override public Expression DoResolve (EmitContext ec)
                {
-                       return DoResolve (ec, false);
+                       return DoResolve (ec, false, false);
                }
 
-               Expression DoResolve (EmitContext ec, bool lvalue_instance)
+               Expression DoResolve (EmitContext ec, bool lvalue_instance, bool out_access)
                {
-                       if (ec.InRefOutArgumentResolving && FieldInfo.IsInitOnly && !ec.IsConstructor && FieldInfo.FieldType.IsValueType) {
-                               if (FieldInfo.FieldType is TypeBuilder) {
-                                       if (FieldInfo.IsStatic)
-                                               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)",
-                                                       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)",
-                                                       Name);
-                                       else
-                                               Report.Error (192, loc, "A readonly field `{0}' cannot be passed ref or out (except in a constructor)",
-                                                       Name);
-                               }
-                               return null;
-                       }
-
                        if (!FieldInfo.IsStatic){
                                if (InstanceExpression == null){
                                        //
                                        // This can happen when referencing an instance field using
                                        // a fully qualified type expression: TypeName.InstanceField = xxx
                                        // 
-                                       SimpleName.Error_ObjectRefRequired (ec, loc, FieldInfo.Name);
+                                       SimpleName.Error_ObjectRefRequired (ec, loc, GetSignatureForError ());
                                        return null;
                                }
 
@@ -3075,10 +3152,11 @@ namespace Mono.CSharp {
                                // "a.b" is initialized, not whether the whole struct "a" is initialized.
 
                                if (lvalue_instance) {
-                                       bool old_do_flow_analysis = ec.DoFlowAnalysis;
-                                       ec.DoFlowAnalysis = false;
-                                       InstanceExpression = InstanceExpression.ResolveLValue (ec, EmptyExpression.LValueMemberAccess, loc);
-                                       ec.DoFlowAnalysis = old_do_flow_analysis;
+                                       using (ec.With (EmitContext.Flags.DoFlowAnalysis, false)) {
+                                               Expression right_side =
+                                                       out_access ? EmptyExpression.LValueMemberOutAccess : EmptyExpression.LValueMemberAccess;
+                                               InstanceExpression = InstanceExpression.ResolveLValue (ec, right_side, loc);
+                                       }
                                } else {
                                        ResolveFlags rf = ResolveFlags.VariableOrValue | ResolveFlags.DisableFlowAnalysis;
                                        InstanceExpression = InstanceExpression.Resolve (ec, rf);
@@ -3086,14 +3164,17 @@ namespace Mono.CSharp {
 
                                if (InstanceExpression == null)
                                        return null;
+
+                               InstanceExpression.CheckMarshalByRefAccess ();
                        }
 
                        if (!in_initializer && !ec.IsFieldInitializer) {
                                ObsoleteAttribute oa;
                                FieldBase f = TypeManager.GetField (FieldInfo);
                                if (f != null) {
-                                       if (ec.TestObsoleteMethodUsage)
+                                       if (!ec.IsInObsoleteScope)
                                                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);
@@ -3129,32 +3210,41 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               void Report_AssignToReadonly (Expression right_side)
-               {
-                       int code;
-                       string msg;
-                       bool need_error_sig = false;
-                       if (right_side == EmptyExpression.LValueMemberAccess) {
-                               if (IsStatic) {
-                                       code = 1650;
-                                       msg = "Fields of static readonly field `{0}' cannot be assigned to (except in a static constructor or a variable initializer)";
-                               } else {
-                                       code = 1648;
-                                       msg = "Members of readonly field `{0}' cannot be modified (except in a constructor or a variable initializer)";
-                               }
-                               need_error_sig = true;
-                       } else if (IsStatic) {
-                               code = 198;
-                               msg = "A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)";
-                       } else {
-                               code = 191;
-                               msg = "A readonly field cannot be assigned to (except in a constructor or a variable initializer)";
-                       }
+               static readonly int [] codes = {
+                       191,    // instance, write access
+                       192,    // instance, out access
+                       198,    // static, write access
+                       199,    // static, out access
+                       1648,   // member of value instance, write access
+                       1649,   // member of value instance, out access
+                       1650,   // member of value static, write access
+                       1651    // member of value static, out access
+               };
+
+               static readonly string [] msgs = {
+                       /*0191*/ "A readonly field `{0}' cannot be assigned to (except in a constructor or a variable initializer)",
+                       /*0192*/ "A readonly field `{0}' cannot be passed ref or out (except in a constructor)",
+                       /*0198*/ "A static readonly field `{0}' cannot be assigned to (except in a static constructor or a variable initializer)",
+                       /*0199*/ "A static readonly field `{0}' cannot be passed ref or out (except in a static constructor)",
+                       /*1648*/ "Members of readonly field `{0}' cannot be modified (except in a constructor or a variable initializer)",
+                       /*1649*/ "Members of readonly field `{0}' cannot be passed ref or out (except in a constructor)",
+                       /*1650*/ "Fields of static readonly field `{0}' cannot be assigned to (except in a static constructor or a variable initializer)",
+                       /*1651*/ "Fields of static readonly field `{0}' cannot be passed ref or out (except in a static constructor)"
+               };
+
+               // The return value is always null.  Returning a value simplifies calling code.
+               Expression Report_AssignToReadonly (Expression right_side)
+               {
+                       int i = 0;
+                       if (right_side == EmptyExpression.OutAccess || right_side == EmptyExpression.LValueMemberOutAccess)
+                               i += 1;
+                       if (IsStatic)
+                               i += 2;
+                       if (right_side == EmptyExpression.LValueMemberAccess || right_side == EmptyExpression.LValueMemberOutAccess)
+                               i += 4;
+                       Report.Error (codes [i], loc, msgs [i], GetSignatureForError ());
 
-                       if (need_error_sig)
-                               Report.Error (code, loc, msg, GetSignatureForError ());
-                       else
-                               Report.Error (code, loc, msg);
+                       return null;
                }
                
                override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
@@ -3164,8 +3254,9 @@ namespace Mono.CSharp {
                                var.VariableInfo.SetFieldAssigned (ec, FieldInfo.Name);
 
                        bool lvalue_instance = !FieldInfo.IsStatic && FieldInfo.DeclaringType.IsValueType;
+                       bool out_access = right_side == EmptyExpression.OutAccess || right_side == EmptyExpression.LValueMemberOutAccess;
 
-                       Expression e = DoResolve (ec, lvalue_instance);
+                       Expression e = DoResolve (ec, lvalue_instance, out_access);
 
                        if (e == null)
                                return null;
@@ -3174,38 +3265,45 @@ namespace Mono.CSharp {
                        if (fb != null)
                                fb.SetAssigned ();
 
-                       if (!FieldInfo.IsInitOnly)
-                               return this;
-
-                       //
-                       // InitOnly fields can only be assigned in constructors
-                       //
-
-                       if (ec.IsConstructor){
-                               if (IsStatic && !ec.IsStatic)
-                                       Report_AssignToReadonly (right_side);
-
-                               Type ctype;
-                               if (ec.TypeContainer.CurrentType != null)
-                                       ctype = ec.TypeContainer.CurrentType;
-                               else
-                                       ctype = ec.ContainerType;
+                       if (FieldInfo.IsInitOnly) {
+                               // InitOnly fields can only be assigned in constructors or initializers
+                               if (!ec.IsFieldInitializer && !ec.IsConstructor)
+                                       return Report_AssignToReadonly (right_side);
+
+                               if (ec.IsConstructor) {
+                                       Type ctype = ec.TypeContainer.CurrentType;
+                                       if (ctype == null)
+                                               ctype = ec.ContainerType;
+
+                                       // InitOnly fields cannot be assigned-to in a different constructor from their declaring type
+                                       if (!TypeManager.IsEqual (ctype, FieldInfo.DeclaringType))
+                                               return Report_AssignToReadonly (right_side);
+                                       // static InitOnly fields cannot be assigned-to in an instance constructor
+                                       if (IsStatic && !ec.IsStatic)
+                                               return Report_AssignToReadonly (right_side);
+                                       // instance constructors can't modify InitOnly fields of other instances of the same type
+                                       if (!IsStatic && !(InstanceExpression is This))
+                                               return Report_AssignToReadonly (right_side);
+                               }
+                       }
 
-                               if (TypeManager.IsEqual (ctype, FieldInfo.DeclaringType))
-                                       return this;
+                       if (right_side == EmptyExpression.OutAccess &&
+                           !IsStatic && !(InstanceExpression is This) && DeclaringType.IsSubclassOf (TypeManager.mbr_type)) {
+                               Report.SymbolRelatedToPreviousError (DeclaringType);
+                               Report.Warning (197, 1, loc,
+                                               "Passing `{0}' as ref or out or taking its address may cause a runtime exception because it is a field of a marshal-by-reference class",
+                                               GetSignatureForError ());
                        }
 
-                       Report_AssignToReadonly (right_side);
-                       
-                       return null;
+                       return this;
                }
 
-               public override void CheckMarshallByRefAccess (Type container)
+               public override void CheckMarshalByRefAccess ()
                {
-                       if (!IsStatic && Type.IsValueType && !container.IsSubclassOf (TypeManager.mbr_type) && DeclaringType.IsSubclassOf (TypeManager.mbr_type)) {
+                       if (!IsStatic && Type.IsValueType && !(InstanceExpression is This) && DeclaringType.IsSubclassOf (TypeManager.mbr_type)) {
                                Report.SymbolRelatedToPreviousError (DeclaringType);
-                               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 ());
+                               Report.Warning (1690, 1, loc, "Cannot call methods, properties, or indexers on `{0}' because it is a value type member of a marshal-by-reference class",
+                                               GetSignatureForError ());
                        }
                }
 
@@ -3218,7 +3316,7 @@ namespace Mono.CSharp {
                        return variable != null && InstanceExpression.Type.IsValueType && variable.VerifyFixed ();
                }
 
-               public override int GetHashCode()
+               public override int GetHashCode ()
                {
                        return FieldInfo.GetHashCode ();
                }
@@ -3243,16 +3341,13 @@ namespace Mono.CSharp {
                        ILGenerator ig = ec.ig;
                        bool is_volatile = false;
 
-                       FieldInfo the_fi = FieldInfo.Mono_GetGenericFieldDefinition ();
-                       if (the_fi is FieldBuilder){
-                               FieldBase f = TypeManager.GetField (the_fi);
-                               if (f != null){
-                                       if ((f.ModFlags & Modifiers.VOLATILE) != 0)
-                                               is_volatile = true;
-                                       
-                                       f.SetMemberIsUsed ();
-                               }
-                       } 
+                       FieldBase f = TypeManager.GetField (FieldInfo);
+                       if (f != null){
+                               if ((f.ModFlags & Modifiers.VOLATILE) != 0)
+                                       is_volatile = true;
+
+                               f.SetMemberIsUsed ();
+                       }
                        
                        if (FieldInfo.IsStatic){
                                if (is_volatile)
@@ -3280,7 +3375,7 @@ namespace Mono.CSharp {
                        if (leave_copy) {
                                ec.ig.Emit (OpCodes.Dup);
                                if (!FieldInfo.IsStatic) {
-                                       temp = new LocalTemporary (ec, this.Type);
+                                       temp = new LocalTemporary (this.Type);
                                        temp.Store (ec);
                                }
                        }
@@ -3305,28 +3400,28 @@ namespace Mono.CSharp {
                        if (leave_copy) {
                                ec.ig.Emit (OpCodes.Dup);
                                if (!FieldInfo.IsStatic) {
-                                       temp = new LocalTemporary (ec, this.Type);
+                                       temp = new LocalTemporary (this.Type);
                                        temp.Store (ec);
                                }
                        }
 
-                       if (FieldInfo is FieldBuilder){
-                               FieldBase f = TypeManager.GetField (FieldInfo);
-                               if (f != null){
-                                       if ((f.ModFlags & Modifiers.VOLATILE) != 0)
-                                               ig.Emit (OpCodes.Volatile);
+                       FieldBase f = TypeManager.GetField (FieldInfo);
+                       if (f != null){
+                               if ((f.ModFlags & Modifiers.VOLATILE) != 0)
+                                       ig.Emit (OpCodes.Volatile);
                                        
-                                       f.SetAssigned ();
-                               }
-                       } 
+                               f.SetAssigned ();
+                       }
 
                        if (is_static)
                                ig.Emit (OpCodes.Stsfld, FieldInfo);
                        else 
                                ig.Emit (OpCodes.Stfld, FieldInfo);
                        
-                       if (temp != null)
+                       if (temp != null) {
                                temp.Emit (ec);
+                               temp.Release (ec);
+                       }
                }
 
                public override void Emit (EmitContext ec)
@@ -3337,22 +3432,20 @@ namespace Mono.CSharp {
                public void AddressOf (EmitContext ec, AddressOp mode)
                {
                        ILGenerator ig = ec.ig;
-                       
-                       if (FieldInfo is FieldBuilder){
-                               FieldBase f = TypeManager.GetField (FieldInfo);
-                               if (f != null){
-                                       if ((f.ModFlags & Modifiers.VOLATILE) != 0){
-                                               Report.Warning (420, 1, loc, "`{0}': A volatile fields cannot be passed using a ref or out parameter",
+
+                       FieldBase f = TypeManager.GetField (FieldInfo);
+                       if (f != null){
+                               if ((f.ModFlags & Modifiers.VOLATILE) != 0){
+                                       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.SetAssigned ();
-                                       if ((mode & AddressOp.Load) != 0)
-                                               f.SetMemberIsUsed ();
+                                       return;
                                }
-                       } 
+                                       
+                               if ((mode & AddressOp.Store) != 0)
+                                       f.SetAssigned ();
+                               if ((mode & AddressOp.Load) != 0)
+                                       f.SetMemberIsUsed ();
+                       }
 
                        //
                        // Handle initonly fields specially: make a copy and then
@@ -3384,7 +3477,8 @@ namespace Mono.CSharp {
                        if (FieldInfo.IsStatic){
                                ig.Emit (OpCodes.Ldsflda, FieldInfo);
                        } else {
-                               EmitInstance (ec, false);
+                               if (!prepared)
+                                       EmitInstance (ec, false);
                                ig.Emit (OpCodes.Ldflda, FieldInfo);
                        }
                }
@@ -3428,7 +3522,7 @@ namespace Mono.CSharp {
 
                internal static PtrHashtable AccessorTable = new PtrHashtable (); 
 
-               public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
+               public PropertyExpr (Type containerType, PropertyInfo pi, Location l)
                {
                        PropertyInfo = pi;
                        eclass = ExprClass.PropertyAccess;
@@ -3437,7 +3531,7 @@ namespace Mono.CSharp {
 
                        type = TypeManager.TypeToCoreType (pi.PropertyType);
 
-                       ResolveAccessors (ec);
+                       ResolveAccessors (containerType);
                }
 
                public override string Name {
@@ -3471,7 +3565,7 @@ namespace Mono.CSharp {
 
                void FindAccessors (Type invocation_type)
                {
-                       BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
+                       const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
                                BindingFlags.Static | BindingFlags.Instance |
                                BindingFlags.DeclaredOnly;
 
@@ -3507,14 +3601,13 @@ namespace Mono.CSharp {
                // We also perform the permission checking here, as the PropertyInfo does not
                // hold the information for the accessibility of its setter/getter
                //
-               void ResolveAccessors (EmitContext ec)
+               // TODO: can use TypeManager.GetProperty to boost performance
+               void ResolveAccessors (Type containerType)
                {
-                       FindAccessors (ec.ContainerType);
+                       FindAccessors (containerType);
 
                        if (getter != null) {
-                               MethodInfo the_getter = getter;
-                               if (the_getter.Mono_IsInflatedMethod)
-                                       the_getter = the_getter.GetGenericMethodDefinition ();
+                               MethodBase the_getter = TypeManager.DropGenericMethodArguments (getter);
                                IMethodData md = TypeManager.GetMethod (the_getter);
                                if (md != null)
                                        md.SetMemberIsUsed ();
@@ -3524,9 +3617,7 @@ namespace Mono.CSharp {
                        }
 
                        if (setter != null) {
-                               MethodInfo the_setter = setter;
-                               if (the_setter.Mono_IsInflatedMethod)
-                                       the_setter = the_setter.GetGenericMethodDefinition ();
+                               MethodBase the_setter = TypeManager.DropGenericMethodArguments (setter);
                                IMethodData md = TypeManager.GetMethod (the_setter);
                                if (md != null)
                                        md.SetMemberIsUsed ();
@@ -3544,7 +3635,7 @@ namespace Mono.CSharp {
                        }
 
                        if (InstanceExpression == null) {
-                               SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
+                               SimpleName.Error_ObjectRefRequired (ec, loc, GetSignatureForError ());
                                return false;
                        }
 
@@ -3554,15 +3645,15 @@ namespace Mono.CSharp {
                                InstanceExpression = InstanceExpression.DoResolve (ec);
                        if (InstanceExpression == null)
                                return false;
-                       
-                       InstanceExpression.CheckMarshallByRefAccess (ec.ContainerType);
 
-                       if (must_do_cs1540_check && InstanceExpression != EmptyExpression.Null &&
-                               InstanceExpression.Type != ec.ContainerType && 
-                               ec.ContainerType.IsSubclassOf (PropertyInfo.DeclaringType) &&
-                               InstanceExpression.Type.IsSubclassOf (PropertyInfo.DeclaringType)) {
-                                       Error_CannotAccessProtected (loc, PropertyInfo, InstanceExpression.Type, ec.ContainerType);
-                                       return false;
+                       InstanceExpression.CheckMarshalByRefAccess ();
+
+                       if (must_do_cs1540_check && (InstanceExpression != EmptyExpression.Null) &&
+                           !TypeManager.IsInstantiationOfSameGenericType (InstanceExpression.Type, ec.ContainerType) &&
+                           TypeManager.IsFamilyAccessible (ec.ContainerType, PropertyInfo.DeclaringType) &&
+                           !TypeManager.IsFamilyAccessible (InstanceExpression.Type, ec.ContainerType)) {
+                               Error_CannotAccessProtected (loc, PropertyInfo, InstanceExpression.Type, ec.ContainerType);
+                               return false;
                        }
 
                        return true;
@@ -3626,11 +3717,13 @@ namespace Mono.CSharp {
                                        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
+                               else {
+                                       Report.SymbolRelatedToPreviousError (getter);
                                        ErrorIsInaccesible (loc, TypeManager.CSharpSignature (getter));
+                               }
                                return null;
                        }
-
+                       
                        if (!InstanceResolve (ec, false, must_do_cs1540_check))
                                return null;
 
@@ -3654,6 +3747,18 @@ namespace Mono.CSharp {
 
                override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
                {
+                       if (right_side == EmptyExpression.OutAccess) {
+                               Report.Error (206, loc, "A property or indexer `{0}' may not be passed as an out or ref parameter",
+                                             GetSignatureForError ());
+                               return null;
+                       }
+
+                       if (right_side == EmptyExpression.LValueMemberAccess || right_side == EmptyExpression.LValueMemberOutAccess) {
+                               Report.Error (1612, loc, "Cannot modify the return value of `{0}' because it is not a variable",
+                                             GetSignatureForError ());
+                               return null;
+                       }
+
                        if (setter == null){
                                //
                                // The following condition happens if the PropertyExpr was
@@ -3663,13 +3768,8 @@ namespace Mono.CSharp {
                                //
                                if (getter == null)
                                        return null;
-
-                               if (right_side == EmptyExpression.LValueMemberAccess)
-                                       Report.Error (1612, loc, "Cannot modify the return value of `{0}' because it is not a variable",
-                                               GetSignatureForError ());
-                               else
-                                       Report.Error (200, loc, "Property or indexer `{0}' cannot be assigned to (it is read only)",
-                                               GetSignatureForError ());
+                               Report.Error (200, loc, "Property or indexer `{0}' cannot be assigned to (it is read only)",
+                                             GetSignatureForError ());
                                return null;
                        }
 
@@ -3686,11 +3786,13 @@ namespace Mono.CSharp {
                                        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
+                               else {
+                                       Report.SymbolRelatedToPreviousError (setter);
                                        ErrorIsInaccesible (loc, TypeManager.CSharpSignature (setter));
+                               }
                                return null;
                        }
-
+                       
                        if (!InstanceResolve (ec, PropertyInfo.DeclaringType.IsValueType, must_do_cs1540_check))
                                return null;
                        
@@ -3737,7 +3839,7 @@ namespace Mono.CSharp {
                        if (leave_copy) {
                                ec.ig.Emit (OpCodes.Dup);
                                if (!is_static) {
-                                       temp = new LocalTemporary (ec, this.Type);
+                                       temp = new LocalTemporary (this.Type);
                                        temp.Store (ec);
                                }
                        }
@@ -3757,14 +3859,14 @@ namespace Mono.CSharp {
                                if (leave_copy) {
                                        ec.ig.Emit (OpCodes.Dup);
                                        if (!is_static) {
-                                               temp = new LocalTemporary (ec, this.Type);
+                                               temp = new LocalTemporary (this.Type);
                                                temp.Store (ec);
                                        }
                                }
                        } else if (leave_copy) {
                                source.Emit (ec);
                                if (!is_static) {
-                                       temp = new LocalTemporary (ec, this.Type);
+                                       temp = new LocalTemporary (this.Type);
                                        temp.Store (ec);
                                }
                                my_source = temp;
@@ -3775,8 +3877,10 @@ namespace Mono.CSharp {
                        
                        Invocation.EmitCall (ec, IsBase, IsStatic, InstanceExpression, setter, args, loc, false, prepared);
                        
-                       if (temp != null)
+                       if (temp != null) {
                                temp.Emit (ec);
+                               temp.Release (ec);
+                       }
                }
        }
 
@@ -3788,6 +3892,8 @@ namespace Mono.CSharp {
 
                bool is_static;
                MethodInfo add_accessor, remove_accessor;
+
+               internal static PtrHashtable AccessorTable = new PtrHashtable (); 
                
                public EventExpr (EventInfo ei, Location loc)
                {
@@ -3797,6 +3903,10 @@ namespace Mono.CSharp {
 
                        add_accessor = TypeManager.GetAddMethod (ei);
                        remove_accessor = TypeManager.GetRemoveMethod (ei);
+                       if (add_accessor != null)
+                               AccessorTable [add_accessor] = ei;
+                       if (remove_accessor != null)
+                               AccessorTable [remove_accessor] = ei;
                        
                        if (add_accessor.IsStatic || remove_accessor.IsStatic)
                                is_static = true;
@@ -3845,7 +3955,7 @@ namespace Mono.CSharp {
                                MemberInfo mi = TypeManager.GetPrivateFieldOfEvent (EventInfo);
 
                                if (mi != null) {
-                                       MemberExpr ml = (MemberExpr) ExprClassFromMemberInfo (ec, mi, loc);
+                                       MemberExpr ml = (MemberExpr) ExprClassFromMemberInfo (ec.ContainerType, mi, loc);
 
                                        if (ml == null) {
                                                Report.Error (-200, loc, "Internal error!!");
@@ -3870,7 +3980,7 @@ namespace Mono.CSharp {
                        }
 
                        if (InstanceExpression == null) {
-                               SimpleName.Error_ObjectRefRequired (ec, loc, EventInfo.Name);
+                               SimpleName.Error_ObjectRefRequired (ec, loc, GetSignatureForError ());
                                return false;
                        }
 
@@ -3882,12 +3992,12 @@ namespace Mono.CSharp {
                        // 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 != EmptyExpression.Null) {
-                               if ((InstanceExpression.Type != ec.ContainerType) &&
-                                       ec.ContainerType.IsSubclassOf (InstanceExpression.Type)) {
-                                       ErrorIsInaccesible (loc, TypeManager.CSharpSignature (EventInfo));
-                                       return false;
-                               }
+                       if (must_do_cs1540_check && InstanceExpression != EmptyExpression.Null &&
+                           InstanceExpression.Type != ec.ContainerType &&
+                           ec.ContainerType.IsSubclassOf (InstanceExpression.Type)) {
+                               Report.SymbolRelatedToPreviousError (EventInfo);
+                               ErrorIsInaccesible (loc, TypeManager.CSharpSignature (EventInfo));
+                               return false;
                        }
 
                        return true;
@@ -3903,6 +4013,7 @@ namespace Mono.CSharp {
                        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.SymbolRelatedToPreviousError (EventInfo);
                                ErrorIsInaccesible (loc, TypeManager.CSharpSignature (EventInfo));
                                return null;
                        }