2009-12-01 Marek Safar <marek.safar@gmail.com>
authorMarek Safar <marek.safar@gmail.com>
Tue, 1 Dec 2009 14:05:18 +0000 (14:05 -0000)
committerMarek Safar <marek.safar@gmail.com>
Tue, 1 Dec 2009 14:05:18 +0000 (14:05 -0000)
 argument.cs, assign.cs, expression.cs, cs-parser.jay: Named
 arguments by ref.

svn path=/trunk/mcs/; revision=147227

mcs/mcs/ChangeLog
mcs/mcs/argument.cs
mcs/mcs/assign.cs
mcs/mcs/cs-parser.jay
mcs/mcs/expression.cs

index 8e68c746d52c01b4cad6a7d4d3d753ee8c0d82d0..652e0302643d192b821e0e635d8fb562a550f9b0 100644 (file)
@@ -1,3 +1,8 @@
+2009-12-01  Marek Safar  <marek.safar@gmail.com>
+
+        argument.cs, assign.cs, expression.cs, cs-parser.jay: Named
+        arguments by ref.
+
 2009-12-01  Marek Safar  <marek.safar@gmail.com>
 
        A fix for bug #360455
index d8ff7e39dc8df9da2814f4f145c6e00d355ce0c7..c75f5b048e97b77d38d81b02281acd85b3eecd0d 100644 (file)
@@ -137,16 +137,7 @@ namespace Mono.CSharp
                                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 (pr != null && pr.IsRef)
-                               pr.EmitLoad (ec);
-                       else
-                               ml.AddressOf (ec, mode);
+                       ml.AddressOf (ec, mode);
                }
 
                public Argument Clone (CloneContext clonectx)
@@ -163,10 +154,15 @@ namespace Mono.CSharp
                readonly Location loc;
                LocalTemporary variable;
 
-               public NamedArgument (string Name, Location loc, Expression expr)
-                       : base (expr)
+               public NamedArgument (string name, Location loc, Expression expr)
+                       : this (name, loc, expr, AType.None)
                {
-                       this.Name = Name;
+               }
+
+               public NamedArgument (string name, Location loc, Expression expr, AType modifier)
+                       : base (expr, modifier)
+               {
+                       this.Name = name;
                        this.loc = loc;
                }
 
