2006-04-28 Marek Safar <marek.safar@seznam.cz>
[mono.git] / mcs / mcs / expression.cs
index afb6734945bb7e8caf58f938a57801cebe457dda..5a2b3615b25bef788e947fae9d719f7942fd6df6 100644 (file)
@@ -693,7 +693,7 @@ namespace Mono.CSharp {
                        Emit (ec);
                        if (leave_copy) {
                                ec.ig.Emit (OpCodes.Dup);
-                               temporary = new LocalTemporary (ec, expr.Type);
+                               temporary = new LocalTemporary (expr.Type);
                                temporary.Store (ec);
                        }
                }
@@ -710,7 +710,7 @@ namespace Mono.CSharp {
                        source.Emit (ec);
                        if (leave_copy) {
                                ec.ig.Emit (OpCodes.Dup);
-                               temporary = new LocalTemporary (ec, expr.Type);
+                               temporary = new LocalTemporary (expr.Type);
                                temporary.Store (ec);
                        }
                        
@@ -1254,9 +1254,9 @@ namespace Mono.CSharp {
                        return null;
                }
        
-               public override bool GetAttributableValue (out object value)
+               public override bool GetAttributableValue (Type valueType, out object value)
                {
-                       return expr.GetAttributableValue (out value);
+                       return expr.GetAttributableValue (valueType, out value);
                }
        }
        
@@ -1281,33 +1281,17 @@ namespace Mono.CSharp {
                        this.expr = expr;
                        this.loc = loc;
 
-                       if (target_type == TypeManager.system_void_expr) {
+                       if (target_type == TypeManager.system_void_expr)
                                Report.Error (1547, loc, "Keyword `void' cannot be used in this context");
-                       }
                }
 
                public Expression TargetType {
-                       get {
-                               return target_type;
-                       }
+                       get { return target_type; }
                }
 
                public Expression Expr {
-                       get {
-                               return expr;
-                       }
-                       set {
-                               expr = value;
-                       }
-               }
-               
-               public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
-               {
-                       expr = expr.DoResolveLValue (ec, right_side);
-                       if (expr == null)
-                               return null;
-
-                       return ResolveRest (ec);
+                       get { return expr; }
+                       set { expr = value; }
                }
 
                public override Expression DoResolve (EmitContext ec)
