2001-11-15 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / mcs / expression.cs
index 5320a23cc14fbf2b8152b384de8e2d21848715e8..6151d4ff3105bdd97900934d6bbaa202f72ea423 100755 (executable)
@@ -16,6 +16,65 @@ namespace CIR {
        using System.Reflection.Emit;
        using System.Text;
 
+       // <summary>
+       //   This is just a helper class, it is generated by Unary, UnaryMutator
+       //   when an overloaded method has been found.  It just emits the code for a
+       //   static call.
+       // </summary>
+       public class StaticCallExpr : ExpressionStatement {
+               ArrayList args;
+               MethodInfo mi;
+
+               StaticCallExpr (MethodInfo m, ArrayList a)
+               {
+                       mi = m;
+                       args = a;
+
+                       type = m.ReturnType;
+                       eclass = ExprClass.Value;
+               }
+
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       //
+                       // We are born fully resolved
+                       //
+                       return this;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       if (args != null) 
+                               Invocation.EmitArguments (ec, mi, args);
+
+                       ec.ig.Emit (OpCodes.Call, mi);
+                       return;
+               }
+               
+               static public Expression MakeSimpleCall (EmitContext ec, MethodGroupExpr mg,
+                                                        Expression e, Location loc)
+               {
+                       ArrayList args;
+                       MethodBase method;
+                       
+                       args = new ArrayList (1);
+                       args.Add (new Argument (e, Argument.AType.Expression));
+                       method = Invocation.OverloadResolve (ec, (MethodGroupExpr) mg, args, loc);
+
+                       if (method == null)
+                               return null;
+
+                       return new StaticCallExpr ((MethodInfo) method, args);
+               }
+
+               public override void EmitStatement (EmitContext ec)
+               {
+                       Emit (ec);
+                       if (type != TypeManager.void_type)
+                               ec.ig.Emit (OpCodes.Pop);
+               }
+       }
+       
        // <summary>
        //   Unary expressions.  
        // </summary>
