2008-01-29 Jb Evain <jbevain@novell.com>
authorJb Evain <jbevain@gmail.com>
Mon, 28 Jan 2008 23:02:26 +0000 (23:02 -0000)
committerJb Evain <jbevain@gmail.com>
Mon, 28 Jan 2008 23:02:26 +0000 (23:02 -0000)
* Expression.cs, NewExpression.cs, ExpressionPrinter.cs:
implement the first flavors of New.

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

mcs/class/System.Core/System.Linq.Expressions/ChangeLog
mcs/class/System.Core/System.Linq.Expressions/Expression.cs
mcs/class/System.Core/System.Linq.Expressions/ExpressionPrinter.cs
mcs/class/System.Core/System.Linq.Expressions/NewExpression.cs

index 002f67f65bcf2a6228ed029bc7c7bbaa5ecdf87a..5f89ee4cf6e5cd880c78307391f08cb8117ee49f 100644 (file)
@@ -1,3 +1,8 @@
+2008-01-29  Jb Evain  <jbevain@novell.com>
+
+       * Expression.cs, NewExpression.cs, ExpressionPrinter.cs:
+       implement the first flavors of New.
+
 2008-01-27  Jb Evain  <jbevain@novell.com>
 
        * ConditionalExpression.cs: implement Emit.
index 5fa2a174bf668384320c4ed6af145f4ae9704a8a..cc165a218c3e036e80681643f7f311d39440272f 100644 (file)
@@ -1346,28 +1346,53 @@ namespace System.Linq.Expressions {
                        return MakeSimpleUnary (ExpressionType.Negate, expression, method);
                }
 
-               [MonoTODO]
                public static NewExpression New (ConstructorInfo constructor)
                {
-                       throw new NotImplementedException ();
+                       if (constructor == null)
+                               throw new ArgumentNullException ("constructor");
+
+                       if (constructor.GetParameters ().Length > 0)
+                               throw new ArgumentException ("Constructor must be parameter less");
+
+                       return new NewExpression (constructor, (null as IEnumerable<Expression>).ToReadOnlyCollection<Expression> (), null);
                }
 
-               [MonoTODO]
                public static NewExpression New (Type type)
                {
-                       throw new NotImplementedException ();
+                       if (type == null)
+                               throw new ArgumentNullException ("type");
+
+                       if (type.GetConstructor (Type.EmptyTypes) == null)
+                               throw new ArgumentException ("Type doesn't have a parameter less constructor");
+
+                       return new NewExpression (type, (null as IEnumerable<Expression>).ToReadOnlyCollection<Expression> ());
                }
 
-               [MonoTODO]
                public static NewExpression New (ConstructorInfo constructor, params Expression [] arguments)
                {
-                       throw new NotImplementedException ();
+                       return New (constructor, arguments as IEnumerable<Expression>);
                }
 
-               [MonoTODO]
                public static NewExpression New (ConstructorInfo constructor, IEnumerable<Expression> arguments)
                {
-                       throw new NotImplementedException ();
+                       if (constructor == null)
+                               throw new ArgumentNullException ("constructor");
+
+                       var args = arguments.ToReadOnlyCollection ();
+                       var parameters = constructor.GetParameters ();
+
+                       if (args.Count != parameters.Length)
+                               throw new ArgumentException ("arguments");
+
+                       for (int i = 0; i < parameters.Length; i++) {
+                               if (args [i] == null)
+                                       throw new ArgumentNullException ("arguments");
+
+                               if (!parameters [i].ParameterType.IsAssignableFrom (args [i].Type))
+                                       throw new ArgumentException ("arguments");
+                       }
+
+                       return new NewExpression (constructor, args, null);
                }
 
                [MonoTODO]
index 26f56ee6a9e3d0b29e57c524a30e1dbd0cc8478f..52c58fa2f271496c26a61fa6ba0da2a41819b6e7 100644 (file)
@@ -311,7 +311,9 @@ namespace System.Linq.Expressions {
 
                protected override void VisitNew (NewExpression nex)
                {
-                       throw new NotImplementedException ();
+                       Print ("new {0}(", nex.Type.Name);
+                       VisitExpressionList (nex.Arguments);
+                       Print (")");
                }
 
                protected override void VisitMemberInit (MemberInitExpression init)
index 6d5581786b805ec39d10c3147e23c883529f7e66..787117de2c236aa785cd4450d6cff542375b184e 100644 (file)
@@ -49,8 +49,14 @@ namespace System.Linq.Expressions {
                public ReadOnlyCollection<MemberInfo> Members {
                        get { return members; }
                }
-               
-               internal NewExpression (ConstructorInfo constructor, ReadOnlyCollection<Expression> arguments,  ReadOnlyCollection<MemberInfo> members)
+
+               internal NewExpression (Type type, ReadOnlyCollection<Expression> arguments)
+                       : base (ExpressionType.New, type)
+               {
+                       this.arguments = arguments;
+               }
+
+               internal NewExpression (ConstructorInfo constructor, ReadOnlyCollection<Expression> arguments, ReadOnlyCollection<MemberInfo> members)
                        : base (ExpressionType.New, constructor.DeclaringType)
                {
                        this.constructor = constructor;