@@ -1316,11 +1300,6 @@ namespace Mono.CSharp {
                        if (expr == null)
                                return null;
 
-                       return ResolveRest (ec);
-               }
-
-               Expression ResolveRest (EmitContext ec)
-               {
                        TypeExpr target = target_type.ResolveAsTypeTerminal (ec, false);
                        if (target == null)
                                return null;
@@ -1356,9 +1335,6 @@ namespace Mono.CSharp {
                
                public override void Emit (EmitContext ec)
                {
-                       //
-                       // This one will never happen
-                       //
                        throw new Exception ("Should not happen");
                }
        }
@@ -3117,7 +3093,7 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       left_temp = new LocalTemporary (ec, type);
+                       left_temp = new LocalTemporary (type);
 
                        ArrayList arguments = new ArrayList ();
                        arguments.Add (new Argument (left_temp, Argument.AType.Expression));
@@ -3399,7 +3375,7 @@ namespace Mono.CSharp {
                bool is_readonly;
                bool prepared;
                LocalTemporary temp;
-               
+
                public LocalVariableReference (Block block, string name, Location l)
                {
                        Block = block;
@@ -3421,15 +3397,11 @@ namespace Mono.CSharp {
                }
 
                public VariableInfo VariableInfo {
-                       get {
-                               return local_info.VariableInfo;
-                       }
+                       get { return local_info.VariableInfo; }
                }
 
                public bool IsReadOnly {
-                       get {
-                               return is_readonly;
-                       }
+                       get { return is_readonly; }
                }
 
                public bool VerifyAssigned (EmitContext ec)
@@ -3438,52 +3410,25 @@ namespace Mono.CSharp {
                        return variable_info == null || variable_info.IsAssigned (ec, loc);
                }
 
-               protected Expression DoResolveBase (EmitContext ec, Expression lvalue_right_side)
+               void ResolveLocalInfo ()
                {
                        if (local_info == null) {
                                local_info = Block.GetLocalInfo (Name);
-
-                               // is out param
-                               if (lvalue_right_side == EmptyExpression.OutAccess)
-                                       local_info.Used = true;
-
                                is_readonly = local_info.ReadOnly;
                        }
+               }
 
+               protected Expression DoResolveBase (EmitContext ec)
+               {
                        type = local_info.VariableType;
 
-                       VariableInfo variable_info = local_info.VariableInfo;
-                       if (lvalue_right_side != null){
-                               if (is_readonly){
-                                       if (lvalue_right_side is LocalVariableReference || lvalue_right_side == EmptyExpression.OutAccess)
-                                               Report.Error (1657, loc, "Cannot pass `{0}' as a ref or out argument because it is a `{1}'",
-                                                       Name, local_info.GetReadOnlyContext ());
-                                       else if (lvalue_right_side == EmptyExpression.LValueMemberAccess)
-                                               Report.Error (1654, loc, "Cannot assign to members of `{0}' because it is a `{1}'",
-                                                       Name, local_info.GetReadOnlyContext ());
-                                       else
-                                               Report.Error (1656, loc, "Cannot assign to `{0}' because it is a `{1}'",
-                                                       Name, local_info.GetReadOnlyContext ());
-                                       return null;
-                               }
-                               
-                               if (variable_info != null)
-                                       variable_info.SetAssigned (ec);
-                       }
-               
                        Expression e = Block.GetConstantExpression (Name);
-                       if (e != null) {
-                               local_info.Used = true;
-                               eclass = ExprClass.Value;
+                       if (e != null)
                                return e.Resolve (ec);
-                       }
 
                        if (!VerifyAssigned (ec))
                                return null;
 
-                       if (lvalue_right_side == null)
-                               local_info.Used = true;
-
                        if (ec.CurrentAnonymousMethod != null){
                                //
                                // If we are referencing a variable from the external block
@@ -3502,15 +3447,42 @@ namespace Mono.CSharp {
 
                        return this;
                }
-               
+
                public override Expression DoResolve (EmitContext ec)
                {
-                       return DoResolveBase (ec, null);
+                       ResolveLocalInfo ();
+                       local_info.Used = true;
+                       return DoResolveBase (ec);
                }
 
                override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
                {
-                       return DoResolveBase (ec, right_side);
+                       ResolveLocalInfo ();
+
+                       if (is_readonly) {
+                               int code;
+                               string msg;
+                               if (right_side == EmptyExpression.OutAccess) {
+                                       code = 1657; msg = "Cannot pass `{0}' as a ref or out argument because it is a `{1}'";
+                               } else if (right_side == EmptyExpression.LValueMemberAccess) {
+                                       code = 1654; msg = "Cannot assign to members of `{0}' because it is a `{1}'";
+                               } else if (right_side == EmptyExpression.LValueMemberOutAccess) {
+                                       code = 1655; msg = "Cannot pass members of `{0}' as ref or out arguments because it is a `{1}'";
+                               } else {
+                                       code = 1656; msg = "Cannot assign to `{0}' because it is a `{1}'";
+                               }
+                               Report.Error (code, loc, msg, Name, local_info.GetReadOnlyContext ());
+                               return null;
+                       }
+
+                       // is out param
+                       if (right_side == EmptyExpression.OutAccess)
+                               local_info.Used = true;
+
+                       if (VariableInfo != null)
+                               VariableInfo.SetAssigned (ec);
+
+                       return DoResolveBase (ec);
                }
 
                public bool VerifyFixed ()
@@ -3519,7 +3491,7 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               public override int GetHashCode()
+               public override int GetHashCode ()
                {
                        return Name.GetHashCode ();
                }
@@ -3559,7 +3531,7 @@ namespace Mono.CSharp {
                        if (leave_copy){
                                ec.ig.Emit (OpCodes.Dup);
                                if (local_info.FieldBuilder != null){
-                                       temp = new LocalTemporary (ec, Type);
+                                       temp = new LocalTemporary (Type);
                                        temp.Store (ec);
                                }
                        }
@@ -3592,7 +3564,7 @@ namespace Mono.CSharp {
                                source.Emit (ec);
                                if (leave_copy){
                                        ig.Emit (OpCodes.Dup);
-                                       temp = new LocalTemporary (ec, Type);
+                                       temp = new LocalTemporary (Type);
                                        temp.Store (ec);
                                }
                                ig.Emit (OpCodes.Stfld, local_info.FieldBuilder);
@@ -3855,7 +3827,7 @@ namespace Mono.CSharp {
                                ec.ig.Emit (OpCodes.Dup);
                                
                                if (is_ref) {
-                                       temp = new LocalTemporary (ec, type);
+                                       temp = new LocalTemporary (type);
                                        temp.Store (ec);
                                }
                        }
@@ -3887,7 +3859,7 @@ namespace Mono.CSharp {
                        
                        if (is_ref) {
                                if (leave_copy) {
-                                       temp = new LocalTemporary (ec, type);
+                                       temp = new LocalTemporary (type);
                                        temp.Store (ec);
                                }
                                
@@ -4005,106 +3977,47 @@ namespace Mono.CSharp {
 
                        return true;
                }
-               
-               void Error_LValueRequired (Location loc)
-               {
-                       Report.Error (1510, loc, "A ref or out argument must be an assignable variable");
-               }
 
                public bool Resolve (EmitContext ec, Location loc)
                {
                        bool old_do_flow_analysis = ec.DoFlowAnalysis;
                        ec.DoFlowAnalysis = true;
 
-                       if (ArgType == AType.Ref) {
-                               ec.InRefOutArgumentResolving = true;
+                       // Verify that the argument is readable
+                       if (ArgType != AType.Out)
                                Expr = Expr.Resolve (ec);
-                               ec.InRefOutArgumentResolving = false;
-                               if (Expr == null) {
-                                       ec.DoFlowAnalysis = old_do_flow_analysis;
-                                       return false;
-                               }
-
-                               int errors = Report.Errors;
-                               Expr = Expr.DoResolveLValue (ec, Expr);
-                               if (Expr == null && errors == Report.Errors)
-                                       Error_LValueRequired (loc);
-                       } else if (ArgType == AType.Out) {
-                               int errors = Report.Errors;
-                               ec.InRefOutArgumentResolving = true;
-                               Expr = Expr.DoResolveLValue (ec, EmptyExpression.OutAccess);
-                               ec.InRefOutArgumentResolving = false;
 
-                               if (Expr == null && errors == Report.Errors)
-                                       Error_LValueRequired (loc);
-                       }
-                       else
-                               Expr = Expr.Resolve (ec);
+                       // Verify that the argument is writeable
+                       if (Expr != null && (ArgType == AType.Out || ArgType == AType.Ref))
+                               Expr = Expr.ResolveLValue (ec, EmptyExpression.OutAccess, loc);
 
                        ec.DoFlowAnalysis = old_do_flow_analysis;
 
-                       if (Expr == null)
-                               return false;
-
-                       if (ArgType == AType.Expression)
-                               return true;
-                       else {
-                               //
-                               // Catch errors where fields of a MarshalByRefObject are passed as ref or out
-                               // This is only allowed for `this'
-                               //
-                               FieldExpr fe = Expr as FieldExpr;
-                               if (fe != null && !fe.IsStatic){
-                                       Expression instance = fe.InstanceExpression;
-
-                                       if (instance.GetType () != typeof (This)){
-                                               if (fe.InstanceExpression.Type.IsSubclassOf (TypeManager.mbr_type)){
-                                                       Report.SymbolRelatedToPreviousError (fe.InstanceExpression.Type);
-                                                       Report.Warning (197, 1, loc,
-                                                               "Passing `{0}' as ref or out or taking its address may cause a runtime exception because it is a field of a marshal-by-reference class",
-                                                               fe.GetSignatureForError ());
-                                                       return false;
-                                               }
-                                       }
-                               }
-                       }
-                               
-                       return true;
+                       return Expr != null;
                }
 
                public void Emit (EmitContext ec)
                {
-                       //
-                       // Ref and Out parameters need to have their addresses taken.
+                       if (ArgType != AType.Ref && ArgType != AType.Out) {
+                               Expr.Emit (ec);
+                               return;
+                       }
+
+                       AddressOp mode = AddressOp.Store;
+                       if (ArgType == AType.Ref)
+                               mode |= AddressOp.Load;
+                               
+                       IMemoryLocation ml = (IMemoryLocation) Expr;
+                       ParameterReference pr = ml as ParameterReference;
+
                        //
                        // ParameterReferences might already be references, so we want
                        // to pass just the value
                        //
-                       if (ArgType == AType.Ref || ArgType == AType.Out){
-                               AddressOp mode = AddressOp.Store;
-
-                               if (ArgType == AType.Ref)
-                                       mode |= AddressOp.Load;
-                               
-                               if (Expr is ParameterReference){
-                                       ParameterReference pr = (ParameterReference) Expr;
-
-                                       if (pr.IsRef)
-                                               pr.EmitLoad (ec);
-                                       else {
-                                               
-                                               pr.AddressOf (ec, mode);
-                                       }
-                               } else {
-                                       if (Expr is IMemoryLocation)
-                                               ((IMemoryLocation) Expr).AddressOf (ec, mode);
-                                       else {
-                                               Error_LValueRequired (Expr.Location);
-                                               return;
-                                       }
-                               }
-                       } else
-                               Expr.Emit (ec);
+                       if (pr != null && pr.IsRef)
+                               pr.EmitLoad (ec);
+                       else
+                               ml.AddressOf (ec, mode);
                }
        }
 
@@ -4879,85 +4792,54 @@ namespace Mono.CSharp {
                                                          Location loc)
                {
                        ParameterData pd = TypeManager.GetParameterData (method);
-                       int pd_count = pd.Count;
-
-                       for (int j = 0; j < arg_count; j++) {
+                       int j;
+                       for (j = 0; j < arg_count; j++) {
                                Argument a = (Argument) Arguments [j];
                                Expression a_expr = a.Expr;
                                Type parameter_type = pd.ParameterType (j);
                                Parameter.Modifier pm = pd.ParameterModifier (j);
-                               
-                               if (pm == Parameter.Modifier.PARAMS){
-                                       if ((pm & ~Parameter.Modifier.PARAMS) != a.Modifier) {
-                                               if (!may_fail)
-                                                       Error_InvalidArguments (loc, j, method, delegate_type, a, pd);
-                                               return false;
-                                       }
+                               Parameter.Modifier am = a.Modifier;
+
+                               if (pm == Parameter.Modifier.ARGLIST) {
+                                       if (!(a.Expr is Arglist))
+                                               break;
+                                       continue;
+                               }
 
+                               if (pm == Parameter.Modifier.PARAMS) {
+                                       pm = Parameter.Modifier.NONE;
                                        if (chose_params_expanded)
                                                parameter_type = TypeManager.GetElementType (parameter_type);
-                               } else if (pm == Parameter.Modifier.ARGLIST) {
-                                       if (!(a.Expr is Arglist)) {
-                                               if (!may_fail)
-                                                       Error_InvalidArguments (loc, j, method, delegate_type, a, pd);
-                                               return false;
-                                       }
-                                       continue;
-                               } else {
-                                       //
-                                       // Check modifiers
-                                       //
-                                       if (pd.ParameterModifier (j) != a.Modifier){
-                                               if (!may_fail)
-                                                       Error_InvalidArguments (loc, j, method, delegate_type, a, pd);
-                                               return false;
-                                       }
                                }
 
-                               //
-                               // Check Type
-                               //
-                               if (!a.Type.Equals (parameter_type)){
-                                       Expression conv;
-                                       
-                                       conv = Convert.ImplicitConversion (ec, a_expr, parameter_type, loc);
+                               if (pm != am)
+                                       break;
+
+                               if (!a.Type.Equals (parameter_type)) {
+                                       if (pm == Parameter.Modifier.OUT || pm == Parameter.Modifier.REF)
+                                               break;
+
+                                       Expression conv = Convert.ImplicitConversion (ec, a_expr, parameter_type, loc);
+                                       if (conv == null)
+                                               break;
 
-                                       if (conv == null) {
-                                               if (!may_fail)
-                                                       Error_InvalidArguments (loc, j, method, delegate_type, a, pd);
-                                               return false;
-                                       }
-                                       
-                                       //
                                        // Update the argument with the implicit conversion
-                                       //
                                        if (a_expr != conv)
                                                a.Expr = conv;
                                }
 
-                               if (parameter_type.IsPointer){
-                                       if (!ec.InUnsafe){
-                                               UnsafeError (loc);
-                                               return false;
-                                       }
-                               }
-                               
-                               Parameter.Modifier a_mod = a.Modifier &
-                                       unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF));
-                               Parameter.Modifier p_mod = pd.ParameterModifier (j) &
-                                       unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF));
-                               
-                               if (a_mod != p_mod &&
-                                   pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS) {
-                                       if (!may_fail) {
-                                               Invocation.Error_InvalidArguments (loc, j, method, null, a, pd);
-                                       }
-                                       
+                               if (parameter_type.IsPointer && !ec.InUnsafe) {
+                                       UnsafeError (loc);
                                        return false;
                                }
                        }
 
-                       return true;
+                       if (j == arg_count)
+                               return true;
+
+                       if (!may_fail)
+                               Error_InvalidArguments (loc, j, method, delegate_type, (Argument) Arguments [j], pd);
+                       return false;
                }
 
                private bool resolved = false;
@@ -5056,7 +4938,7 @@ namespace Mono.CSharp {
                        }
 
                        if (mg.InstanceExpression != null)
-                               mg.InstanceExpression.CheckMarshallByRefAccess (ec.ContainerType);
+                               mg.InstanceExpression.CheckMarshalByRefAccess ();
 
                        eclass = ExprClass.Value;
                        this.method = method;
@@ -5166,7 +5048,7 @@ namespace Mono.CSharp {
                                a.Emit (ec);
                                if (dup_args) {
                                        ec.ig.Emit (OpCodes.Dup);
-                                       (temps [i] = new LocalTemporary (ec, a.Type)).Store (ec);
+                                       (temps [i] = new LocalTemporary (a.Type)).Store (ec);
                                }
                        }
                        
@@ -5331,7 +5213,7 @@ namespace Mono.CSharp {
                                                                ((IMemoryLocation)instance_expr).
                                                                        AddressOf (ec, AddressOp.LoadStore);
                                                        } else {
-                                                               LocalTemporary temp = new LocalTemporary (ec, instance_expr.Type);
+                                                               LocalTemporary temp = new LocalTemporary (instance_expr.Type);
                                                                instance_expr.Emit (ec);
                                                                temp.Store (ec);
                                                                temp.AddressOf (ec, AddressOp.Load);
@@ -5353,7 +5235,7 @@ namespace Mono.CSharp {
                                        if (dup_args) {
                                                ig.Emit (OpCodes.Dup);
                                                if (Arguments != null && Arguments.Count != 0) {
-                                                       this_arg = new LocalTemporary (ec, t);
+                                                       this_arg = new LocalTemporary (t);
                                                        this_arg.Store (ec);
                                                }
                                        }
@@ -5780,7 +5662,7 @@ namespace Mono.CSharp {
                                // We need to create a new LocalTemporary each time since
                                // you can't share LocalBuilders among ILGeneators.
                                if (!value_target_set)
-                                       value_target = new LocalTemporary (ec, type);
+                                       value_target = new LocalTemporary (type);
 
                                ml = (IMemoryLocation) value_target;
                                ml.AddressOf (ec, AddressOp.Store);
@@ -5829,7 +5711,7 @@ namespace Mono.CSharp {
                        }
 
                        if (!value_target_set)
-                               value_target = new LocalTemporary (ec, type);
+                               value_target = new LocalTemporary (type);
                                        
                        IMemoryLocation ml = (IMemoryLocation) value_target;
                        ml.AddressOf (ec, AddressOp.Store);
@@ -5881,15 +5763,10 @@ namespace Mono.CSharp {
 
                ArrayList array_data;
 
-               Hashtable bounds;
-
-               //
-               // The number of array initializers that we can handle
-               // via the InitializeArray method - through EmitStaticInitializers
-               //
-               int num_automatic_initializers;
+               IDictionary bounds;
 
-               const int max_automatic_initializers = 6;
+               // The number of constants in array initializers
+               int const_initializers_count;
                
                public ArrayCreation (Expression requested_base_type, ArrayList exprs, string rank, ArrayList initializers, Location l)
                {
@@ -5939,7 +5816,7 @@ namespace Mono.CSharp {
                        Error (178, "Invalid rank specifier: expected `,' or `]'");
                }
                
-               public bool CheckIndices (EmitContext ec, ArrayList probe, int idx, bool specified_dims)
+               bool CheckIndices (EmitContext ec, ArrayList probe, int idx, bool specified_dims)
                {
                        if (specified_dims) { 
                                Argument a = (Argument) arguments [idx];
@@ -5981,7 +5858,7 @@ namespace Mono.CSharp {
                                                Error_IncorrectArrayInitializer ();
                                                return false;
                                        }
-                                       if (specified_dims && (idx + 1 >= arguments.Count)){
+                                       if (idx + 1 >= dimensions){
                                                Error (623, "Array initializers can only be used in a variable or field initializer. Try using a new expression instead");
                                                return false;
                                        }
@@ -5997,31 +5874,30 @@ namespace Mono.CSharp {
                                        
                                        Expression tmp = (Expression) o;
                                        tmp = tmp.Resolve (ec);
-                                       probe [i] = tmp;
                                        if (tmp == null)
                                                return false;
 
-                                       // Console.WriteLine ("I got: " + tmp);
-                                       // Handle initialization from vars, fields etc.
-
                                        Expression conv = Convert.ImplicitConversionRequired (
                                                ec, tmp, underlying_type, loc);
                                        
                                        if (conv == null) 
                                                return false;
+
+                                       // Initializers with the default values can be ignored
+                                       Constant c = tmp as Constant;
+                                       if (c != null) {
+                                               if (c.IsDefaultInitializer (array_element_type)) {
+                                                       conv = null;
+                                               }
+                                               else {
+                                                       ++const_initializers_count;
+                                               }
+                                       } else {
+                                               // Used to invalidate static initializer
+                                               const_initializers_count = int.MinValue;
+                                       }
                                        
-                                       if (conv is StringConstant || conv is DecimalConstant || conv is NullCast) {
-                                               // These are subclasses of Constant that can appear as elements of an
-                                               // array that cannot be statically initialized (with num_automatic_initializers
-                                               // > max_automatic_initializers), so num_automatic_initializers should be left as zero.
-                                               array_data.Add (conv);
-                                       } else if (conv is Constant) {
-                                               // These are the types of Constant that can appear in arrays that can be
-                                               // statically allocated.
-                                               array_data.Add (conv);
-                                               num_automatic_initializers++;
-                                       } else
-                                               array_data.Add (conv);
+                                       array_data.Add (conv);
                                }
                        }
 
@@ -6045,13 +5921,13 @@ namespace Mono.CSharp {
                                        arguments.Add (new Argument (e, Argument.AType.Expression));
 
                                        bounds [i++] = probe.Count;
-                                       probe = null;
+                                       return;
                                }
                        }
 
                }
                
-               public bool ValidateInitializers (EmitContext ec)
+               bool ResolveInitializers (EmitContext ec)
                {
                        if (initializers == null) {
                                return !expect_initializers;
@@ -6065,25 +5941,24 @@ namespace Mono.CSharp {
                        // will need to store them in the byte blob later
                        //
                        array_data = new ArrayList ();
-                       bounds = new Hashtable ();
+                       bounds = new System.Collections.Specialized.HybridDictionary ();
                        
-                       if (arguments != null) {
+                       if (arguments != null)
                                return CheckIndices (ec, initializers, 0, true);
-                       } else {
-                               arguments = new ArrayList ();
 
-                               if (!CheckIndices (ec, initializers, 0, false))
-                                       return false;
+                       arguments = new ArrayList ();
+
+                       if (!CheckIndices (ec, initializers, 0, false))
+                               return false;
                                
-                               UpdateIndices ();
+                       UpdateIndices ();
                                
-                               if (arguments.Count != dimensions) {
-                                       Error_IncorrectArrayInitializer ();
-                                       return false;
-                               }
-
-                               return true;
+                       if (arguments.Count != dimensions) {
+                               Error_IncorrectArrayInitializer ();
+                               return false;
                        }
+
+                       return true;
                }
 
                //
@@ -6122,18 +5997,26 @@ namespace Mono.CSharp {
                
                public override Expression DoResolve (EmitContext ec)
                {
-                       int arg_count;
+                       if (type != null)
+                               return this;
 
                        if (!LookupType (ec))
                                return null;
                        
+                       array_element_type = TypeManager.GetElementType (type);
+                       if (array_element_type.IsAbstract && array_element_type.IsSealed) {
+                               Report.Error (719, loc, "`{0}': array elements cannot be of static type", TypeManager.CSharpName (array_element_type));
+                               return null;
+                       }
+
                        //
                        // First step is to validate the initializers and fill
                        // in any missing bits
                        //
-                       if (!ValidateInitializers (ec))
+                       if (!ResolveInitializers (ec))
                                return null;
 
+                       int arg_count;
                        if (arguments == null)
                                arg_count = 0;
                        else {
@@ -6150,13 +6033,6 @@ namespace Mono.CSharp {
                                }
                        }
                        
-                       array_element_type = TypeManager.GetElementType (type);
-
-                       if (array_element_type.IsAbstract && array_element_type.IsSealed) {
-                               Report.Error (719, loc, "`{0}': array elements cannot be of static type", TypeManager.CSharpName (array_element_type));
-                               return null;
-                       }
-
                        if (arg_count == 1) {
                                is_one_dimensional = true;
                                eclass = ExprClass.Value;
@@ -6223,7 +6099,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               public static byte [] MakeByteBlob (ArrayList array_data, Type underlying_type, Location loc)
+               byte [] MakeByteBlob ()
                {
                        int factor;
                        byte [] data;
@@ -6239,7 +6115,7 @@ namespace Mono.CSharp {
 
                        data = new byte [(count * factor + 4) & ~3];
                        int idx = 0;
-                       
+
                        for (int i = 0; i < count; ++i) {
                                object v = array_data [i];
 
@@ -6378,7 +6254,7 @@ namespace Mono.CSharp {
                        FieldBuilder fb;
                        ILGenerator ig = ec.ig;
                        
-                       byte [] data = MakeByteBlob (array_data, underlying_type, loc);
+                       byte [] data = MakeByteBlob ();
 
                        fb = RootContext.MakeStaticData (data);
 
@@ -6399,87 +6275,67 @@ namespace Mono.CSharp {
                        ILGenerator ig = ec.ig;
                        int dims = bounds.Count;
                        int [] current_pos = new int [dims];
-                       int top = array_data.Count;
 
                        MethodInfo set = null;
 
                        if (dims != 1){
-                               Type [] args;
-                               ModuleBuilder mb = null;
-                               mb = CodeGen.Module.Builder;
-                               args = new Type [dims + 1];
+                               Type [] args = new Type [dims + 1];
 
-                               int j;
-                               for (j = 0; j < dims; j++)
+                               for (int j = 0; j < dims; j++)
                                        args [j] = TypeManager.int32_type;
-
-                               args [j] = array_element_type;
+                               args [dims] = array_element_type;
                                
-                               set = mb.GetArrayMethod (
+                               set = CodeGen.Module.Builder.GetArrayMethod (
                                        type, "Set",
                                        CallingConventions.HasThis | CallingConventions.Standard,
                                        TypeManager.void_type, args);
                        }
-                       
-                       for (int i = 0; i < top; i++){
 
-                               Expression e = null;
+                       for (int i = 0; i < array_data.Count; i++){
 
-                               if (array_data [i] is Expression)
-                                       e = (Expression) array_data [i];
+                               Expression e = (Expression)array_data [i];
 
                                if (e != null) {
-                                       //
-                                       // Basically we do this for string literals and
-                                       // other non-literal expressions
-                                       //
-                                       if (e is EnumConstant){
-                                               e = ((EnumConstant) e).Child;
-                                       }
-                                       
-                                       if (e is StringConstant || e is DecimalConstant || !(e is Constant) ||
-                                           num_automatic_initializers <= max_automatic_initializers) {
-                                               Type etype = e.Type;
+                                       Type etype = e.Type;
 
-                                               ig.Emit (OpCodes.Dup);
+                                       ig.Emit (OpCodes.Dup);
 
-                                               for (int idx = 0; idx < dims; idx++) 
-                                                       IntConstant.EmitInt (ig, current_pos [idx]);
+                                       for (int idx = 0; idx < dims; idx++) 
+                                               IntConstant.EmitInt (ig, current_pos [idx]);
 
-                                               //
-                                               // If we are dealing with a struct, get the
-                                               // address of it, so we can store it.
-                                               //
-                                               if ((dims == 1) && 
-                                                   TypeManager.IsValueType (etype) &&
-                                                   (!TypeManager.IsBuiltinOrEnum (etype) ||
-                                                    etype == TypeManager.decimal_type)) {
-                                                       if (e is New){
-                                                               New n = (New) e;
-
-                                                               //
-                                                               // Let new know that we are providing
-                                                               // the address where to store the results
-                                                               //
-                                                               n.DisableTemporaryValueType ();
-                                                       }
+                                       //
+                                       // If we are dealing with a struct, get the
+                                       // address of it, so we can store it.
+                                       //
+                                       if ((dims == 1) && 
+                                               TypeManager.IsValueType (etype) &&
+                                               (!TypeManager.IsBuiltinOrEnum (etype) ||
+                                               etype == TypeManager.decimal_type)) {
+                                               if (e is New){
+                                                       New n = (New) e;
 
-                                                       ig.Emit (OpCodes.Ldelema, etype);
+                                                       //
+                                                       // Let new know that we are providing
+                                                       // the address where to store the results
+                                                       //
+                                                       n.DisableTemporaryValueType ();
                                                }
 
-                                               e.Emit (ec);
+                                               ig.Emit (OpCodes.Ldelema, etype);
+                                       }
 
-                                               if (dims == 1) {
-                                                       bool is_stobj;
-                                                       OpCode op = ArrayAccess.GetStoreOpcode (etype, out is_stobj);
-                                                       if (is_stobj)
-                                                               ig.Emit (OpCodes.Stobj, etype);
-                                                       else
-                                                               ig.Emit (op);
-                                               } else 
-                                                       ig.Emit (OpCodes.Call, set);
+                                       e.Emit (ec);
+
+                                       if (dims == 1) {
+                                               bool is_stobj;
+                                               OpCode op = ArrayAccess.GetStoreOpcode (etype, out is_stobj);
+                                               if (is_stobj)
+                                                       ig.Emit (OpCodes.Stobj, etype);
+                                               else
+                                                       ig.Emit (op);
+                                       } else 
+                                               ig.Emit (OpCodes.Call, set);
 
-                                       }
                                }
                                
                                //
@@ -6523,53 +6379,75 @@ namespace Mono.CSharp {
                                        ig.Emit (OpCodes.Newobj, (MethodInfo) new_method);
                        }
                        
-                       if (initializers != null){
-                               //
-                               // FIXME: Set this variable correctly.
-                               // 
-                               bool dynamic_initializers = true;
+                       if (initializers == null)
+                               return;
 
-                               // This will never be true for array types that cannot be statically
-                               // initialized. num_automatic_initializers will always be zero.  See
-                               // CheckIndices.
-                               if (num_automatic_initializers > max_automatic_initializers)
-                                       EmitStaticInitializers (ec);
-                               
-                               if (dynamic_initializers)
-                                       EmitDynamicInitializers (ec);
+                       // This is a treshold for static initializers
+                       // I tried to make more accurate but it seems to me that Array.Initialize is
+                       // always slower (managed -> unmanaged switch?)
+                       const int max_automatic_initializers = 200;
+
+                       if (const_initializers_count > max_automatic_initializers && TypeManager.IsPrimitiveType (array_element_type)) {
+                               EmitStaticInitializers (ec);
+                               return;
                        }
+                               
+                       EmitDynamicInitializers (ec);
                }
 
-               public override bool GetAttributableValue (out object value)
+               public override bool GetAttributableValue (Type valueType, out object value)
                {
                        if (!is_one_dimensional){
 //                             Report.Error (-211, Location, "attribute can not encode multi-dimensional arrays");
-                               return base.GetAttributableValue (out value);
+                               return base.GetAttributableValue (null, out value);
                        }
 
                        if (array_data == null) {
                                Constant c = (Constant)((Argument)arguments [0]).Expr;
                                if (c.IsDefaultValue) {
-                                       value = new object [0];
+                                       value = Array.CreateInstance (array_element_type, 0);
                                        return true;
                                }
 //                             Report.Error (-212, Location, "array should be initialized when passing it to an attribute");
-                               return base.GetAttributableValue (out value);
+                               return base.GetAttributableValue (null, out value);
                        }
                        
-                       object [] ret = new object [array_data.Count];
+                       Array ret = Array.CreateInstance (array_element_type, array_data.Count);
+                       object element_value;
                        for (int i = 0; i < ret.Length; ++i)
                        {
-                               if (!((Expression)array_data [i]).GetAttributableValue (out ret [i])) {
+                               Expression e = (Expression)array_data [i];
+                               if (e == null) // Is null when initializer is optimized away
+                                       e = (Expression)initializers [i];
+
+                               if (!e.GetAttributableValue (array_element_type, out element_value)) {
                                        value = null;
                                        return false;
                                }
+                               ret.SetValue (element_value, i);
                        }
                        value = ret;
                        return true;
                }
        }
        
+       public sealed class CompilerGeneratedThis : This
+       {
+               public static This Instance = new CompilerGeneratedThis ();
+
+               private CompilerGeneratedThis ()
+                       : base (Location.Null)
+               {
+               }
+
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       eclass = ExprClass.Variable;
+                       type = ec.ContainerType;
+                       return this;
+               }
+       }
+       
        /// <summary>
        ///   Represents the `this' construct
        /// </summary>
@@ -6669,7 +6547,7 @@ namespace Mono.CSharp {
                                
                                LocalTemporary t = null;
                                if (leave_copy) {
-                                       t = new LocalTemporary (ec, type);
+                                       t = new LocalTemporary (type);
                                        ec.ig.Emit (OpCodes.Dup);
                                        t.Store (ec);
                                }
@@ -6861,8 +6739,12 @@ namespace Mono.CSharp {
                        ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
                }
 
-               public override bool GetAttributableValue (out object value)
+               public override bool GetAttributableValue (Type valueType, out object value)
                {
+                       if (valueType == TypeManager.object_type) {
+                               value = (object)typearg;
+                               return true;
+                       }
                        value = typearg;
                        return true;
                }
@@ -7673,7 +7555,7 @@ namespace Mono.CSharp {
                        
                        if (leave_copy) {
                                ec.ig.Emit (OpCodes.Dup);
-                               temp = new LocalTemporary (ec, this.type);
+                               temp = new LocalTemporary (this.type);
                                temp.Store (ec);
                        }
                }
@@ -7696,7 +7578,7 @@ namespace Mono.CSharp {
                                source.Emit (ec);
                                if (leave_copy) {
                                        ec.ig.Emit (OpCodes.Dup);
-                                       temp = new LocalTemporary (ec, this.type);
+                                       temp = new LocalTemporary (this.type);
                                        temp.Store (ec);
                                }
                                StoreFromPtr (ec.ig, t);
@@ -7723,7 +7605,7 @@ namespace Mono.CSharp {
                                source.Emit (ec);
                                if (leave_copy) {
                                        ec.ig.Emit (OpCodes.Dup);
-                                       temp = new LocalTemporary (ec, this.type);
+                                       temp = new LocalTemporary (this.type);
                                        temp.Store (ec);
                                }
                                
@@ -7740,7 +7622,7 @@ namespace Mono.CSharp {
                                source.Emit (ec);
                                if (leave_copy) {
                                        ec.ig.Emit (OpCodes.Dup);
-                                       temp = new LocalTemporary (ec, this.type);
+                                       temp = new LocalTemporary (this.type);
                                        temp.Store (ec);
                                }
                                
@@ -7977,7 +7859,7 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       instance_expr.CheckMarshallByRefAccess (ec.ContainerType);
+                       instance_expr.CheckMarshalByRefAccess ();
                        
                        eclass = ExprClass.IndexerAccess;
                        return this;
@@ -7992,7 +7874,7 @@ namespace Mono.CSharp {
                        }
 
                        // if the indexer returns a value type, and we try to set a field in it
-                       if (right_side == EmptyExpression.LValueMemberAccess) {
+                       if (right_side == EmptyExpression.LValueMemberAccess || right_side == EmptyExpression.LValueMemberOutAccess) {
                                Report.Error (1612, loc, "Cannot modify the return value of `{0}' because it is not a variable",
                                              GetSignatureForError ());
                                return null;
@@ -8057,7 +7939,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       instance_expr.CheckMarshallByRefAccess (ec.ContainerType);
+                       instance_expr.CheckMarshalByRefAccess ();
 
                        eclass = ExprClass.IndexerAccess;
                        return this;
@@ -8071,7 +7953,7 @@ namespace Mono.CSharp {
                        Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, get, arguments, loc, prepared, false);
                        if (leave_copy) {
                                ec.ig.Emit (OpCodes.Dup);
-                               temp = new LocalTemporary (ec, Type);
+                               temp = new LocalTemporary (Type);
                                temp.Store (ec);
                        }
                }
@@ -8090,11 +7972,11 @@ namespace Mono.CSharp {
                                source.Emit (ec);
                                if (leave_copy) {
                                        ec.ig.Emit (OpCodes.Dup);
-                                       temp = new LocalTemporary (ec, Type);
+                                       temp = new LocalTemporary (Type);
                                        temp.Store (ec);
                                }
                        } else if (leave_copy) {
-                               temp = new LocalTemporary (ec, Type);
+                               temp = new LocalTemporary (Type);
                                source.Emit (ec);
                                temp.Store (ec);
                                a.Expr = temp;
@@ -8257,6 +8139,7 @@ namespace Mono.CSharp {
 
                public static readonly EmptyExpression OutAccess = new EmptyExpression ();
                public static readonly EmptyExpression LValueMemberAccess = new EmptyExpression ();
+               public static readonly EmptyExpression LValueMemberOutAccess = new EmptyExpression ();
 
                static EmptyExpression temp = new EmptyExpression ();
                public static EmptyExpression Grab ()