@@ -25,17 +84,14 @@ namespace CIR {
        //   ExpressionStatement becuase the pre/post increment/decrement
        //   operators can be used in a statement context.
        // </remarks>
-       public class Unary : ExpressionStatement {
-               public enum Operator {
+       public class Unary : Expression {
+               public enum Operator : byte {
                        UnaryPlus, UnaryNegation, LogicalNot, OnesComplement,
-                       Indirection, AddressOf, PreIncrement,
-                       PreDecrement, PostIncrement, PostDecrement 
+                       Indirection, AddressOf, 
                }
 
                Operator   oper;
                Expression expr;
-               ArrayList  Arguments;
-               MethodBase method;
                Location   loc;
                
                public Unary (Operator op, Expression expr, Location loc)
@@ -83,23 +139,11 @@ namespace CIR {
                                return "&";
                        case Operator.Indirection:
                                return "*";
-                       case Operator.PreIncrement : case Operator.PostIncrement :
-                               return "++";
-                       case Operator.PreDecrement : case Operator.PostDecrement :
-                               return "--";
                        }
 
                        return oper.ToString ();
                }
 
-               Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
-               {
-                       if (expr.Type == target_type)
-                               return expr;
-
-                       return ConvertImplicit (ec, expr, target_type, new Location (-1));
-               }
-
                void error23 (Type t)
                {
                        Report.Error (
@@ -108,28 +152,6 @@ namespace CIR {
                                TypeManager.CSharpName (t) + "'");
                }
 
-               // <summary>
-               //   Returns whether an object of type `t' can be incremented
-               //   or decremented with add/sub (ie, basically whether we can
-               //   use pre-post incr-decr operations on it, but it is not a
-               //   System.Decimal, which we test elsewhere)
-               // </summary>
-               static bool IsIncrementableNumber (Type t)
-               {
-                       return (t == TypeManager.sbyte_type) ||
-                               (t == TypeManager.byte_type) ||
-                               (t == TypeManager.short_type) ||
-                               (t == TypeManager.ushort_type) ||
-                               (t == TypeManager.int32_type) ||
-                               (t == TypeManager.uint32_type) ||
-                               (t == TypeManager.int64_type) ||
-                               (t == TypeManager.uint64_type) ||
-                               (t == TypeManager.char_type) ||
-                               (t.IsSubclassOf (TypeManager.enum_type)) ||
-                               (t == TypeManager.float_type) ||
-                               (t == TypeManager.double_type);
-               }
-
                static Expression TryReduceNegative (Expression expr)
                {
                        Expression e = null;
@@ -160,12 +182,7 @@ namespace CIR {
                        Expression mg;
                        string op_name;
                        
-                       if (oper == Operator.PostIncrement || oper == Operator.PreIncrement)
-                               op_name = "op_Increment";
-                       else if (oper == Operator.PostDecrement || oper == Operator.PreDecrement)
-                               op_name = "op_Decrement";
-                       else
-                               op_name = "op_" + oper;
+                       op_name = "op_" + oper;
 
                        mg = MemberLookup (ec, expr_type, op_name, false, loc);
                        
@@ -173,20 +190,15 @@ namespace CIR {
                                mg = MemberLookup (ec, expr_type.BaseType, op_name, false, loc);
                        
                        if (mg != null) {
-                               Arguments = new ArrayList ();
-                               Arguments.Add (new Argument (expr, Argument.AType.Expression));
-                               
-                               method = Invocation.OverloadResolve (ec, (MethodGroupExpr) mg,
-                                                                    Arguments, loc);
-                               if (method != null) {
-                                       MethodInfo mi = (MethodInfo) method;
-                                       type = mi.ReturnType;
-                                       return this;
-                               } else {
+                               Expression e = StaticCallExpr.MakeSimpleCall (
+                                       ec, (MethodGroupExpr) mg, expr, loc);
+
+                               if (e == null){
                                        error23 (expr_type);
                                        return null;
                                }
-                                       
+                               
+                               return e;
                        }
 
                        //
@@ -194,7 +206,7 @@ namespace CIR {
                        //
 
                        // Only perform numeric promotions on:
-                       // +, -, ++, --
+                       // +, - 
 
                        if (expr_type == null)
                                return null;
@@ -311,47 +323,19 @@ namespace CIR {
                                return null;
                        }
 
-                       //
-                       // The operand of the prefix/postfix increment decrement operators
-                       // should be an expression that is classified as a variable,
-                       // a property access or an indexer access
-                       //
-                       if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
-                           oper == Operator.PostDecrement || oper == Operator.PostIncrement){
-                               if (expr.ExprClass == ExprClass.Variable){
-                                       if (IsIncrementableNumber (expr_type) ||
-                                           expr_type == TypeManager.decimal_type){
-                                               type = expr_type;
-                                               return this;
-                                       }
-                               } else if (expr.ExprClass == ExprClass.IndexerAccess){
-                                       //
-                                       // FIXME: Verify that we have both get and set methods
-                                       //
-                                       throw new Exception ("Implement me");
-                               } else if (expr.ExprClass == ExprClass.PropertyAccess){
-                                       PropertyExpr pe = (PropertyExpr) expr;
-                                       
-                                       if (pe.VerifyAssignable ())
-                                               return this;
-                                       return null;
-                               } else {
-                                       report118 (loc, expr, "variable, indexer or property access");
-                               }
-                       }
-
                        if (oper == Operator.AddressOf){
                                if (expr.ExprClass != ExprClass.Variable){
-                                       Error (211, "Cannot take the address of non-variables");
+                                       Error (211, loc, "Cannot take the address of non-variables");
                                        return null;
                                }
                                type = Type.GetType (expr.Type.ToString () + "*");
+
+                               return this;
                        }
                        
-                       Error (187, "No such operator '" + OperName () + "' defined for type '" +
+                       Error (187, loc, "No such operator '" + OperName () + "' defined for type '" +
                               TypeManager.CSharpName (expr_type) + "'");
                        return null;
-
                }
 
                public override Expression DoResolve (EmitContext ec)
@@ -370,40 +354,6 @@ namespace CIR {
                        ILGenerator ig = ec.ig;
                        Type expr_type = expr.Type;
                        
-                       if (method != null) {
-
-                               // Note that operators are static anyway
-                               
-                               if (Arguments != null) 
-                                       Invocation.EmitArguments (ec, Arguments);
-
-                               //
-                               // Post increment/decrement operations need a copy at this
-                               // point.
-                               //
-                               if (oper == Operator.PostDecrement || oper == Operator.PostIncrement)
-                                       ig.Emit (OpCodes.Dup);
-                               
-
-                               ig.Emit (OpCodes.Call, (MethodInfo) method);
-
-                               //
-                               // Pre Increment and Decrement operators
-                               //
-                               if (oper == Operator.PreIncrement || oper == Operator.PreDecrement){
-                                       ig.Emit (OpCodes.Dup);
-                               }
-                               
-                               //
-                               // Increment and Decrement should store the result
-                               //
-                               if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
-                                   oper == Operator.PostDecrement || oper == Operator.PostIncrement){
-                                       ((IStackStore) expr).Store (ec);
-                               }
-                               return;
-                       }
-                       
                        switch (oper) {
                        case Operator.UnaryPlus:
                                throw new Exception ("This should be caught by Resolve");
@@ -431,46 +381,6 @@ namespace CIR {
                        case Operator.Indirection:
                                throw new Exception ("Not implemented yet");
                                
-                       case Operator.PreIncrement:
-                       case Operator.PreDecrement:
-                               if (expr.ExprClass == ExprClass.Variable){
-                                       //
-                                       // Resolve already verified that it is an "incrementable"
-                                       // 
-                                       expr.Emit (ec);
-                                       ig.Emit (OpCodes.Ldc_I4_1);
-                                       
-                                       if (oper == Operator.PreDecrement)
-                                               ig.Emit (OpCodes.Sub);
-                                       else
-                                               ig.Emit (OpCodes.Add);
-                                       ig.Emit (OpCodes.Dup);
-                                       ((IStackStore) expr).Store (ec);
-                               } else {
-                                       throw new Exception ("Handle Indexers and Properties here");
-                               }
-                               break;
-                               
-                       case Operator.PostIncrement:
-                       case Operator.PostDecrement:
-                               if (expr is IStackStore){
-                                       //
-                                       // Resolve already verified that it is an "incrementable"
-                                       // 
-                                       expr.Emit (ec);
-                                       ig.Emit (OpCodes.Dup);
-                                       ig.Emit (OpCodes.Ldc_I4_1);
-                                       
-                                       if (oper == Operator.PostDecrement)
-                                               ig.Emit (OpCodes.Sub);
-                                       else
-                                               ig.Emit (OpCodes.Add);
-                                       ((IStackStore) expr).Store (ec);
-                               } else {
-                                       Console.WriteLine ("Unknown exprclass: " + expr);
-                               }
-                               break;
-                               
                        default:
                                throw new Exception ("This should not happen: Operator = "
                                                     + oper.ToString ());
@@ -489,27 +399,10 @@ namespace CIR {
                        expr.Emit (ec);
                }
                
-               public override void EmitStatement (EmitContext ec)
-               {
-                       //
-                       // FIXME: we should rewrite this code to generate
-                       // better code for ++ and -- as we know we wont need
-                       // the values on the stack
-                       //
-                       Emit (ec);
-                       ec.ig.Emit (OpCodes.Pop);
-               }
-
                public override Expression Reduce (EmitContext ec)
                {
                        Expression e;
                        
-                       //
-                       // We can not reduce expressions that invoke operator overloaded functions.
-                       //
-                       if (method != null)
-                               return this;
-
                        //
                        // First, reduce our child.  Note that although we handle 
                        //
@@ -548,6 +441,233 @@ namespace CIR {
                        return this;
                }
        }
+
+       // <summary>
+       //   Unary Mutator expressions (pre and post ++ and --)
+       // </summary>
+       //
+       // <remarks>
+       //   UnaryMutator implements ++ and -- expressions.   It derives from
+       //   ExpressionStatement becuase the pre/post increment/decrement
+       //   operators can be used in a statement context.
+       // </remarks>
+       //
+       // FIXME: Idea, we could split this up in two classes, one simpler
+       // for the common case, and one with the extra fields for more complex
+       // classes (indexers require temporary access;  overloaded require method)
+       //
+       // Maybe we should have classes PreIncrement, PostIncrement, PreDecrement,
+       // PostDecrement, that way we could save the `Mode' byte as well.  
+       //
+       public class UnaryMutator : ExpressionStatement {
+               public enum Mode : byte {
+                       PreIncrement, PreDecrement, PostIncrement, PostDecrement
+               }
+               
+               Mode mode;
+               Location loc;
+               Expression expr;
+               LocalTemporary temp_storage;
+
+               //
+               // This is expensive for the simplest case.
+               //
+               Expression method;
+                       
+               public UnaryMutator (Mode m, Expression e, Location l)
+               {
+                       mode = m;
+                       loc = l;
+                       expr = e;
+               }
+
+               string OperName ()
+               {
+                       return (mode == Mode.PreIncrement || mode == Mode.PostIncrement) ?
+                               "++" : "--";
+               }
+               
+               void error23 (Type t)
+               {
+                       Report.Error (
+                               23, loc, "Operator " + OperName () + 
+                               " cannot be applied to operand of type `" +
+                               TypeManager.CSharpName (t) + "'");
+               }
+
+               // <summary>
+               //   Returns whether an object of type `t' can be incremented
+               //   or decremented with add/sub (ie, basically whether we can
+               //   use pre-post incr-decr operations on it, but it is not a
+               //   System.Decimal, which we require operator overloading to catch)
+               // </summary>
+               static bool IsIncrementableNumber (Type t)
+               {
+                       return (t == TypeManager.sbyte_type) ||
+                               (t == TypeManager.byte_type) ||
+                               (t == TypeManager.short_type) ||
+                               (t == TypeManager.ushort_type) ||
+                               (t == TypeManager.int32_type) ||
+                               (t == TypeManager.uint32_type) ||
+                               (t == TypeManager.int64_type) ||
+                               (t == TypeManager.uint64_type) ||
+                               (t == TypeManager.char_type) ||
+                               (t.IsSubclassOf (TypeManager.enum_type)) ||
+                               (t == TypeManager.float_type) ||
+                               (t == TypeManager.double_type);
+               }
+
+               Expression ResolveOperator (EmitContext ec)
+               {
+                       Type expr_type = expr.Type;
+
+                       //
+                       // Step 1: Perform Operator Overload location
+                       //
+                       Expression mg;
+                       string op_name;
+                       
+                       if (mode == Mode.PreIncrement || mode == Mode.PostIncrement)
+                               op_name = "op_Increment";
+                       else 
+                               op_name = "op_Decrement";
+
+                       mg = MemberLookup (ec, expr_type, op_name, false, loc);
+
+                       if (mg == null && expr_type.BaseType != null)
+                               mg = MemberLookup (ec, expr_type.BaseType, op_name, false, loc);
+                       
+                       if (mg != null) {
+                               method = StaticCallExpr.MakeSimpleCall (
+                                       ec, (MethodGroupExpr) mg, expr, loc);
+
+                               type = method.Type;
+                               return this;
+                       }
+
+                       //
+                       // The operand of the prefix/postfix increment decrement operators
+                       // should be an expression that is classified as a variable,
+                       // a property access or an indexer access
+                       //
+                       type = expr_type;
+                       if (expr.ExprClass == ExprClass.Variable){
+                               if (IsIncrementableNumber (expr_type) ||
+                                   expr_type == TypeManager.decimal_type){
+                                       return this;
+                               }
+                       } else if (expr.ExprClass == ExprClass.IndexerAccess){
+                               IndexerAccess ia = (IndexerAccess) expr;
+                               
+                               temp_storage = new LocalTemporary (ec, expr.Type);
+                               
+                               expr = ia.ResolveLValue (ec, temp_storage);
+                               if (expr == null)
+                                       return null;
+
+                               return this;
+                       } else if (expr.ExprClass == ExprClass.PropertyAccess){
+                               PropertyExpr pe = (PropertyExpr) expr;
+
+                               if (pe.VerifyAssignable ())
+                                       return this;
+
+                               return null;
+                       } else {
+                               report118 (loc, expr, "variable, indexer or property access");
+                               return null;
+                       }
+
+                       Error (187, loc, "No such operator '" + OperName () + "' defined for type '" +
+                              TypeManager.CSharpName (expr_type) + "'");
+                       return null;
+               }
+
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       expr = expr.Resolve (ec);
+                       
+                       if (expr == null)
+                               return null;
+
+                       eclass = ExprClass.Value;
+                       return ResolveOperator (ec);
+               }
+               
+
+               //
+               // FIXME: We need some way of avoiding the use of temp_storage
+               // for some types of storage (parameters, local variables,
+               // static fields) and single-dimension array access.
+               //
+               void EmitCode (EmitContext ec, bool is_expr)
+               {
+                       ILGenerator ig = ec.ig;
+                       IAssignMethod ia = (IAssignMethod) expr;
+
+                       if (temp_storage == null)
+                               temp_storage = new LocalTemporary (ec, expr.Type);
+                       
+                       switch (mode){
+                       case Mode.PreIncrement:
+                       case Mode.PreDecrement:
+                               if (method == null){
+                                       expr.Emit (ec);
+
+                                       ig.Emit (OpCodes.Ldc_I4_1);
+                               
+                                       if (mode == Mode.PreDecrement)
+                                               ig.Emit (OpCodes.Sub);
+                                       else
+                                               ig.Emit (OpCodes.Add);
+                               } else
+                                       method.Emit (ec);
+                               
+                               temp_storage.Store (ec);
+                               ia.EmitAssign (ec, temp_storage);
+                               if (is_expr)
+                                       temp_storage.Emit (ec);
+                               break;
+                               
+                       case Mode.PostIncrement:
+                       case Mode.PostDecrement:
+                               if (is_expr)
+                                       expr.Emit (ec);
+                               
+                               if (method == null){
+                                       if (!is_expr)
+                                               expr.Emit (ec);
+                                       else
+                                               ig.Emit (OpCodes.Dup);
+
+                                       ig.Emit (OpCodes.Ldc_I4_1);
+                               
+                                       if (mode == Mode.PostDecrement)
+                                               ig.Emit (OpCodes.Sub);
+                                       else
+                                               ig.Emit (OpCodes.Add);
+                               } else {
+                                       method.Emit (ec);
+                               }
+                               
+                               temp_storage.Store (ec);
+                               ia.EmitAssign (ec, temp_storage);
+                               break;
+                       }
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       EmitCode (ec, true);
+                       
+               }
+               
+               public override void EmitStatement (EmitContext ec)
+               {
+                       EmitCode (ec, false);
+               }
+
+       }
        
        public class Probe : Expression {
                public readonly string ProbeType;
@@ -555,7 +675,7 @@ namespace CIR {
                Expression expr;
                Type probe_type;
                
-               public enum Operator {
+               public enum Operator : byte {
                        Is, As
                }
                
@@ -663,7 +783,7 @@ namespace CIR {
        }
 
        public class Binary : Expression {
-               public enum Operator {
+               public enum Operator : byte {
                        Multiply, Division, Modulus,
                        Addition, Subtraction,
                        LeftShift, RightShift,
@@ -1237,7 +1357,7 @@ namespace CIR {
                                // Note that operators are static anyway
                                
                                if (Arguments != null) 
-                                       Invocation.EmitArguments (ec, Arguments);
+                                       Invocation.EmitArguments (ec, method, Arguments);
                                
                                if (method is MethodInfo)
                                        ig.Emit (OpCodes.Call, (MethodInfo) method);
@@ -1370,7 +1490,6 @@ namespace CIR {
                // </summary>
                public override Expression Reduce (EmitContext ec)
                {
-                       Console.WriteLine ("Reduce called");
                        
                        left = left.Reduce (ec);
                        right = right.Reduce (ec);
@@ -1506,7 +1625,7 @@ namespace CIR {
                }
        }
 
-       public class LocalVariableReference : Expression, IStackStore, IMemoryLocation {
+       public class LocalVariableReference : Expression, IAssignMethod, IMemoryLocation {
                public readonly string Name;
                public readonly Block Block;
 
@@ -1596,21 +1715,23 @@ namespace CIR {
                                break;
                        }
                }
-               
-               public void Store (EmitContext ec)
+
+               public void EmitAssign (EmitContext ec, Expression source)
                {
                        ILGenerator ig = ec.ig;
                        VariableInfo vi = VariableInfo;
 
                        vi.Assigned = true;
 
-                       // Funny seems the above generates optimal code for us, but
+                       source.Emit (ec);
+                       
+                       // Funny seems the code below generates optimal code for us, but
                        // seems to take too long to generate what we need.
                        // ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
 
                        Store (ig, vi.Idx);
                }
-
+               
                public void AddressOf (EmitContext ec)
                {
                        VariableInfo vi = VariableInfo;
@@ -1626,7 +1747,7 @@ namespace CIR {
                }
        }
 
-       public class ParameterReference : Expression, IStackStore, IMemoryLocation {
+       public class ParameterReference : Expression, IAssignMethod, IMemoryLocation {
                public readonly Parameters Pars;
                public readonly String Name;
                public readonly int Idx;
@@ -1661,8 +1782,10 @@ namespace CIR {
                                ec.ig.Emit (OpCodes.Ldarg, arg_idx);
                }
 
-               public void Store (EmitContext ec)
+               public void EmitAssign (EmitContext ec, Expression source)
                {
+                       source.Emit (ec);
+                       
                        if (arg_idx <= 255)
                                ec.ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
                        else
@@ -1683,7 +1806,7 @@ namespace CIR {
        //   Used for arguments to New(), Invocation()
        // </summary>
        public class Argument {
-               public enum AType {
+               public enum AType : byte {
                        Expression,
                        Ref,
                        Out
@@ -1691,7 +1814,7 @@ namespace CIR {
 
                public readonly AType ArgType;
                public Expression expr;
-
+               
                public Argument (Expression expr, AType type)
                {
                        this.expr = expr;
@@ -1727,29 +1850,34 @@ namespace CIR {
 
                public static string FullDesc (Argument a)
                {
-                       StringBuilder sb = new StringBuilder ();
-
-                       if (a.ArgType == AType.Ref)
-                               sb.Append ("ref ");
-
-                       if (a.ArgType == AType.Out)
-                               sb.Append ("out ");
-
-                       sb.Append (TypeManager.CSharpName (a.Expr.Type));
-
-                       return sb.ToString ();
+                       return (a.ArgType == AType.Ref ? "ref " :
+                               (a.ArgType == AType.Out ? "out " : "")) +
+                               TypeManager.CSharpName (a.Expr.Type);
                }
                
-               public bool Resolve (EmitContext ec)
+               public bool Resolve (EmitContext ec, Location loc)
                {
                        expr = expr.Resolve (ec);
 
+                       if (ArgType == AType.Expression)
+                               return expr != null;
+
+                       if (expr.ExprClass != ExprClass.Variable){
+                               Report.Error (206, loc,
+                                             "A property or indexer can not be passed as an out or ref " +
+                                             "parameter");
+                               return false;
+                       }
+                               
                        return expr != null;
                }
 
                public void Emit (EmitContext ec)
                {
-                       expr.Emit (ec);
+                       if (ArgType == AType.Ref || ArgType == AType.Out)
+                               ((IMemoryLocation)expr).AddressOf (ec);
+                       else
+                               expr.Emit (ec);
                }
        }
 
@@ -1758,7 +1886,7 @@ namespace CIR {
        // </summary>
        public class Invocation : ExpressionStatement {
                public readonly ArrayList Arguments;
-               public readonly Location Location;
+               Location loc;
 
                Expression expr;
                MethodBase method = null;
@@ -1781,7 +1909,7 @@ namespace CIR {
                {
                        this.expr = expr;
                        Arguments = arguments;
-                       Location = l;
+                       loc = l;
                }
 
                public Expression Expr {
@@ -1809,6 +1937,7 @@ namespace CIR {
 
                                return (ParameterData) ip;
                        } else {
+                               Console.WriteLine ("Getting parameters for: " + mb);
                                ParameterInfo [] pi = mb.GetParameters ();
                                ReflectionParameters rp = new ReflectionParameters (pi);
                                method_parameter_cache [mb] = rp;
@@ -2425,12 +2554,12 @@ namespace CIR {
                                        bool IsDelegate = TypeManager.IsDelegateType (expr_type);
                                        if (IsDelegate)
                                                return (new DelegateInvocation (
-                                                       this.expr, Arguments, Location)).Resolve (ec);
+                                                       this.expr, Arguments, loc)).Resolve (ec);
                                }
                        }
 
                        if (!(expr is MethodGroupExpr)){
-                               report118 (Location, this.expr, "method group");
+                               report118 (loc, this.expr, "method group");
                                return null;
                        }
 
@@ -2442,16 +2571,15 @@ namespace CIR {
                                        --i;
                                        Argument a = (Argument) Arguments [i];
 
-                                       if (!a.Resolve (ec))
+                                       if (!a.Resolve (ec, loc))
                                                return null;
                                }
                        }
 
-                       method = OverloadResolve (ec, (MethodGroupExpr) this.expr, Arguments,
-                                                 Location);
+                       method = OverloadResolve (ec, (MethodGroupExpr) this.expr, Arguments, loc);
 
                        if (method == null){
-                               Error (-6, Location,
+                               Error (-6, loc,
                                       "Could not find any applicable function for this argument list");
                                return null;
                        }
@@ -2463,18 +2591,66 @@ namespace CIR {
                        return this;
                }
 
-               public static void EmitArguments (EmitContext ec, ArrayList Arguments)
+               // <summary>
+               //   Emits the list of arguments as an array
+               // </summary>
+               static void EmitParams (EmitContext ec, int idx, ArrayList arguments)
                {
+                       ILGenerator ig = ec.ig;
+                       int count = arguments.Count - idx;
+                       Argument a = (Argument) arguments [idx];
+                       Type t = a.expr.Type;
+                       string array_type = t.FullName + "[]";
+                       LocalBuilder array;
+                       
+                       array = ec.GetTemporaryStorage (Type.GetType (array_type));
+                       IntLiteral.EmitInt (ig, count);
+                       ig.Emit (OpCodes.Newarr, t);
+                       ig.Emit (OpCodes.Stloc, array);
+
+                       int top = arguments.Count;
+                       for (int j = idx; j < top; j++){
+                               ig.Emit (OpCodes.Ldloc, array);
+                               IntLiteral.EmitInt (ig, j - idx);
+                               a.Emit (ec);
+                               ig.Emit (OpCodes.Stelem_Ref);
+                       }
+                       ig.Emit (OpCodes.Ldloc, array);
+               }
+               
+               // <summary>
+               //   Emits a list of resolved Arguments that are in the arguments
+               //   ArrayList.
+               // 
+               //   The MethodBase argument might be null if the
+               //   emission of the arguments is known not to contain
+               //   a `params' field (for example in constructors or other routines
+               //   that keep their arguments in this structure
+               // </summary>
+               
+               public static void EmitArguments (EmitContext ec, MethodBase mb, ArrayList arguments)
+               {
+                       ParameterData pd = null;
                        int top;
 
-                       if (Arguments != null)
-                               top = Arguments.Count;
+                       if (arguments != null)
+                               top = arguments.Count;
                        else
                                top = 0;
 
+                       if (mb != null)
+                                pd = GetParameterData (mb);
+
                        for (int i = 0; i < top; i++){
-                               Argument a = (Argument) Arguments [i];
+                               Argument a = (Argument) arguments [i];
 
+                               if (pd != null){
+                                       if (pd.ParameterModifier (i) == Parameter.Modifier.PARAMS){
+                                               EmitParams (ec, i, arguments);
+                                               return;
+                                       }
+                               }
+                                           
                                a.Emit (ec);
                        }
                }
@@ -2523,7 +2699,7 @@ namespace CIR {
                        }
 
                        if (Arguments != null)
-                               EmitArguments (ec, Arguments);
+                               EmitArguments (ec, method, Arguments);
 
                        if (is_static || struct_call){
                                if (method is MethodInfo)
@@ -2562,7 +2738,7 @@ namespace CIR {
                public readonly ArrayList Arguments;
                public readonly string    RequestedType;
 
-               Location Location;
+               Location loc;
                MethodBase method = null;
 
                //
@@ -2571,11 +2747,11 @@ namespace CIR {
                //
                Expression value_target;
                
-               public New (string requested_type, ArrayList arguments, Location loc)
+               public New (string requested_type, ArrayList arguments, Location l)
                {
                        RequestedType = requested_type;
                        Arguments = arguments;
-                       Location = loc;
+                       loc = l;
                }
 
                public Expression ValueTypeVariable {
@@ -2598,19 +2774,19 @@ namespace CIR {
                        bool IsDelegate = TypeManager.IsDelegateType (type);
                        
                        if (IsDelegate)
-                               return (new NewDelegate (type, Arguments, Location)).Resolve (ec);
+                               return (new NewDelegate (type, Arguments, loc)).Resolve (ec);
                        
                        Expression ml;
                        
                        ml = MemberLookup (ec, type, ".ctor", false,
-                                          MemberTypes.Constructor, AllBindingsFlags, Location);
+                                          MemberTypes.Constructor, AllBindingsFlags, loc);
                        
                        bool is_struct = false;
                        is_struct = type.IsSubclassOf (TypeManager.value_type);
                        
                        if (! (ml is MethodGroupExpr)){
                                if (!is_struct){
-                                       report118 (Location, ml, "method group");
+                                       report118 (loc, ml, "method group");
                                        return null;
                                }
                        }
@@ -2621,17 +2797,17 @@ namespace CIR {
                                                --i;
                                                Argument a = (Argument) Arguments [i];
                                                
-                                               if (!a.Resolve (ec))
+                                               if (!a.Resolve (ec, loc))
                                                        return null;
                                        }
                                }
 
                                method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml,
-                                                                    Arguments, Location);
+                                                                    Arguments, loc);
                        }
                        
                        if (method == null && !is_struct) {
-                               Error (-6, Location,
+                               Error (-6, loc,
                                       "New invocation: Can not find a constructor for " +
                                       "this argument list");
                                return null;
@@ -2659,7 +2835,7 @@ namespace CIR {
 
                                ml.AddressOf (ec);
                        } else {
-                               Invocation.EmitArguments (ec, Arguments);
+                               Invocation.EmitArguments (ec, method, Arguments);
                                ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
                                return true;
                        }
@@ -2706,7 +2882,7 @@ namespace CIR {
                string RequestedType;
                string Rank;
                ArrayList Initializers;
-               Location  Location;
+               Location  loc;
                ArrayList Arguments;
 
                MethodBase method = null;
@@ -2721,7 +2897,7 @@ namespace CIR {
                        RequestedType = requested_type;
                        Rank          = rank;
                        Initializers  = initializers;
-                       Location      = l;
+                       loc = l;
 
                        Arguments = new ArrayList ();
 
@@ -2735,7 +2911,7 @@ namespace CIR {
                        RequestedType = requested_type;
                        Rank = rank;
                        Initializers = initializers;
-                       Location = l;
+                       loc = l;
                }
 
                public static string FormArrayType (string base_type, int idx_count, string rank)
@@ -2802,15 +2978,15 @@ namespace CIR {
                                Expression ml;
                                
                                ml = MemberLookup (ec, type, ".ctor", false, MemberTypes.Constructor,
-                                                  AllBindingsFlags, Location);
+                                                  AllBindingsFlags, loc);
                                
                                if (!(ml is MethodGroupExpr)){
-                                       report118 (Location, ml, "method group");
+                                       report118 (loc, ml, "method group");
                                        return null;
                                }
                                
                                if (ml == null) {
-                                       Report.Error (-6, Location, "New invocation: Can not find a constructor for " +
+                                       Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
                                                      "this argument list");
                                        return null;
                                }
@@ -2820,15 +2996,15 @@ namespace CIR {
                                                --i;
                                                Argument a = (Argument) Arguments [i];
                                                
-                                               if (!a.Resolve (ec))
+                                               if (!a.Resolve (ec, loc))
                                                        return null;
                                        }
                                }
                                
-                               method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, Arguments, Location);
+                               method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, Arguments, loc);
                                
                                if (method == null) {
-                                       Report.Error (-6, Location, "New invocation: Can not find a constructor for " +
+                                       Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
                                                      "this argument list");
                                        return null;
                                }
@@ -2846,7 +3022,7 @@ namespace CIR {
                                                --i;
                                                Argument a = (Argument) Arguments [i];
                                                
-                                               if (!a.Resolve (ec))
+                                               if (!a.Resolve (ec, loc))
                                                        return null;
                                                
                                                args.Add (a.Type);
@@ -2864,7 +3040,7 @@ namespace CIR {
                                                            arg_types);
                                
                                if (method == null) {
-                                       Report.Error (-6, Location, "New invocation: Can not find a constructor for " +
+                                       Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
                                                      "this argument list");
                                        return null;
                                }
@@ -2880,11 +3056,11 @@ namespace CIR {
                        ILGenerator ig = ec.ig;
                        
                        if (IsOneDimensional) {
-                               Invocation.EmitArguments (ec, Arguments);
+                               Invocation.EmitArguments (ec, null, Arguments);
                                ig.Emit (OpCodes.Newarr, array_element_type);
                                
                        } else {
-                               Invocation.EmitArguments (ec, Arguments);
+                               Invocation.EmitArguments (ec, method, Arguments);
 
                                if (IsBuiltinType)
                                        ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
@@ -2918,7 +3094,7 @@ namespace CIR {
        //
        // Represents the `this' construct
        //
-       public class This : Expression, IStackStore, IMemoryLocation {
+       public class This : Expression, IAssignMethod, IMemoryLocation {
                Location loc;
                
                public This (Location loc)
@@ -2957,8 +3133,9 @@ namespace CIR {
                        ec.ig.Emit (OpCodes.Ldarg_0);
                }
 
-               public void Store (EmitContext ec)
+               public void EmitAssign (EmitContext ec, Expression source)
                {
+                       source.Emit (ec);
                        ec.ig.Emit (OpCodes.Starg, 0);
                }
 
@@ -3060,7 +3237,7 @@ namespace CIR {
 
                        if (expr is SimpleName){
                                SimpleName child_expr = (SimpleName) expr;
-                               
+
                                expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
 
                                return expr.Resolve (ec);
@@ -3108,7 +3285,12 @@ namespace CIR {
 
                                if (fi.IsLiteral) {
                                        Type t = fi.FieldType;
-                                       object o = fi.GetValue (null);
+                                       object o;
+
+                                       if (fi is FieldBuilder)
+                                               o = TypeManager.GetValue ((FieldBuilder) fi);
+                                       else
+                                               o = fi.GetValue (fi);
                                        
                                        if (t.IsSubclassOf (TypeManager.enum_type)) {
                                                Expression enum_member = MemberLookup (ec, t, "value__", false, loc); 
@@ -3269,7 +3451,7 @@ namespace CIR {
                                --i;
                                Argument a = (Argument) Arguments [i];
                                
-                               if (!a.Resolve (ec))
+                               if (!a.Resolve (ec, loc))
                                        return false;
                        }
 
@@ -3347,6 +3529,38 @@ namespace CIR {
                        return this;
                }
 
+               // <summary>
+               //    Emits the right opcode to load an object of Type `t'
+               //    from an array of T
+               // </summary>
+               static public void EmitLoadOpcode (ILGenerator ig, Type type)
+               {
+                       if (type == TypeManager.byte_type)
+                               ig.Emit (OpCodes.Ldelem_I1);
+                       else if (type == TypeManager.sbyte_type)
+                               ig.Emit (OpCodes.Ldelem_U1);
+                       else if (type == TypeManager.short_type)
+                               ig.Emit (OpCodes.Ldelem_I2);
+                       else if (type == TypeManager.ushort_type)
+                               ig.Emit (OpCodes.Ldelem_U2);
+                       else if (type == TypeManager.int32_type)
+                               ig.Emit (OpCodes.Ldelem_I4);
+                       else if (type == TypeManager.uint32_type)
+                               ig.Emit (OpCodes.Ldelem_U4);
+                       else if (type == TypeManager.uint64_type)
+                               ig.Emit (OpCodes.Ldelem_I8);
+                       else if (type == TypeManager.int64_type)
+                               ig.Emit (OpCodes.Ldelem_I8);
+                       else if (type == TypeManager.float_type)
+                               ig.Emit (OpCodes.Ldelem_R4);
+                       else if (type == TypeManager.double_type)
+                               ig.Emit (OpCodes.Ldelem_R8);
+                       else if (type == TypeManager.intptr_type)
+                               ig.Emit (OpCodes.Ldelem_I);
+                       else
+                               ig.Emit (OpCodes.Ldelem_Ref);
+               }
+               
                public override void Emit (EmitContext ec)
                {
                        int rank = ea.Expr.Type.GetArrayRank ();
@@ -3357,32 +3571,9 @@ namespace CIR {
                        foreach (Argument a in ea.Arguments)
                                a.Expr.Emit (ec);
 
-                       if (rank == 1){
-                               if (type == TypeManager.byte_type)
-                                       ig.Emit (OpCodes.Ldelem_I1);
-                               else if (type == TypeManager.sbyte_type)
-                                       ig.Emit (OpCodes.Ldelem_U1);
-                               else if (type == TypeManager.short_type)
-                                       ig.Emit (OpCodes.Ldelem_I2);
-                               else if (type == TypeManager.ushort_type)
-                                       ig.Emit (OpCodes.Ldelem_U2);
-                               else if (type == TypeManager.int32_type)
-                                       ig.Emit (OpCodes.Ldelem_I4);
-                               else if (type == TypeManager.uint32_type)
-                                       ig.Emit (OpCodes.Ldelem_U4);
-                               else if (type == TypeManager.uint64_type)
-                                       ig.Emit (OpCodes.Ldelem_I8);
-                               else if (type == TypeManager.int64_type)
-                                       ig.Emit (OpCodes.Ldelem_I8);
-                               else if (type == TypeManager.float_type)
-                                       ig.Emit (OpCodes.Ldelem_R4);
-                               else if (type == TypeManager.double_type)
-                                       ig.Emit (OpCodes.Ldelem_R8);
-                               else if (type == TypeManager.intptr_type)
-                                       ig.Emit (OpCodes.Ldelem_I);
-                               else
-                                       ig.Emit (OpCodes.Ldelem_Ref);
-                       } else {
+                       if (rank == 1)
+                               EmitLoadOpcode (ig, type);
+                       else {
                                ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
                                Type [] args = new Type [ea.Arguments.Count];
                                MethodInfo get;
@@ -3511,7 +3702,10 @@ namespace CIR {
                        return ix;
                }
        }
-       
+
+       // <summary>
+       //   Expressions that represent an indexer call.
+       // </summary>
        public class IndexerAccess : Expression, IAssignMethod {
                //
                // Points to our "data" repository
@@ -3527,11 +3721,6 @@ namespace CIR {
                        eclass = ExprClass.Value;
                }
 
-               public bool VerifyAssignable (Expression source)
-               {
-                       throw new Exception ("Implement me!");
-               }
-
                public override Expression DoResolve (EmitContext ec)
                {
                        Type indexer_type = ea.Expr.Type;
@@ -3545,7 +3734,11 @@ namespace CIR {
                        if (ilist == null)
                                ilist = Indexers.GetIndexersForType (
                                        indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
-                       
+
+
+                       //
+                       // Step 2: find the proper match
+                       //
                        if (ilist != null && ilist.getters != null && ilist.getters.Count > 0)
                                get = (MethodInfo) Invocation.OverloadResolve (
                                        ec, new MethodGroupExpr (ilist.getters), ea.Arguments, ea.loc);
@@ -3554,11 +3747,11 @@ namespace CIR {
                                Report.Error (154, ea.loc,
                                              "indexer can not be used in this context, because " +
                                              "it lacks a `get' accessor");
-                                       return null;
+                               return null;
                        }
 
                        type = get.ReturnType;
-                       eclass = ExprClass.Value;
+                       eclass = ExprClass.IndexerAccess;
                        return this;
                }
 
@@ -3609,7 +3802,7 @@ namespace CIR {
        
        public class BaseAccess : Expression {
 
-               public enum BaseAccessType {
+               public enum BaseAccessType : byte {
                        Member,
                        Indexer
                };
@@ -3654,6 +3847,12 @@ namespace CIR {
                        eclass = ExprClass.Value;
                }
 
+               public EmptyExpression (Type t)
+               {
+                       type = t;
+                       eclass = ExprClass.Value;
+               }
+               
                public override Expression DoResolve (EmitContext ec)
                {
                        return this;