2007-01-10 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / mcs / ecore.cs
index 5c21bc60f2965f35c56659820edac9c58d8b28c3..69c937ce0d956b0a008aff432cc9d06c0c0095eb 100644 (file)
@@ -155,14 +155,18 @@ namespace Mono.CSharp {
 
                        must_do_cs1540_check = false; // by default we do not check for this
 
+                       if (ma == MethodAttributes.Public)
+                               return true;
+                       
                        //
                        // If only accessible to the current class or children
                        //
                        if (ma == MethodAttributes.Private)
-                               return invocation_type == mi.DeclaringType ||
+                               return TypeManager.IsPrivateAccessible (invocation_type, mi.DeclaringType) ||
                                        TypeManager.IsNestedChildOf (invocation_type, mi.DeclaringType);
 
-                       if (mi.DeclaringType.Assembly == invocation_type.Assembly) {
+                       if (mi.DeclaringType.Assembly == invocation_type.Assembly ||
+                                       TypeManager.IsFriendAssembly (mi.DeclaringType.Assembly)) {
                                if (ma == MethodAttributes.Assembly || ma == MethodAttributes.FamORAssem)
                                        return true;
                        } else {
@@ -172,17 +176,11 @@ namespace Mono.CSharp {
 
                        // Family and FamANDAssem require that we derive.
                        // FamORAssem requires that we derive if in different assemblies.
-                       if (ma == MethodAttributes.Family ||
-                           ma == MethodAttributes.FamANDAssem ||
-                           ma == MethodAttributes.FamORAssem) {
-                               if (!TypeManager.IsNestedFamilyAccessible (invocation_type, mi.DeclaringType))
-                                       return false;
-
-                               if (!TypeManager.IsNestedChildOf (invocation_type, mi.DeclaringType))
-                                       must_do_cs1540_check = true;
+                       if (!TypeManager.IsNestedFamilyAccessible (invocation_type, mi.DeclaringType))
+                               return false;
 
-                               return true;
-                       }
+                       if (!TypeManager.IsNestedChildOf (invocation_type, mi.DeclaringType))
+                               must_do_cs1540_check = true;
 
                        return true;
                }
@@ -241,10 +239,24 @@ namespace Mono.CSharp {
                        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);
+                       if (!silent) {
+                               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;
                }
 
@@ -254,11 +266,8 @@ namespace Mono.CSharp {
 
                        FullNamedExpression fne = ResolveAsTypeStep (ec, silent);
 
-                       if (fne == null){
-                               if (!silent && errors == Report.Errors)
-                                       Report.Error (118, loc, "Expecting a type.");
+                       if (fne == null)
                                return null;
-                       }
 
                        if (fne.eclass != ExprClass.Type) {
                                if (!silent && errors == Report.Errors)
@@ -285,21 +294,32 @@ namespace Mono.CSharp {
 
                protected static void Error_CannotAccessProtected (Location loc, MemberInfo m, Type qualifier, Type container)
                {
-                       Report.Error (1540, loc, "Cannot access protected member `{0}' via a qualifier of type `{1}';"
-                               + " the qualifier must be of type `{2}' (or derived from it)", 
+                       Report.Error (1540, loc, "Cannot access protected member `{0}' via a qualifier of type `{1}'."
+                               + " The qualifier must be of type `{2}' or derived from it", 
                                TypeManager.GetFullNameSignature (m),
                                TypeManager.CSharpName (qualifier),
                                TypeManager.CSharpName (container));
 
                }
 
-               public virtual void Error_ValueCannotBeConverted (Location loc, Type target, bool expl)
+               protected void Error_CannotAssign (string to, string roContext)
                {
-                       if (Type.Name == target.Name){
+                       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 (EmitContext ec, Location loc, Type target, bool expl)
+               {
+                       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));
                                                         
                        }
 
@@ -312,15 +332,20 @@ namespace Mono.CSharp {
                        Expression e = (this is EnumConstant) ? ((EnumConstant)this).Child : this;
                        bool b = Convert.ExplicitNumericConversion (e, target) != null;
 
-                       if (b || Convert.ExplicitReferenceConversionExists (Type, target) || Convert.ExplicitUnsafe (e, target) != null) {
-                               Report.Error (266, loc, "Cannot implicitly convert type `{0}' to `{1}'. An explicit conversion exists (are you missing a cast?)",
+                       if (b ||
+                           Convert.ExplicitReferenceConversionExists (Type, target) ||
+                           Convert.ExplicitUnsafe (e, target) != null ||
+                           (ec != null && Convert.UserDefinedConversion (ec, this, target, Location.Null, true) != null))
+                       {
+                               Report.Error (266, loc, "Cannot implicitly convert type `{0}' to `{1}'. " +
+                                             "An explicit conversion exists (are you missing a cast?)",
                                        TypeManager.CSharpName (Type), TypeManager.CSharpName (target));
                                return;
                        }
 
-                       if (Type != TypeManager.string_type && this is Constant && !(this is NullCast)) {
+                       if (Type != TypeManager.string_type && this is Constant && !(this is EmptyConstantCast)) {
                                Report.Error (31, loc, "Constant value `{0}' cannot be converted to a `{1}'",
-                                       GetSignatureForError (), TypeManager.CSharpName (target));
+                                       ((Constant)(this)).GetValue ().ToString (), TypeManager.CSharpName (target));
                                return;
                        }
 
@@ -336,6 +361,11 @@ namespace Mono.CSharp {
                                TypeManager.CSharpName (type), name);
                }
 
+               protected static void Error_ValueAssignment (Location loc)
+               {
+                       Report.Error (131, loc, "The left-hand side of an assignment must be a variable, a property or an indexer");
+               }
+
                ResolveFlags ExprClassToResolveFlags
                {
                        get {
@@ -433,12 +463,7 @@ namespace Mono.CSharp {
                        if (c != null)
                                return c;
 
-                       Type constant_type = null;
-                       if (mc is MemberBase) {
-                               constant_type = ((MemberBase)mc).MemberType;
-                       }
-
-                       Const.Error_ExpressionMustBeConstant (constant_type, loc, mc.GetSignatureForError ());
+                       Const.Error_ExpressionMustBeConstant (loc, mc.GetSignatureForError ());
                        return null;
                }
 
@@ -471,7 +496,7 @@ namespace Mono.CSharp {
                                        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");
+                                               Error_ValueAssignment (loc);
                                }
                                return null;
                        }
@@ -484,7 +509,7 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       if (e.type == null)
+                       if ((e.type == null) && !(e is ConstructedType))
                                throw new Exception ("Expression " + e + " did not set its type after Resolve");
 
                        return e;
@@ -517,64 +542,6 @@ namespace Mono.CSharp {
                        type = null;
                }
 
-               /// <summary>
-               ///   Returns a literalized version of a literal FieldInfo
-               /// </summary>
-               ///
-               /// <remarks>
-               ///   The possible return values are:
-               ///      IntConstant, UIntConstant
-               ///      LongLiteral, ULongConstant
-               ///      FloatConstant, DoubleConstant
-               ///      StringConstant
-               ///
-               ///   The value returned is already resolved.
-               /// </remarks>
-               public static Constant Constantify (object v, Type t)
-               {
-                       if (t == TypeManager.int32_type)
-                               return new IntConstant ((int) v, Location.Null);
-                       else if (t == TypeManager.uint32_type)
-                               return new UIntConstant ((uint) v, Location.Null);
-                       else if (t == TypeManager.int64_type)
-                               return new LongConstant ((long) v, Location.Null);
-                       else if (t == TypeManager.uint64_type)
-                               return new ULongConstant ((ulong) v, Location.Null);
-                       else if (t == TypeManager.float_type)
-                               return new FloatConstant ((float) v, Location.Null);
-                       else if (t == TypeManager.double_type)
-                               return new DoubleConstant ((double) v, Location.Null);
-                       else if (t == TypeManager.string_type)
-                               return new StringConstant ((string) v, Location.Null);
-                       else if (t == TypeManager.short_type)
-                               return new ShortConstant ((short)v, Location.Null);
-                       else if (t == TypeManager.ushort_type)
-                               return new UShortConstant ((ushort)v, Location.Null);
-                       else if (t == TypeManager.sbyte_type)
-                               return new SByteConstant ((sbyte)v, Location.Null);
-                       else if (t == TypeManager.byte_type)
-                               return new ByteConstant ((byte)v, Location.Null);
-                       else if (t == TypeManager.char_type)
-                               return new CharConstant ((char)v, Location.Null);
-                       else if (t == TypeManager.bool_type)
-                               return new BoolConstant ((bool) v, Location.Null);
-                       else if (t == TypeManager.decimal_type)
-                               return new DecimalConstant ((decimal) v, Location.Null);
-                       else if (TypeManager.IsEnumType (t)){
-                               Type real_type = TypeManager.TypeToCoreType (v.GetType ());
-                               if (real_type == t)
-                                       real_type = System.Enum.GetUnderlyingType (real_type);
-
-                               Constant e = Constantify (v, real_type);
-
-                               return new EnumConstant (e, t);
-                       } else if (v == null && !TypeManager.IsValueType (t))
-                               return new NullLiteral (Location.Null);
-                       else
-                               throw new Exception ("Unknown type for constant (" + t +
-                                                    "), details: " + v);
-               }
-
                /// <summary>
                ///   Returns a fully formed expression after a MemberLookup
                /// </summary>
@@ -648,14 +615,59 @@ namespace Mono.CSharp {
                        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;
+                                       }
+
+                                       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 (methods.Count == 0)
+                                       return null;
+
+                               if (non_method != null) {
+                                       MethodBase method = (MethodBase) methods [0];
+
+                                       if (method.DeclaringType == non_method.DeclaringType) {
+                                               // Cannot happen with C# code, but is valid in IL
+                                               Report.SymbolRelatedToPreviousError (method);
+                                               Report.SymbolRelatedToPreviousError (non_method);
+                                               Report.Error (229, loc, "Ambiguity between `{0}' and `{1}'",
+                                                             TypeManager.GetFullNameSignature (non_method),
+                                                             TypeManager.CSharpSignature (method));
+                                               return null;
+                                       }
+
+                                       if (is_interface) {
+                                               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));
+                                       }
+                               }
+
+                               return new MethodGroupExpr (methods, loc);
+                       }
 
                        if (mi [0] is MethodBase)
                                return new MethodGroupExpr (mi, loc);
 
-                       if (count > 1)
-                               return null;
-
                        return ExprClassFromMemberInfo (container_type, mi [0], loc);
                }
 
@@ -686,10 +698,10 @@ namespace Mono.CSharp {
                                             name, AllMemberTypes, AllBindingFlags, loc);
                }
 
-               public static Expression MethodLookup (EmitContext ec, Type queried_type,
+               public static Expression MethodLookup (Type container_type, Type queried_type,
                                                       string name, Location loc)
                {
-                       return MemberLookup (ec.ContainerType, null, queried_type, name,
+                       return MemberLookup (container_type, null, queried_type, name,
                                             MemberTypes.Method, AllBindingFlags, loc);
                }
 
@@ -758,7 +770,6 @@ namespace Mono.CSharp {
                                                // then we are in this situation.
                                                Error_CannotAccessProtected (loc, m, qualifier_type, container_type);
                                        } else {
-                                               Report.SymbolRelatedToPreviousError (m);
                                                ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (m));
                                        }
                                }
@@ -787,8 +798,23 @@ namespace Mono.CSharp {
                                return;
                        }
 
+                       if (TypeManager.MemberLookup (queried_type, null, queried_type,
+                                                     AllMemberTypes, AllBindingFlags |
+                                                     BindingFlags.NonPublic, name, null) == null) {
+                               if ((lookup.Length == 1) && (lookup [0] is Type)) {
+                                       Type t = (Type) lookup [0];
+
+                                       Report.Error (305, loc,
+                                                     "Using the generic type `{0}' " +
+                                                     "requires {1} type arguments",
+                                                     TypeManager.CSharpName (t),
+                                                     TypeManager.GetNumberOfTypeArguments (t).ToString ());
+                                       return;
+                               }
+                       }
+
                        MemberList ml = TypeManager.FindMembers (queried_type, MemberTypes.Constructor,
-                               BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, null, null);
+                                                                BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, null, null);
                        if (name == ".ctor" && ml.Count == 0)
                        {
                                Report.Error (143, loc, "The type `{0}' has no constructors defined", TypeManager.CSharpName (queried_type));
@@ -803,7 +829,7 @@ namespace Mono.CSharp {
                ///   Returns an expression that can be used to invoke operator true
                ///   on the expression if it exists.
                /// </summary>
-               static public StaticCallExpr GetOperatorTrue (EmitContext ec, Expression e, Location loc)
+               static public Expression GetOperatorTrue (EmitContext ec, Expression e, Location loc)
                {
                        return GetOperatorTrueOrFalse (ec, e, true, loc);
                }
@@ -812,17 +838,22 @@ namespace Mono.CSharp {
                ///   Returns an expression that can be used to invoke operator false
                ///   on the expression if it exists.
                /// </summary>
-               static public StaticCallExpr GetOperatorFalse (EmitContext ec, Expression e, Location loc)
+               static public Expression GetOperatorFalse (EmitContext ec, Expression e, Location loc)
                {
                        return GetOperatorTrueOrFalse (ec, e, false, loc);
                }
 
-               static StaticCallExpr GetOperatorTrueOrFalse (EmitContext ec, Expression e, bool is_true, Location loc)
+               static Expression GetOperatorTrueOrFalse (EmitContext ec, Expression e, bool is_true, Location loc)
                {
                        MethodBase method;
                        Expression operator_group;
 
-                       operator_group = MethodLookup (ec, e.Type, is_true ? "op_True" : "op_False", loc);
+#if GMCS_SOURCE
+                       if (TypeManager.IsNullableType (e.Type))
+                               return new Nullable.OperatorTrueOrFalse (e, is_true, loc).Resolve (ec);
+#endif
+
+                       operator_group = MethodLookup (ec.ContainerType, e.Type, is_true ? "op_True" : "op_False", loc);
                        if (operator_group == null)
                                return null;
 
@@ -860,7 +891,7 @@ namespace Mono.CSharp {
                        //
                        converted = Expression.GetOperatorTrue (ec, e, loc);
                        if (converted == null){
-                               e.Error_ValueCannotBeConverted (loc, TypeManager.bool_type, false);
+                               e.Error_ValueCannotBeConverted (ec, loc, TypeManager.bool_type, false);
                                return null;
                        }
                        return converted;
@@ -987,11 +1018,11 @@ namespace Mono.CSharp {
                                        ig.Emit (OpCodes.Ldind_Ref);
                                else
                                        LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
-                       } else if (t.IsValueType)
+                       } else if (t.IsValueType || TypeManager.IsGenericParameter (t))
                                ig.Emit (OpCodes.Ldobj, t);
                        else if (t.IsPointer)
                                ig.Emit (OpCodes.Ldind_I);
-                       else 
+                       else
                                ig.Emit (OpCodes.Ldind_Ref);
                }
 
@@ -1018,7 +1049,7 @@ namespace Mono.CSharp {
                                ig.Emit (OpCodes.Stind_I1);
                        else if (type == TypeManager.intptr_type)
                                ig.Emit (OpCodes.Stind_I);
-                       else if (type.IsValueType)
+                       else if (type.IsValueType || TypeManager.IsGenericParameter (type))
                                ig.Emit (OpCodes.Stobj, type);
                        else
                                ig.Emit (OpCodes.Stind_Ref);
@@ -1079,7 +1110,7 @@ namespace Mono.CSharp {
                                        target = Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, loc);
 
                                if (target == null) {
-                                       source.Error_ValueCannotBeConverted (loc, TypeManager.int32_type, false);
+                                       source.Error_ValueCannotBeConverted (ec, loc, TypeManager.int32_type, false);
                                        return null;
                                }
                        }
@@ -1183,11 +1214,68 @@ namespace Mono.CSharp {
                }
 
        }
+
+       /// <summary>
+       ///    Performs a cast using an operator (op_Explicit or op_Implicit)
+       /// </summary>
+       public class OperatorCast : EmptyCast {
+               MethodInfo conversion_operator;
+               bool find_explicit;
+                       
+               public OperatorCast (Expression child, Type target_type) : this (child, target_type, false) {}
+
+               public OperatorCast (Expression child, Type target_type, bool find_explicit)
+                       : base (child, target_type)
+               {
+                       this.find_explicit = find_explicit;
+               }
+
+               // Returns the implicit operator that converts from
+               // 'child.Type' to our target type (type)
+               MethodInfo GetConversionOperator (bool find_explicit)
+               {
+                       string operator_name = find_explicit ? "op_Explicit" : "op_Implicit";
+
+                       MemberInfo [] mi;
+
+                       mi = TypeManager.MemberLookup (child.Type, child.Type, child.Type, MemberTypes.Method,
+                               BindingFlags.Static | BindingFlags.Public, operator_name, null);
+
+                       if (mi == null){
+                               mi = TypeManager.MemberLookup (type, type, type, MemberTypes.Method,
+                                                              BindingFlags.Static | BindingFlags.Public, operator_name, null);
+                       }
+                       
+                       foreach (MethodInfo oper in mi) {
+                               ParameterData pd = TypeManager.GetParameterData (oper);
+
+                               if (pd.ParameterType (0) == child.Type && oper.ReturnType == type)
+                                       return oper;
+                       }
+
+                       return null;
+
+
+               }
+               public override void Emit (EmitContext ec)
+               {
+                       ILGenerator ig = ec.ig;
+
+                       child.Emit (ec);
+                       conversion_operator = GetConversionOperator (find_explicit);
+
+                       if (conversion_operator == null)
+                               throw new InternalErrorException ("Outer conversion routine is out of sync");
+
+                       ig.Emit (OpCodes.Call, conversion_operator);
+               }
+               
+       }
+       
        /// <summary>
        ///     This is a numeric cast to a Decimal
        /// </summary>
        public class CastToDecimal : EmptyCast {
-
                MethodInfo conversion_operator;
 
                public CastToDecimal (Expression child)
@@ -1275,69 +1363,63 @@ namespace Mono.CSharp {
                }
        }
 
-        //
+       
+       //
+       // Constant specialization of EmptyCast.
        // We need to special case this since an empty cast of
-       // a NullLiteral is still a Constant
+       // a constant is still a constant. 
        //
-       public class NullCast : Constant {
-               public Constant child;
-                               
-               public NullCast (Constant child, Type return_type):
-                       base (Location.Null)
+       public class EmptyConstantCast : Constant
+       {
+               public readonly Constant child;
+
+               public EmptyConstantCast(Constant child, Type type)
+                       : base (child.Location)
                {
                        eclass = child.eclass;
-                       type = return_type;
                        this.child = child;
+                       this.type = type;
                }
 
-               override public string AsString ()
+               public override string AsString ()
                {
-                       return "null";
+                       return child.AsString ();
                }
 
                public override object GetValue ()
                {
-                       return null;
+                       return child.GetValue ();
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               public override Constant ConvertExplicitly (bool inCheckedContext, Type target_type)
                {
-                       // This should never be invoked, we are born in fully
-                       // initialized state.
-
-                       return this;
+                       return child.ConvertExplicitly (inCheckedContext, target_type);
                }
 
-               public override void Emit (EmitContext ec)
+               public override Constant Increment ()
                {
-                       child.Emit (ec);
+                       return child.Increment ();
                }
 
-               public override Constant Increment ()
+               public override bool IsDefaultValue
                {
-                       throw new NotSupportedException ();
+                       get { return child.IsDefaultValue; }
                }
 
-               public override bool IsDefaultValue {
-                       get {
-                               return true;
-                       }
+               public override bool IsNegative
+               {
+                       get { return child.IsNegative; }
                }
 
-               public override bool IsNegative {
-                       get {
-                               return false;
-                       }
+               public override void Emit (EmitContext ec)
+               {
+                       child.Emit (ec);
                }
 
-               public override Constant Reduce (bool inCheckedContext, Type target_type)
+               public override Constant ConvertImplicitly (Type type)
                {
-                       if (type == target_type)
-                               return child.Reduce (inCheckedContext, target_type);
-
-                       return null;
+                       return child.ConvertImplicitly (type);
                }
-
        }
 
 
@@ -1396,37 +1478,8 @@ namespace Mono.CSharp {
                
                public override string AsString ()
                {
-                       return Child.AsString ();
-               }
-
-               public override DoubleConstant ConvertToDouble ()
-               {
-                       return Child.ConvertToDouble ();
-               }
-
-               public override FloatConstant ConvertToFloat ()
-               {
-                       return Child.ConvertToFloat ();
-               }
-
-               public override ULongConstant ConvertToULong ()
-               {
-                       return Child.ConvertToULong ();
-               }
-
-               public override LongConstant ConvertToLong ()
-               {
-                       return Child.ConvertToLong ();
-               }
-
-               public override UIntConstant ConvertToUInt ()
-               {
-                       return Child.ConvertToUInt ();
-               }
-
-               public override IntConstant ConvertToInt ()
-               {
-                       return Child.ConvertToInt ();
+                       string value = System.Enum.GetName (type, Child.GetValue ());
+                       return value == null ? "0" : value;
                }
 
                public override Constant Increment()
@@ -1450,15 +1503,15 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override Constant Reduce(bool inCheckedContext, Type target_type)
+               public override Constant ConvertExplicitly(bool inCheckedContext, Type target_type)
                {
                        if (Child.Type == target_type)
                                return Child;
 
-                       return Child.Reduce (inCheckedContext, target_type);
+                       return Child.ConvertExplicitly (inCheckedContext, target_type);
                }
 
-               public override Constant ToType (Type type, Location loc)
+               public override Constant ConvertImplicitly (Type type)
                {
                        if (Type == type) {
                                // This is workaround of mono bug. It can be removed when the latest corlib spreads enough
@@ -1466,16 +1519,15 @@ namespace Mono.CSharp {
                                        return this;
 
                                if (type.UnderlyingSystemType != Child.Type)
-                                       Child = Child.ToType (type.UnderlyingSystemType, loc);
+                                       Child = Child.ConvertImplicitly (type.UnderlyingSystemType);
                                return this;
                        }
 
                        if (!Convert.ImplicitStandardConversionExists (this, type)){
-                               Error_ValueCannotBeConverted (loc, type, false);
                                return null;
                        }
 
-                       return Child.ToType (type, loc);
+                       return Child.ConvertImplicitly(type);
                }
 
        }
@@ -1537,9 +1589,16 @@ namespace Mono.CSharp {
                        ILGenerator ig = ec.ig;
                        
                        base.Emit (ec);
-                       ig.Emit (OpCodes.Unbox, t);
+#if GMCS_SOURCE
+                       if (t.IsGenericParameter || t.IsGenericType && t.IsValueType)
+                               ig.Emit (OpCodes.Unbox_Any, t);
+                       else
+#endif
+                       {
+                               ig.Emit (OpCodes.Unbox, t);
 
-                       LoadFromPtr (ig, t);
+                               LoadFromPtr (ig, t);
+                       }
                }
        }
        
@@ -1820,9 +1879,16 @@ namespace Mono.CSharp {
                {
                        base.Emit (ec);
 
-                       ec.ig.Emit (OpCodes.Castclass, type);
-               }                       
-               
+                       if (TypeManager.IsGenericParameter (child.Type))
+                               ec.ig.Emit (OpCodes.Box, child.Type);
+
+#if GMCS_SOURCE
+                       if (type.IsGenericParameter)
+                               ec.ig.Emit (OpCodes.Unbox_Any, type);
+                       else
+#endif
+                               ec.ig.Emit (OpCodes.Castclass, type);
+               }
        }
        
        /// <summary>
@@ -1831,6 +1897,7 @@ namespace Mono.CSharp {
        /// </summary>
        public class SimpleName : Expression {
                public string Name;
+               public readonly TypeArguments Arguments;
                bool in_transit;
 
                public SimpleName (string name, Location l)
@@ -1839,6 +1906,56 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
+               public SimpleName (string name, TypeArguments args, Location l)
+               {
+                       Name = name;
+                       Arguments = args;
+                       loc = l;
+               }
+
+               public SimpleName (string name, TypeParameter[] type_params, Location l)
+               {
+                       Name = name;
+                       loc = l;
+
+                       Arguments = new TypeArguments (l);
+                       foreach (TypeParameter type_param in type_params)
+                               Arguments.Add (new TypeParameterExpr (type_param, l));
+               }
+
+               public static string RemoveGenericArity (string name)
+               {
+                       int start = 0;
+                       StringBuilder sb = null;
+                       do {
+                               int pos = name.IndexOf ('`', start);
+                               if (pos < 0) {
+                                       if (start == 0)
+                                               return name;
+
+                                       sb.Append (name.Substring (start));
+                                       break;
+                               }
+
+                               if (sb == null)
+                                       sb = new StringBuilder ();
+                               sb.Append (name.Substring (start, pos-start));
+
+                               pos++;
+                               while ((pos < name.Length) && Char.IsNumber (name [pos]))
+                                       pos++;
+
+                               start = pos;
+                       } while (start < name.Length);
+
+                       return sb.ToString ();
+               }
+
+               public SimpleName GetMethodGroup ()
+               {
+                       return new SimpleName (RemoveGenericArity (Name), Arguments, loc);
+               }
+
                public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
                {
                        if (ec.IsFieldInitializer)
@@ -1874,12 +1991,82 @@ namespace Mono.CSharp {
                        return SimpleNameResolve (ec, null, intermediate);
                }
 
+               private bool IsNestedChild (Type t, Type parent)
+               {
+                       if (parent == null)
+                               return false;
+
+                       while (parent != null) {
+                               parent = TypeManager.DropGenericTypeArguments (parent);
+                               if (TypeManager.IsNestedChildOf (t, parent))
+                                       return true;
+
+                               parent = parent.BaseType;
+                       }
+
+                       return false;
+               }
+
+               FullNamedExpression ResolveNested (IResolveContext ec, Type t)
+               {
+                       if (!TypeManager.IsGenericTypeDefinition (t))
+                               return null;
+
+                       DeclSpace ds = ec.DeclContainer;
+                       while (ds != null) {
+                               if (IsNestedChild (t, ds.TypeBuilder))
+                                       break;
+
+                               ds = ds.Parent;
+                       }
+
+                       if (ds == null)
+                               return null;
+
+                       Type[] gen_params = TypeManager.GetTypeArguments (t);
+
+                       int arg_count = Arguments != null ? Arguments.Count : 0;
+
+                       for (; (ds != null) && ds.IsGeneric; ds = ds.Parent) {
+                               if (arg_count + ds.CountTypeParameters == gen_params.Length) {
+                                       TypeArguments new_args = new TypeArguments (loc);
+                                       foreach (TypeParameter param in ds.TypeParameters)
+                                               new_args.Add (new TypeParameterExpr (param, loc));
+
+                                       if (Arguments != null)
+                                               new_args.Add (Arguments);
+
+                                       return new ConstructedType (t, new_args, loc);
+                               }
+                       }
+
+                       return null;
+               }
+
                public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
                {
-                       int errors = Report.Errors;
-                       FullNamedExpression fne = ec.DeclContainer.LookupType (Name, loc, /*ignore_cs0104=*/ false);
+                       FullNamedExpression fne = ec.GenericDeclContainer.LookupGeneric (Name, loc);
                        if (fne != null)
+                               return fne.ResolveAsTypeStep (ec, silent);
+
+                       int errors = Report.Errors;
+                       fne = ec.DeclContainer.LookupType (Name, loc, /*ignore_cs0104=*/ false);
+
+                       if (fne != null) {
+                               if (fne.Type == null)
+                                       return fne;
+
+                               FullNamedExpression nested = ResolveNested (ec, fne.Type);
+                               if (nested != null)
+                                       return nested.ResolveAsTypeStep (ec, false);
+
+                               if (Arguments != null) {
+                                       ConstructedType ct = new ConstructedType (fne, Arguments, loc);
+                                       return ct.ResolveAsTypeStep (ec, false);
+                               }
+
                                return fne;
+                       }
 
                        if (silent || errors != Report.Errors)
                                return null;
@@ -1901,6 +2088,12 @@ namespace Mono.CSharp {
                                }
                        }
 
+                       Type t = ec.DeclContainer.NamespaceEntry.NS.LookForAnyGenericType (Name);
+                       if (t != null) {
+                               Namespace.Error_InvalidNumberOfTypeArguments (t, loc);
+                               return null;
+                       }
+
                        NamespaceEntry.Error_NamespaceNotFound (loc, Name);
                        return null;
                }
@@ -1967,6 +2160,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);
@@ -1980,6 +2180,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
@@ -2009,7 +2216,7 @@ namespace Mono.CSharp {
 
                                lookup_ds =lookup_ds.Parent;
                        } while (lookup_ds != null);
-                               
+
                        if (e == null && ec.ContainerType != null)
                                e = MemberLookup (ec.ContainerType, ec.ContainerType, Name, loc);
 
@@ -2030,8 +2237,14 @@ namespace Mono.CSharp {
                                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;
@@ -2071,10 +2284,18 @@ namespace Mono.CSharp {
                                if (me == null)
                                        return e;
 
-                               if (!me.IsStatic &&
+                               if (Arguments != null) {
+                                       MethodGroupExpr mg = me as MethodGroupExpr;
+                                       if (mg == null)
+                                               return null;
+
+                                       return mg.ResolveGeneric (ec, Arguments);
+                               }
+
+                               if (!me.IsStatic && (me.InstanceExpression != null) &&
                                    TypeManager.IsNestedFamilyAccessible (me.InstanceExpression.Type, me.DeclaringType) &&
                                    me.InstanceExpression.Type != me.DeclaringType &&
-                                   !me.InstanceExpression.Type.IsSubclassOf (me.DeclaringType) &&
+                                   !TypeManager.IsFamilyAccessible (me.InstanceExpression.Type, me.DeclaringType) &&
                                    (!intermediate || !IdenticalNameAndTypeName (ec, e, loc))) {
                                        Report.Error (38, loc, "Cannot access a nonstatic member of outer type `{0}' via nested type `{1}'",
                                                TypeManager.CSharpName (me.DeclaringType), TypeManager.CSharpName (me.InstanceExpression.Type));
@@ -2335,27 +2556,102 @@ namespace Mono.CSharp {
                }
        }
 
+       /// <summary>
+       ///   Represents an "unbound generic type", ie. typeof (Foo<>).
+       ///   See 14.5.11.
+       /// </summary>
+       public class UnboundTypeExpression : TypeExpr
+       {
+               MemberName name;
+
+               public UnboundTypeExpression (MemberName name, Location l)
+               {
+                       this.name = name;
+                       loc = l;
+               }
+
+               protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
+               {
+                       Expression expr;
+                       if (name.Left != null) {
+                               Expression lexpr = name.Left.GetTypeExpression ();
+                               expr = new MemberAccess (lexpr, name.Basename);
+                       } else {
+                               expr = new SimpleName (name.Basename, loc);
+                       }
+
+                       FullNamedExpression fne = expr.ResolveAsTypeStep (ec, false);
+                       if (fne == null)
+                               return null;
+
+                       type = fne.Type;
+                       return new TypeExpression (type, loc);
+               }
+
+               public override string Name {
+                       get { return name.FullName; }
+               }
+
+               public override string FullName {
+                       get { return name.FullName; }
+               }
+       }
+
        public class TypeAliasExpression : TypeExpr {
+               FullNamedExpression alias;
                TypeExpr texpr;
+               TypeArguments args;
+               string name;
 
-               public TypeAliasExpression (TypeExpr texpr, Location l)
+               public TypeAliasExpression (FullNamedExpression alias, TypeArguments args, Location l)
                {
-                       this.texpr = texpr;
-                       loc = texpr.Location;
+                       this.alias = alias;
+                       this.args = args;
+                       loc = l;
 
                        eclass = ExprClass.Type;
+                       if (args != null)
+                               name = alias.FullName + "<" + args.ToString () + ">";
+                       else
+                               name = alias.FullName;
                }
 
                public override string Name {
-                       get { return texpr.Name; }
+                       get { return alias.FullName; }
                }
 
                public override string FullName {
-                       get { return texpr.FullName; }
+                       get { return name; }
                }
 
                protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
                {
+                       texpr = alias.ResolveAsTypeTerminal (ec, false);
+                       if (texpr == null)
+                               return null;
+
+                       Type type = texpr.Type;
+                       int num_args = TypeManager.GetNumberOfTypeArguments (type);
+
+                       if (args != null) {
+                               if (num_args == 0) {
+                                       Report.Error (308, loc,
+                                                     "The non-generic type `{0}' cannot " +
+                                                     "be used with type arguments.",
+                                                     TypeManager.CSharpName (type));
+                                       return null;
+                               }
+
+                               ConstructedType ctype = new ConstructedType (type, args, loc);
+                               return ctype.ResolveAsTypeTerminal (ec, false);
+                       } else if (num_args > 0) {
+                               Report.Error (305, loc,
+                                             "Using the generic type `{0}' " +
+                                             "requires {1} type arguments",
+                                             TypeManager.CSharpName (type), num_args.ToString ());
+                               return null;
+                       }
+
                        return texpr;
                }
 
@@ -2498,7 +2794,8 @@ namespace Mono.CSharp {
        /// </summary>
        public class MethodGroupExpr : MemberExpr {
                public MethodBase [] Methods;
-               bool identical_type_name = false;
+               bool has_type_arguments = false;
+               bool identical_type_name = false;
                bool is_base;
                
                public MethodGroupExpr (MemberInfo [] mi, Location l)
@@ -2506,7 +2803,10 @@ namespace Mono.CSharp {
                        Methods = new MethodBase [mi.Length];
                        mi.CopyTo (Methods, 0);
                        eclass = ExprClass.MethodGroup;
-                       type = TypeManager.object_type;
+
+                       // Set the type to something that will never be useful, which will
+                       // trigger the proper conversions.
+                       type = typeof (MethodGroupExpr);
                        loc = l;
                }
 
@@ -2534,10 +2834,20 @@ namespace Mono.CSharp {
                public override Type DeclaringType {
                        get {
                                 //
-                                // The methods are arranged in this order:
-                                // derived type -> base type
+                                // We assume that the top-level type is in the end
                                 //
-                               return Methods [0].DeclaringType;
+                               return Methods [Methods.Length - 1].DeclaringType;
+                                //return Methods [0].DeclaringType;
+                       }
+               }
+
+               public bool HasTypeArguments {
+                       get {
+                               return has_type_arguments;
+                       }
+
+                       set {
+                               has_type_arguments = value;
                        }
                }
 
@@ -2550,7 +2860,7 @@ namespace Mono.CSharp {
                                identical_type_name = value;
                        }
                }
-               
+
                public bool IsBase {
                        get {
                                return is_base;
@@ -2661,6 +2971,60 @@ namespace Mono.CSharp {
                {
                        return RemoveMethods (false);
                }
+
+               public Expression ResolveGeneric (EmitContext ec, TypeArguments args)
+               {
+#if GMCS_SOURCE
+                       if (args.Resolve (ec) == false)
+                               return null;
+
+                       Type[] atypes = args.Arguments;
+
+                       int first_count = 0;
+                       MethodInfo first = null;
+
+                       ArrayList list = new ArrayList ();
+                       foreach (MethodBase mb in Methods) {
+                               MethodInfo mi = mb as MethodInfo;
+                               if ((mi == null) || !mi.IsGenericMethod)
+                                       continue;
+
+                               Type[] gen_params = mi.GetGenericArguments ();
+
+                               if (first == null) {
+                                       first = mi;
+                                       first_count = gen_params.Length;
+                               }
+
+                               if (gen_params.Length != atypes.Length)
+                                       continue;
+
+                               list.Add (mi.MakeGenericMethod (atypes));
+                       }
+
+                       if (list.Count > 0) {
+                               MethodGroupExpr new_mg = new MethodGroupExpr (list, Location);
+                               new_mg.InstanceExpression = InstanceExpression;
+                               new_mg.HasTypeArguments = true;
+                               new_mg.IsBase = IsBase;
+                               return new_mg;
+                       }
+
+                       if (first != null)
+                               Report.Error (
+                                       305, loc, "Using the generic method `{0}' " +
+                                       "requires {1} type arguments", Name,
+                                       first_count.ToString ());
+                       else
+                               Report.Error (
+                                       308, loc, "The non-generic method `{0}' " +
+                                       "cannot be used with type arguments", Name);
+
+                       return null;
+#else
+                       throw new NotImplementedException ();
+#endif
+               }
        }
 
        /// <summary>
@@ -2669,7 +3033,7 @@ namespace Mono.CSharp {
        public class FieldExpr : MemberExpr, IAssignMethod, IMemoryLocation, IVariable {
                public readonly FieldInfo FieldInfo;
                VariableInfo variable_info;
-
+               
                LocalTemporary temp;
                bool prepared;
                bool in_initializer;
@@ -2684,7 +3048,7 @@ namespace Mono.CSharp {
                {
                        FieldInfo = fi;
                        eclass = ExprClass.Variable;
-                       type = fi.FieldType;
+                       type = TypeManager.TypeToCoreType (fi.FieldType);
                        loc = l;
                }
 
@@ -2726,20 +3090,22 @@ namespace Mono.CSharp {
                public override Expression ResolveMemberAccess (EmitContext ec, Expression left, Location loc,
                                                                SimpleName original)
                {
-                       Type t = FieldInfo.FieldType;
+                       FieldInfo fi = TypeManager.GetGenericFieldDefinition (FieldInfo);
+
+                       Type t = fi.FieldType;
 
-                       if (FieldInfo.IsLiteral || (FieldInfo.IsInitOnly && t == TypeManager.decimal_type)) {
-                               IConstant ic = TypeManager.GetConstant (FieldInfo);
+                       if (fi.IsLiteral || (fi.IsInitOnly && t == TypeManager.decimal_type)) {
+                               IConstant ic = TypeManager.GetConstant (fi);
                                if (ic == null) {
-                                       if (FieldInfo.IsLiteral) {
-                                               ic = new ExternalConstant (FieldInfo);
+                                       if (fi.IsLiteral) {
+                                               ic = new ExternalConstant (fi);
                                        } else {
-                                               ic = ExternalConstant.CreateDecimal (FieldInfo);
+                                               ic = ExternalConstant.CreateDecimal (fi);
                                                if (ic == null) {
                                                        return base.ResolveMemberAccess (ec, left, loc, original);
                                                }
                                        }
-                                       TypeManager.RegisterConstant (FieldInfo, ic);
+                                       TypeManager.RegisterConstant (fi, ic);
                                }
 
                                bool left_is_type = left is TypeExpr;
@@ -2754,7 +3120,7 @@ namespace Mono.CSharp {
                                                ic.CheckObsoleteness (loc);
                                }
 
-                               return ic.Value;
+                               return ic.CreateConstantReference (loc);
                        }
                        
                        if (t.IsPointer && !ec.InUnsafe) {
@@ -2827,8 +3193,6 @@ namespace Mono.CSharp {
                                                        "this");
                                                return null;
                                        }
-                                       if ((am.ContainerAnonymousMethod == null) && (InstanceExpression is This))
-                                               ec.CaptureField (this);
                                }
                        }
                        
@@ -2906,8 +3270,12 @@ namespace Mono.CSharp {
                                        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 (ec.ContainerType != FieldInfo.DeclaringType)
+                                       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)
@@ -2988,7 +3356,7 @@ namespace Mono.CSharp {
                        } else {
                                if (!prepared)
                                        EmitInstance (ec, false);
-                               
+
                                if (is_volatile)
                                        ig.Emit (OpCodes.Volatile);
 
@@ -3003,7 +3371,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       if (leave_copy) {       
+                       if (leave_copy) {
                                ec.ig.Emit (OpCodes.Dup);
                                if (!FieldInfo.IsStatic) {
                                        temp = new LocalTemporary (this.Type);
@@ -3011,7 +3379,7 @@ namespace Mono.CSharp {
                                }
                        }
                }
-               
+
                public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
                {
                        FieldAttributes fa = FieldInfo.Attributes;
@@ -3036,23 +3404,23 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       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)
@@ -3063,22 +3431,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
@@ -3110,7 +3476,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);
                        }
                }
@@ -3152,8 +3519,6 @@ namespace Mono.CSharp {
                LocalTemporary temp;
                bool prepared;
 
-               internal static PtrHashtable AccessorTable = new PtrHashtable (); 
-
                public PropertyExpr (Type containerType, PropertyInfo pi, Location l)
                {
                        PropertyInfo = pi;
@@ -3233,26 +3598,26 @@ 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
                //
-               // TODO: can use TypeManager.GetProperty to boost performance
+               // TODO: Refactor to use some kind of cache together with GetPropertyFromAccessor
                void ResolveAccessors (Type containerType)
                {
                        FindAccessors (containerType);
 
                        if (getter != null) {
-                               IMethodData md = TypeManager.GetMethod (getter);
+                               MethodBase the_getter = TypeManager.DropGenericMethodArguments (getter);
+                               IMethodData md = TypeManager.GetMethod (the_getter);
                                if (md != null)
                                        md.SetMemberIsUsed ();
 
-                               AccessorTable [getter] = PropertyInfo;
                                is_static = getter.IsStatic;
                        }
 
                        if (setter != null) {
-                               IMethodData md = TypeManager.GetMethod (setter);
+                               MethodBase the_setter = TypeManager.DropGenericMethodArguments (setter);
+                               IMethodData md = TypeManager.GetMethod (the_setter);
                                if (md != null)
                                        md.SetMemberIsUsed ();
 
-                               AccessorTable [setter] = PropertyInfo;
                                is_static = setter.IsStatic;
                        }
                }
@@ -3269,19 +3634,20 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       if (lvalue_instance)
+                       InstanceExpression = InstanceExpression.DoResolve (ec);
+                       if (lvalue_instance && InstanceExpression != null)
                                InstanceExpression = InstanceExpression.ResolveLValue (ec, EmptyExpression.LValueMemberAccess, loc);
-                       else
-                               InstanceExpression = InstanceExpression.DoResolve (ec);
+
                        if (InstanceExpression == null)
                                return false;
 
                        InstanceExpression.CheckMarshalByRefAccess ();
 
-                       if (must_do_cs1540_check && InstanceExpression != EmptyExpression.Null &&
-                           InstanceExpression.Type != ec.ContainerType &&
-                           ec.ContainerType.IsSubclassOf (PropertyInfo.DeclaringType) &&
-                           !InstanceExpression.Type.IsSubclassOf (ec.ContainerType)) {
+                       if (must_do_cs1540_check && (InstanceExpression != EmptyExpression.Null) &&
+                           !TypeManager.IsInstantiationOfSameGenericType (InstanceExpression.Type, ec.ContainerType) &&
+                           !TypeManager.IsNestedChildOf (ec.ContainerType, InstanceExpression.Type) &&
+                           !TypeManager.IsSubclassOf (InstanceExpression.Type, ec.ContainerType)) {
+                               Report.SymbolRelatedToPreviousError (PropertyInfo);
                                Error_CannotAccessProtected (loc, PropertyInfo, InstanceExpression.Type, ec.ContainerType);
                                return false;
                        }
@@ -3507,8 +3873,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);
+                       }
                }
        }
 
@@ -3521,8 +3889,6 @@ namespace Mono.CSharp {
                bool is_static;
                MethodInfo add_accessor, remove_accessor;
 
-               internal static PtrHashtable AccessorTable = new PtrHashtable (); 
-               
                public EventExpr (EventInfo ei, Location loc)
                {
                        EventInfo = ei;
@@ -3531,11 +3897,6 @@ 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;
 
@@ -3668,7 +4029,11 @@ namespace Mono.CSharp {
 
                public void EmitAddOrRemove (EmitContext ec, Expression source)
                {
-                       BinaryDelegate source_del = (BinaryDelegate) source;
+                       BinaryDelegate source_del = source as BinaryDelegate;
+                       if (source_del == null) {
+                               Emit (ec);
+                               return;
+                       }
                        Expression handler = source_del.Right;
                        
                        Argument arg = new Argument (handler, Argument.AType.Expression);
@@ -3685,10 +4050,10 @@ namespace Mono.CSharp {
                }
        }
 
-       
        public class TemporaryVariable : Expression, IMemoryLocation
        {
                LocalInfo li;
+               Variable var;
                
                public TemporaryVariable (Type type, Location loc)
                {
@@ -3706,64 +4071,47 @@ namespace Mono.CSharp {
                        li = ec.CurrentBlock.AddTemporaryVariable (te, loc);
                        if (!li.Resolve (ec))
                                return null;
-                       
-                       AnonymousContainer am = ec.CurrentAnonymousMethod;
-                       if ((am != null) && am.IsIterator)
-                               ec.CaptureVariable (li);
+
+                       if (ec.MustCaptureVariable (li)) {
+                               ScopeInfo scope = li.Block.CreateScopeInfo ();
+                               var = scope.AddLocal (li);
+                               type = var.Type;
+                       }
                        
                        return this;
                }
+
+               public Variable Variable {
+                       get { return var != null ? var : li.Variable; }
+               }
                
                public override void Emit (EmitContext ec)
                {
-                       ILGenerator ig = ec.ig;
-                       
-                       if (li.FieldBuilder != null) {
-                               ig.Emit (OpCodes.Ldarg_0);
-                               ig.Emit (OpCodes.Ldfld, li.FieldBuilder);
-                       } else {
-                               ig.Emit (OpCodes.Ldloc, li.LocalBuilder);
-                       }
+                       Variable.EmitInstance (ec);
+                       Variable.Emit (ec);
                }
                
                public void EmitLoadAddress (EmitContext ec)
                {
-                       ILGenerator ig = ec.ig;
-                       
-                       if (li.FieldBuilder != null) {
-                               ig.Emit (OpCodes.Ldarg_0);
-                               ig.Emit (OpCodes.Ldflda, li.FieldBuilder);
-                       } else {
-                               ig.Emit (OpCodes.Ldloca, li.LocalBuilder);
-                       }
+                       Variable.EmitInstance (ec);
+                       Variable.EmitAddressOf (ec);
                }
                
                public void Store (EmitContext ec, Expression right_side)
                {
-                       if (li.FieldBuilder != null)
-                               ec.ig.Emit (OpCodes.Ldarg_0);
-                       
+                       Variable.EmitInstance (ec);
                        right_side.Emit (ec);
-                       if (li.FieldBuilder != null) {
-                               ec.ig.Emit (OpCodes.Stfld, li.FieldBuilder);
-                       } else {
-                               ec.ig.Emit (OpCodes.Stloc, li.LocalBuilder);
-                       }
+                       Variable.EmitAssign (ec);
                }
                
                public void EmitThis (EmitContext ec)
                {
-                       if (li.FieldBuilder != null) {
-                               ec.ig.Emit (OpCodes.Ldarg_0);
-                       }
+                       Variable.EmitInstance (ec);
                }
                
-               public void EmitStore (ILGenerator ig)
+               public void EmitStore (EmitContext ec)
                {
-                       if (li.FieldBuilder != null)
-                               ig.Emit (OpCodes.Stfld, li.FieldBuilder);
-                       else
-                               ig.Emit (OpCodes.Stloc, li.LocalBuilder);
+                       Variable.EmitAssign (ec);
                }
                
                public void AddressOf (EmitContext ec, AddressOp mode)