2005-01-19 Sureshkumar T <tsureshkumar@novell.com>
[mono.git] / mcs / gmcs / expression.cs
old mode 100755 (executable)
new mode 100644 (file)
index 8f5cca4..a056929
@@ -217,6 +217,10 @@ namespace Mono.CSharp {
                                e = new IntConstant (-((ShortConstant) expr).Value);
                        else if (expr is UShortConstant)
                                e = new IntConstant (-((UShortConstant) expr).Value);
+                       else if (expr is SByteConstant)
+                               e = new IntConstant (-((SByteConstant) expr).Value);
+                       else if (expr is ByteConstant)
+                               e = new IntConstant (-((ByteConstant) expr).Value);
                        return e;
                }
 
@@ -237,7 +241,7 @@ namespace Mono.CSharp {
                                
                        case Operator.UnaryNegation:
                                result = TryReduceNegative (e);
-                               return true;
+                               return result != null;
                                
                        case Operator.LogicalNot:
                                if (expr_type != TypeManager.bool_type) {
@@ -323,11 +327,22 @@ namespace Mono.CSharp {
 
                Expression ResolveOperator (EmitContext ec)
                {
-                       Type expr_type = Expr.Type;
+                       //
+                       // Step 1: Default operations on CLI native types.
+                       //
+
+                       // Attempt to use a constant folding operation.
+                       if (Expr is Constant){
+                               Expression result;
+                               
+                               if (Reduce (ec, (Constant) Expr, out result))
+                                       return result;
+                       }
 
                        //
-                       // Step 1: Perform Operator Overload location
+                       // Step 2: Perform Operator Overload location
                        //
+                       Type expr_type = Expr.Type;
                        Expression mg;
                        string op_name;
                        
@@ -353,18 +368,6 @@ namespace Mono.CSharp {
                        if (expr_type == null)
                                return null;
                        
-                       //
-                       // Step 2: Default operations on CLI native types.
-                       //
-
-                       // Attempt to use a constant folding operation.
-                       if (Expr is Constant){
-                               Expression result;
-                               
-                               if (Reduce (ec, (Constant) Expr, out result))
-                                       return result;
-                       }
-
                        switch (Oper){
                        case Operator.LogicalNot:
                                if (expr_type != TypeManager.bool_type) {
@@ -439,6 +442,16 @@ namespace Mono.CSharp {
                                        return null;
                                }
 
+                               LocalVariableReference lr = Expr as LocalVariableReference;
+                               if (lr != null){
+                                       if (lr.local_info.IsCaptured){
+                                               AnonymousMethod.Error_AddressOfCapturedVar (lr.Name, loc);
+                                               return null;
+                                       }
+                                       lr.local_info.AddressTaken = true;
+                                       lr.local_info.Used = true;
+                               }
+
                                // According to the specs, a variable is considered definitely assigned if you take
                                // its address.
                                if ((variable != null) && (variable.VariableInfo != null))
@@ -1031,10 +1044,10 @@ namespace Mono.CSharp {
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       TypeExpr texpr = ProbeType.ResolveAsTypeTerminal (ec, false);
+                       TypeExpr texpr = ProbeType.ResolveAsTypeTerminal (ec);
                        if (texpr == null)
                                return null;
-                       probe_type = texpr.ResolveType (ec);
+                       probe_type = texpr.Type;
 
                        CheckObsoleteAttribute (probe_type);
 
@@ -1153,6 +1166,9 @@ namespace Mono.CSharp {
 
                                warning_always_matches = true;
                        } else if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
+                               if (etype.IsGenericParameter)
+                                       expr = new BoxedCast (expr, etype);
+
                                //
                                // Second case: explicit reference convresion
                                //
@@ -1165,15 +1181,15 @@ namespace Mono.CSharp {
                                warning_never_matches = true;
                        }
                        
-                               if (warning_always_matches)
+                       if (warning_always_matches)
                                Warning (183, "The given expression is always of the provided ('{0}') type", TypeManager.CSharpName (probe_type));
-                               else if (warning_never_matches){
-                                       if (!(probe_type.IsInterface || expr.Type.IsInterface))
+                       else if (warning_never_matches){
+                               if (!(probe_type.IsInterface || expr.Type.IsInterface))
                                        Warning (184, "The given expression is never of the provided ('{0}') type", TypeManager.CSharpName (probe_type));
                        }
 
                        return this;
-               }                               
+               }
        }
 
        /// <summary>
@@ -1231,6 +1247,9 @@ namespace Mono.CSharp {
                        }
 
                        if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
+                               if (etype.IsGenericParameter)
+                                       expr = new BoxedCast (expr, etype);
+
                                do_isinst = true;
                                return this;
                        }
@@ -1768,11 +1787,11 @@ namespace Mono.CSharp {
                        if (expr == null)
                                return null;
 
-                       TypeExpr target = target_type.ResolveAsTypeTerminal (ec, false);
+                       TypeExpr target = target_type.ResolveAsTypeTerminal (ec);
                        if (target == null)
                                return null;
                        
-                       type = target.ResolveType (ec);
+                       type = target.Type;
 
                        CheckObsoleteAttribute (type);
 
@@ -2236,8 +2255,8 @@ namespace Mono.CSharp {
                        // Special cases: string or type parameter comapred to null
                        //
                        if (oper == Operator.Equality || oper == Operator.Inequality){
-                               if ((!TypeManager.IsValueType (l) && (right is NullLiteral)) ||
-                                   (!TypeManager.IsValueType (r) && (left is NullLiteral))) {
+                               if ((!TypeManager.IsValueType (l) && r == TypeManager.null_type) ||
+                                   (!TypeManager.IsValueType (r) && l == TypeManager.null_type)) {
                                        Type = TypeManager.bool_type;
                                        
                                        return this;
@@ -2696,16 +2715,27 @@ namespace Mono.CSharp {
                                }
                        } else
                                left = left.Resolve (ec);
-                       right = right.Resolve (ec);
 
-                       if (left == null || right == null)
+                       if (left == null)
+                               return null;
+
+                       Constant lc = left as Constant;
+                       if (lc != null && lc.Type == TypeManager.bool_type && 
+                               ((oper == Operator.LogicalAnd && (bool)lc.GetValue () == false) ||
+                                (oper == Operator.LogicalOr && (bool)lc.GetValue () == true))) {
+
+                               // TODO: make a sence to resolve unreachable expression as we do for statement
+                               Report.Warning (429, 4, loc, "Unreachable expression code detected");
+                               return left;
+                       }
+
+                       right = right.Resolve (ec);
+                       if (right == null)
                                return null;
 
                        eclass = ExprClass.Value;
 
                        Constant rc = right as Constant;
-                       Constant lc = left as Constant;
-
                        if (rc != null & lc != null){
                                Expression e = ConstantFold.BinaryFold (
                                        ec, oper, lc, rc, loc);
@@ -3487,9 +3517,6 @@ namespace Mono.CSharp {
                        if (trueExpr == null || falseExpr == null)
                                return null;
 
-                       if ((trueExpr is NullLiteral) && (falseExpr is NullLiteral))
-                               return trueExpr;
-
                        eclass = ExprClass.Value;
                        if (trueExpr.Type == falseExpr.Type)
                                type = trueExpr.Type;
@@ -3604,6 +3631,11 @@ namespace Mono.CSharp {
                {
                        if (local_info == null) {
                                local_info = Block.GetLocalInfo (Name);
+
+                               // is out param
+                               if (lvalue_right_side == EmptyExpression.Null)
+                                       local_info.Used = true;
+
                                is_readonly = local_info.ReadOnly;
                        }
 
@@ -3639,8 +3671,11 @@ namespace Mono.CSharp {
                                // flag it for capturing
                                //
                                if (local_info.Block.Toplevel != ec.CurrentBlock.Toplevel){
+                                       if (local_info.AddressTaken){
+                                               AnonymousMethod.Error_AddressOfCapturedVar (local_info.Name, loc);
+                                               return null;
+                                       }
                                        ec.CaptureVariable (local_info);
-                                       //Console.WriteLine ("Capturing at " + loc);
                                }
                        }
                        
@@ -4158,8 +4193,9 @@ namespace Mono.CSharp {
 
                                        if (instance.GetType () != typeof (This)){
                                                if (fe.InstanceExpression.Type.IsSubclassOf (TypeManager.mbr_type)){
-                                                       Report.Error (197, loc,
-                                                                     "Can not pass a type that derives from MarshalByRefObject with out or ref");
+                                                       Report.SymbolRelatedToPreviousError (fe.InstanceExpression.Type);
+                                                       Report.Error (197, loc, "Cannot pass '{0}' as ref or out or take its address because it is a member of a marshal-by-reference class",
+                                                               fe.Name);
                                                        return false;
                                                }
                                        }
@@ -4316,9 +4352,9 @@ namespace Mono.CSharp {
                                //   * There is no implicit conversion from type 'object' to other reference types
                                //  => Conversion of 'null' to a reference type is better than conversion to 'object'
                                //
-                               //  FIXME: This probably isn't necessary, since the type of a NullLiteral is 'System.Null'.
-                               //         I think it used to be 'object' and thus needed a special case to avoid the
-                               //         immediately following two checks.
+                               //  FIXME: This probably isn't necessary, since the type of a NullLiteral is the 
+                               //         null type. I think it used to be 'object' and thus needed a special 
+                               //         case to avoid the immediately following two checks.
                                //
                                if (!p.IsValueType && q == TypeManager.object_type)
                                        return p;
@@ -4579,7 +4615,7 @@ namespace Mono.CSharp {
                                                      bool do_varargs, ref MethodBase candidate)
                {
                        if (!me.HasTypeArguments &&
-                           !InferParamsTypeArguments (ec, arguments, ref candidate))
+                           !TypeManager.InferParamsTypeArguments (ec, arguments, ref candidate))
                                return false;
 
                        return IsParamsMethodApplicable (
@@ -4679,7 +4715,7 @@ namespace Mono.CSharp {
                                          ref MethodBase candidate)
                {
                        if (!me.HasTypeArguments &&
-                           !InferTypeArguments (ec, arguments, ref candidate))
+                           !TypeManager.InferTypeArguments (ec, arguments, ref candidate))
                                return false;
 
                        return IsApplicable (ec, arguments, arg_count, candidate);
@@ -4847,7 +4883,7 @@ namespace Mono.CSharp {
                                        if (pd.Count != arg_count)
                                                continue;
 
-                                       if (!InferTypeArguments (ec, Arguments, ref c))
+                                       if (!TypeManager.InferTypeArguments (ec, Arguments, ref c))
                                                continue;
 
                                        VerifyArgumentsCompat (ec, Arguments, arg_count,
@@ -4867,7 +4903,7 @@ namespace Mono.CSharp {
                                                if (pd.Count != arg_count)
                                                        continue;
 
-                                               if (InferTypeArguments (ec, Arguments, ref c))
+                                               if (TypeManager.InferTypeArguments (ec, Arguments, ref c))
                                                        continue;
 
                                                Report.Error (
@@ -4875,7 +4911,7 @@ namespace Mono.CSharp {
                                                        "method `{0}' cannot be infered from " +
                                                        "the usage. Try specifying the type " +
                                                        "arguments explicitly.", report_name);
-                                               break;
+                                               return null;
                                        }
 
                                        Error_WrongNumArguments (
@@ -5119,241 +5155,6 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               static bool InferType (Type pt, Type at, ref Type[] infered)
-               {
-                       if (pt.IsGenericParameter && (pt.DeclaringMethod != null)) {
-                               int pos = pt.GenericParameterPosition;
-
-                               if (infered [pos] == null) {
-                                       Type check = at;
-                                       while (check.IsArray)
-                                               check = check.GetElementType ();
-
-                                       if (pt == check)
-                                               return false;
-
-                                       infered [pos] = at;
-                                       return true;
-                               }
-
-                               if (infered [pos] != at)
-                                       return false;
-
-                               return true;
-                       }
-
-                       if (!pt.ContainsGenericParameters)
-                               return true;
-
-                       if (at.IsArray) {
-                               if (!pt.IsArray ||
-                                   (at.GetArrayRank () != pt.GetArrayRank ()))
-                                       return false;
-
-                               return InferType (pt.GetElementType (), at.GetElementType (),
-                                                 ref infered);
-                       }
-
-                       if (pt.IsArray) {
-                               if (!at.IsArray ||
-                                   (pt.GetArrayRank () != at.GetArrayRank ()))
-                                       return false;
-
-                               return InferType (pt.GetElementType (), at.GetElementType (),
-                                                 ref infered);
-                       }
-
-                       if (!at.IsGenericInstance)
-                               return false;
-
-                       Type[] at_args = at.GetGenericArguments ();
-                       Type[] pt_args = pt.GetGenericArguments ();
-
-                       if (at_args.Length != pt_args.Length)
-                               return false;
-
-                       Type[] infered_types = new Type [at_args.Length];
-
-                       for (int i = 0; i < at_args.Length; i++)
-                               if (!InferType (pt_args [i], at_args [i], ref infered_types))
-                                       return false;
-
-                       for (int i = 0; i < infered_types.Length; i++)
-                               if (infered_types [i] == null)
-                                       return false;
-
-                       for (int i = 0; i < infered_types.Length; i++) {
-                               if (infered [i] == null) {
-                                       infered [i] = infered_types [i];
-                                       continue;
-                               }
-
-                               if (infered [i] != infered_types [i])
-                                       return false;
-                       }
-
-                       return true;
-               }
-
-               static bool InferParamsTypeArguments (EmitContext ec, ArrayList arguments,
-                                                     ref MethodBase method)
-               {
-                       if ((arguments == null) || !TypeManager.IsGenericMethod (method))
-                               return true;
-
-                       int arg_count;
-                       
-                       if (arguments == null)
-                               arg_count = 0;
-                       else
-                               arg_count = arguments.Count;
-                       
-                       ParameterData pd = GetParameterData (method);
-
-                       int pd_count = pd.Count;
-
-                       if (pd_count == 0)
-                               return false;
-                       
-                       if (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS)
-                               return false;
-                       
-                       if (pd_count - 1 > arg_count)
-                               return false;
-                       
-                       if (pd_count == 1 && arg_count == 0)
-                               return true;
-
-                       Type[] method_args = method.GetGenericArguments ();
-                       Type[] infered_types = new Type [method_args.Length];
-
-                       //
-                       // If we have come this far, the case which
-                       // remains is when the number of parameters is
-                       // less than or equal to the argument count.
-                       //
-                       for (int i = 0; i < pd_count - 1; ++i) {
-                               Argument a = (Argument) arguments [i];
-
-                               if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
-                                       continue;
-
-                               Type pt = pd.ParameterType (i);
-                               Type at = a.Type;
-
-                               if (!InferType (pt, at, ref infered_types))
-                                       return false;
-                       }
-
-                       Type element_type = TypeManager.GetElementType (pd.ParameterType (pd_count - 1));
-
-                       for (int i = pd_count - 1; i < arg_count; i++) {
-                               Argument a = (Argument) arguments [i];
-
-                               if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
-                                       continue;
-
-                               if (!InferType (element_type, a.Type, ref infered_types))
-                                       return false;
-                       }
-
-                       for (int i = 0; i < infered_types.Length; i++)
-                               if (infered_types [i] == null)
-                                       return false;
-
-                       method = method.BindGenericParameters (infered_types);
-                       return true;
-               }
-
-               public static bool InferTypeArguments (Type[] param_types, Type[] arg_types,
-                                                      ref Type[] infered_types)
-               {
-                       if (infered_types == null)
-                               return false;
-
-                       for (int i = 0; i < arg_types.Length; i++) {
-                               if (arg_types [i] == null)
-                                       continue;
-
-                               if (!InferType (param_types [i], arg_types [i],
-                                               ref infered_types))
-                                       return false;
-                       }
-
-                       for (int i = 0; i < infered_types.Length; i++)
-                               if (infered_types [i] == null)
-                                       return false;
-
-                       return true;
-               }
-
-               static bool InferTypeArguments (EmitContext ec, ArrayList arguments,
-                                               ref MethodBase method)
-               {
-                       if (!TypeManager.IsGenericMethod (method))
-                               return true;
-
-                       int arg_count;
-                       if (arguments != null)
-                               arg_count = arguments.Count;
-                       else
-                               arg_count = 0;
-
-                       ParameterData pd = GetParameterData (method);
-                       if (arg_count != pd.Count)
-                               return false;
-
-                       Type[] method_args = method.GetGenericArguments ();
-                       Type[] infered_types = new Type [method_args.Length];
-
-                       Type[] param_types = new Type [pd.Count];
-                       Type[] arg_types = new Type [pd.Count];
-
-                       for (int i = 0; i < arg_count; i++) {
-                               param_types [i] = pd.ParameterType (i);
-
-                               Argument a = (Argument) arguments [i];
-                               if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
-                                       continue;
-
-                               arg_types [i] = a.Type;
-                       }
-
-                       if (!InferTypeArguments (param_types, arg_types, ref infered_types))
-                               return false;
-
-                       method = method.BindGenericParameters (infered_types);
-                       return true;
-               }
-
-               public static bool InferTypeArguments (EmitContext ec, ParameterData apd,
-                                                      ref MethodBase method)
-               {
-                       if (!TypeManager.IsGenericMethod (method))
-                               return true;
-
-                       ParameterData pd = GetParameterData (method);
-                       if (apd.Count != pd.Count)
-                               return false;
-
-                       Type[] method_args = method.GetGenericArguments ();
-                       Type[] infered_types = new Type [method_args.Length];
-
-                       Type[] param_types = new Type [pd.Count];
-                       Type[] arg_types = new Type [pd.Count];
-
-                       for (int i = 0; i < apd.Count; i++) {
-                               param_types [i] = pd.ParameterType (i);
-                               arg_types [i] = apd.ParameterType (i);
-                       }
-
-                       if (!InferTypeArguments (param_types, arg_types, ref infered_types))
-                               return false;
-
-                       method = method.BindGenericParameters (infered_types);
-                       return true;
-               }
-
                public override Expression DoResolve (EmitContext ec)
                {
                        //
@@ -5449,6 +5250,9 @@ namespace Mono.CSharp {
                                }
                        }
                        
+                       if (mg.InstanceExpression != null)
+                               mg.InstanceExpression.CheckMarshallByRefAccess (ec.ContainerType);
+
                        eclass = ExprClass.Value;
                        return this;
                }
@@ -5816,8 +5620,8 @@ namespace Mono.CSharp {
                        //
                        // First try to resolve it as a cast.
                        //
-                       TypeExpr te = expr.ResolveAsTypeTerminal (ec, true);
-                       if (te != null) {
+                       TypeExpr te = expr.ResolveAsTypeStep (ec) as TypeExpr;
+                       if ((te != null) && (te.eclass == ExprClass.Type)) {
                                Cast cast = new Cast (te, argument, loc);
                                return cast.Resolve (ec);
                        }
@@ -5863,8 +5667,8 @@ namespace Mono.CSharp {
                        //
                        // First try to resolve it as a cast.
                        //
-                       TypeExpr te = expr.ResolveAsTypeTerminal (ec, true);
-                       if (te != null) {
+                       TypeExpr te = expr.ResolveAsTypeStep (ec) as TypeExpr;
+                       if ((te != null) && (te.eclass == ExprClass.Type)) {
                                error201 ();
                                return null;
                        }
@@ -5999,11 +5803,11 @@ namespace Mono.CSharp {
                                return this;
                        }
                        
-                       TypeExpr texpr = RequestedType.ResolveAsTypeTerminal (ec, false);
+                       TypeExpr texpr = RequestedType.ResolveAsTypeTerminal (ec);
                        if (texpr == null)
                                return null;
                        
-                       type = texpr.ResolveType (ec);
+                       type = texpr.Type;
                        if (type == null)
                                return null;
                        
@@ -6014,7 +5818,7 @@ namespace Mono.CSharp {
                        if (IsDelegate){
                                RequestedType = (new NewDelegate (type, Arguments, loc)).Resolve (ec);
                                if (RequestedType != null)
-                                       if (!(RequestedType is NewDelegate))
+                                       if (!(RequestedType is DelegateCreation))
                                                throw new Exception ("NewDelegate.Resolve returned a non NewDelegate: " + RequestedType.GetType ());
                                return RequestedType;
                        }
@@ -6536,11 +6340,11 @@ namespace Mono.CSharp {
                        //
                        TypeExpr array_type_expr;
                        array_type_expr = new ComposedCast (requested_base_type, array_qualifier.ToString (), loc);
-                       array_type_expr = array_type_expr.ResolveAsTypeTerminal (ec, false);
+                       array_type_expr = array_type_expr.ResolveAsTypeTerminal (ec);
                        if (array_type_expr == null)
                                return false;
 
-                       type = array_type_expr.ResolveType (ec);
+                       type = array_type_expr.Type;
 
                        if (!type.IsArray) {
                                Error (622, "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
@@ -7040,7 +6844,7 @@ namespace Mono.CSharp {
                        eclass = ExprClass.Variable;
 
                        if (ec.TypeContainer.CurrentType != null)
-                               type = ec.TypeContainer.CurrentType.ResolveType (ec);
+                               type = ec.TypeContainer.CurrentType;
                        else
                                type = ec.ContainerType;
 
@@ -7255,11 +7059,11 @@ namespace Mono.CSharp {
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec, false);
+                       TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec);
                        if (texpr == null)
                                return null;
 
-                       typearg = texpr.ResolveType (ec);
+                       typearg = texpr.Type;
 
                        if (typearg == TypeManager.void_type) {
                                Error (673, "System.Void cannot be used from C# - " +
@@ -7329,7 +7133,7 @@ namespace Mono.CSharp {
                                return null;
                        }
                                
-                       TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec, false);
+                       TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec);
                        if (texpr == null)
                                return null;
 
@@ -7338,7 +7142,7 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       type_queried = texpr.ResolveType (ec);
+                       type_queried = texpr.Type;
 
                        CheckObsoleteAttribute (type_queried);
 
@@ -7408,6 +7212,8 @@ namespace Mono.CSharp {
                        return RootContext.LookupType (ec.DeclSpace, sn.Name, true, loc) != null;
                }
                
+               // TODO: possible optimalization
+               // Cache resolved constant result in FieldBuilder <-> expresion map
                public static Expression ResolveMemberAccess (EmitContext ec, Expression member_lookup,
                                                              Expression left, Location loc,
                                                              Expression left_original)
@@ -7433,7 +7239,10 @@ namespace Mono.CSharp {
                                FieldInfo fi = fe.FieldInfo.Mono_GetGenericFieldDefinition ();
                                Type decl_type = fi.DeclaringType;
 
-                               if (fi is FieldBuilder) {
+                               bool is_emitted = fi is FieldBuilder;
+                               Type t = fi.FieldType;
+
+                               if (is_emitted) {
                                        Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
                                        
                                        if (c != null) {
@@ -7443,16 +7252,21 @@ namespace Mono.CSharp {
 
                                                object real_value = ((Constant) c.Expr).GetValue ();
 
-                                               return Constantify (real_value, fi.FieldType);
+                                               return Constantify (real_value, t);
                                        }
                                }
 
+                               // IsInitOnly is because of MS compatibility, I don't know why but they emit decimal constant as InitOnly
+                               if (fi.IsInitOnly && !is_emitted && t == TypeManager.decimal_type) {
+                                       object[] attrs = fi.GetCustomAttributes (TypeManager.decimal_constant_attribute_type, false);
+                                       if (attrs.Length == 1)
+                                               return new DecimalConstant (((System.Runtime.CompilerServices.DecimalConstantAttribute) attrs [0]).Value);
+                               }
+
                                if (fi.IsLiteral) {
-                                       Type t = fi.FieldType;
-                                       
                                        object o;
 
-                                       if (fi is FieldBuilder)
+                                       if (is_emitted)
                                                o = TypeManager.GetValue ((FieldBuilder) fi);
                                        else
                                                o = fi.GetValue (fi);
@@ -7489,7 +7303,7 @@ namespace Mono.CSharp {
                                        return exp;
                                }
 
-                               if (fi.FieldType.IsPointer && !ec.InUnsafe){
+                               if (t.IsPointer && !ec.InUnsafe){
                                        UnsafeError (loc);
                                        return null;
                                }
@@ -7639,7 +7453,7 @@ namespace Mono.CSharp {
 
                        Type expr_type;
                        if (expr is TypeExpr){
-                               expr_type = ((TypeExpr) expr).ResolveType (ec);
+                               expr_type = expr.Type;
 
                                if (!ec.DeclSpace.CheckAccessLevel (expr_type)){
                                        Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", expr_type);
@@ -7781,12 +7595,11 @@ namespace Mono.CSharp {
                                        string full_name = String.Concat (((SimpleName) full_expr.Expr).Name, ".", fname);
                                        Type fully_qualified = ec.DeclSpace.FindType (loc, full_name);
                                        if (fully_qualified != null) {
-                                               if (args != null)
-                                                       return new ConstructedType (
-                                                               fully_qualified, args, loc);
-                                               else
-                                                       return new TypeExpression (
-                                                               fully_qualified, loc);
+                                               if (args == null)
+                                                       return new TypeExpression (fully_qualified, loc);
+
+                                               ConstructedType ctype = new ConstructedType (fully_qualified, args, loc);
+                                               return ctype.ResolveAsTypeStep (ec);
                                        }
                                }
 
@@ -7810,10 +7623,12 @@ namespace Mono.CSharp {
                                return new_expr.ResolveAsTypeStep (ec);
                        }
 
-                       Type expr_type = ((TypeExpr) new_expr).ResolveType (ec);
-                       if (expr_type == null)
+                       TypeExpr tnew_expr = new_expr.ResolveAsTypeTerminal (ec);
+                       if (tnew_expr == null)
                                return null;
 
+                       Type expr_type = tnew_expr.Type;
+
                        if (expr_type.IsPointer){
                                Error (23, "The `.' operator can not be applied to pointer operands (" +
                                       TypeManager.CSharpName (expr_type) + ")");
@@ -7832,8 +7647,8 @@ namespace Mono.CSharp {
                        if (texpr == null)
                                return null;
 
-                       Type t = texpr.ResolveType (ec);
-                       if (t == null)
+                       texpr = texpr.ResolveAsTypeTerminal (ec);
+                       if (texpr == null)
                                return null;
 
                        TypeArguments the_args = args;
@@ -7851,7 +7666,7 @@ namespace Mono.CSharp {
                        }
 
                        if (the_args != null) {
-                               ConstructedType ctype = new ConstructedType (t, the_args, loc);
+                               ConstructedType ctype = new ConstructedType (texpr.Type, the_args, loc);
                                return ctype.ResolveAsTypeStep (ec);
                        }
 
@@ -8631,6 +8446,8 @@ namespace Mono.CSharp {
                                UnsafeError (loc);
                                return null;
                        }
+
+                       instance_expr.CheckMarshallByRefAccess (ec.ContainerType);
                        
                        eclass = ExprClass.IndexerAccess;
                        return this;
@@ -8701,6 +8518,8 @@ namespace Mono.CSharp {
                                }
                        }
                        
+                       instance_expr.CheckMarshallByRefAccess (ec.ContainerType);
+
                        eclass = ExprClass.IndexerAccess;
                        return this;
                }
@@ -8941,6 +8760,12 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
+               public Expression Source {
+                       get {
+                               return source;
+                       }
+               }
+                       
                public override Expression DoResolve (EmitContext ec)
                {
                        //
@@ -8980,13 +8805,13 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
+               protected override TypeExpr DoResolveAsTypeStep (EmitContext ec)
                {
-                       TypeExpr lexpr = left.ResolveAsTypeTerminal (ec, false);
+                       TypeExpr lexpr = left.ResolveAsTypeTerminal (ec);
                        if (lexpr == null)
                                return null;
 
-                       Type ltype = lexpr.ResolveType (ec);
+                       Type ltype = lexpr.Type;
 
                        if ((ltype == TypeManager.void_type) && (dim != "*")) {
                                Report.Error (1547, Location,
@@ -9037,13 +8862,7 @@ namespace Mono.CSharp {
                                //
                                // For now, fall back to the full lookup in that case.
                                //
-                               TypeExpr texpr = RootContext.LookupType (
-                                       ec.DeclSpace, cname, false, loc);
-
-                               if (texpr == null)
-                                       return null;
-
-                               type = texpr.ResolveType (ec);
+                               type = RootContext.LookupType (ec.DeclSpace, cname, false, loc);
                                if (type == null)
                                        return null;
                        }
@@ -9173,13 +8992,11 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       TypeExpr texpr = t.ResolveAsTypeTerminal (ec, false);
+                       TypeExpr texpr = t.ResolveAsTypeTerminal (ec);
                        if (texpr == null)
                                return null;
 
-                       otype = texpr.ResolveType (ec);
-                       if (otype == null)
-                               return null;
+                       otype = texpr.Type;
 
                        if (!TypeManager.VerifyUnManaged (otype, loc))
                                return null;