@@ -188,8 +184,16 @@ namespace Mono.CSharp
 
                public void EmitAssign (EmitContext ec)
                {
-                       Expr.Emit (ec);
-                       variable = new LocalTemporary (Expr.Type);
+                       var type = Expr.Type;
+                       if (IsByRef) {
+                               var ml = (IMemoryLocation) Expr;
+                               ml.AddressOf (ec, AddressOp.Load);
+                               type = TypeManager.GetReferenceType (type);
+                       } else {
+                               Expr.Emit (ec);
+                       }
+
+                       variable = new LocalTemporary (type);
                        variable.Store (ec);
 
                        Expr = variable;
index a9faebde73a0cbe1b5fb4533523a3e85db4259d1..a1d3dc098ea8804a0e67a6f65d516bb5ff0eda80 100644 (file)
@@ -176,27 +176,19 @@ namespace Mono.CSharp {
        ///   Does not happen with a class because a class is a pointer -- so you always
        ///   get the indirection.
        ///
-       ///   The `is_address' stuff is really just a hack. We need to come up with a better
-       ///   way to handle it.
        /// </remarks>
        public class LocalTemporary : Expression, IMemoryLocation, IAssignMethod {
                LocalBuilder builder;
-               bool is_address;
 
-               public LocalTemporary (Type t) : this (t, false) {}
-
-               public LocalTemporary (Type t, bool is_address)
+               public LocalTemporary (Type t)
                {
                        type = t;
                        eclass = ExprClass.Value;
-                       this.is_address = is_address;
                }
 
                public LocalTemporary (LocalBuilder b, Type t)
+                       : this (t)
                {
-                       type = t;
-                       eclass = ExprClass.Value;
-                       loc = Location.Null;
                        builder = b;
                }
 
@@ -231,8 +223,9 @@ namespace Mono.CSharp {
                                throw new InternalErrorException ("Emit without Store, or after Release");
 
                        ig.Emit (OpCodes.Ldloc, builder);
+
                        // we need to copy from the pointer
-                       if (is_address)
+                       if (builder.LocalType.IsByRef)
                                LoadFromPtr (ig, type);
                }
 
@@ -265,14 +258,11 @@ namespace Mono.CSharp {
                        get { return builder; }
                }
 
-               // NB: if you have `is_address' on the stack there must
-               // be a managed pointer. Otherwise, it is the type from
-               // the ctor.
                public void Store (EmitContext ec)
                {
                        ILGenerator ig = ec.ig;
                        if (builder == null)
-                               builder = ec.GetTemporaryLocal (is_address ? TypeManager.GetReferenceType (type): type);
+                               builder = ec.GetTemporaryLocal (type);
 
                        ig.Emit (OpCodes.Stloc, builder);
                }
@@ -280,28 +270,25 @@ namespace Mono.CSharp {
                public void AddressOf (EmitContext ec, AddressOp mode)
                {
                        if (builder == null)
-                               builder = ec.GetTemporaryLocal (is_address ? TypeManager.GetReferenceType (type): type);
+                               builder = ec.GetTemporaryLocal (type);
 
-                       // if is_address, than this is just the address anyways,
-                       // so we just return this.
                        ILGenerator ig = ec.ig;
 
-                       if (is_address)
+                       if (builder.LocalType.IsByRef) {
+                               //
+                               // if is_address, than this is just the address anyways,
+                               // so we just return this.
+                               //
                                ig.Emit (OpCodes.Ldloc, builder);
-                       else
+                       } else {
                                ig.Emit (OpCodes.Ldloca, builder);
+                       }
                }
 
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
                        type = storey.MutateType (type);
                }
-
-               public bool PointsToAddress {
-                       get {
-                               return is_address;
-                       }
-               }
        }
 
        /// <summary>
index 776fa34d7145d3b8068c93c3eeecaeac2c546c55..c00c43b4bb259a77d63daf42ebbcdc3e3e65c3a7 100644 (file)
@@ -874,16 +874,30 @@ named_attribute_argument
        ;
        
 named_argument
-       : IDENTIFIER COLON expression
+       : IDENTIFIER COLON opt_named_modifier expression
          {
                if (RootContext.Version <= LanguageVersion.V_3)
                        Report.FeatureIsNotAvailable (GetLocation ($1), "named argument");
                        
+               // Avoid boxing in common case (no modifier)
+               var arg_mod = $3 == null ? Argument.AType.None : (Argument.AType) $3;
+                       
                var lt = (Tokenizer.LocatedToken) $1;
-               $$ = new NamedArgument (lt.Value, lt.Location, (Expression) $3);
-         }       
-       ;       
-
+               $$ = new NamedArgument (lt.Value, lt.Location, (Expression) $4, arg_mod);
+         }
+       ;
+       
+opt_named_modifier
+       : /* empty */   { $$ = null; }
+       | REF
+         { 
+               $$ = Argument.AType.Ref;
+         }
+       | OUT
+         { 
+               $$ = Argument.AType.Out;
+         }
+       ;
                  
 class_body
        :  OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
index 28da4b7ea991c0941a653538a7127cc02cc683b1..8b4f747cc70ea2c4ba300f20571e9d6a07d7a212 100644 (file)
@@ -4353,7 +4353,7 @@ namespace Mono.CSharp {
                public abstract VariableInfo VariableInfo { get; }
                #endregion
 
-               public void AddressOf (EmitContext ec, AddressOp mode)
+               public virtual void AddressOf (EmitContext ec, AddressOp mode)
                {
                        HoistedVariable hv = GetHoistedVariable (ec);
                        if (hv != null) {
@@ -4816,6 +4816,19 @@ namespace Mono.CSharp {
 
                        return Name == pr.Name;
                }
+
+               public override void AddressOf (EmitContext ec, AddressOp mode)
+               {
+                       //
+                       // ParameterReferences might already be a reference
+                       //
+                       if (IsRef) {
+                               EmitLoad (ec);
+                               return;
+                       }
+
+                       base.AddressOf (ec, mode);
+               }
                
                protected override void CloneTo (CloneContext clonectx, Expression target)
                {