2001-11-18 Ravi Pratap <ravi@ximian.com>
authorRavi Pratap M <ravi@mono-cvs.ximian.com>
Sun, 18 Nov 2001 10:17:16 +0000 (10:17 -0000)
committerRavi Pratap M <ravi@mono-cvs.ximian.com>
Sun, 18 Nov 2001 10:17:16 +0000 (10:17 -0000)
* expression.cs (ArrayCreation::ValidateInitializers): Update to perform
some type checking etc.

2001-11-17  Ravi Pratap  <ravi@ximian.com>

* expression.cs (ArrayCreation::ValidateInitializers): Implement
bits to provide dimension info is user skips doing that.

Update second constructor to store the rank correctly.

2001-11-16  Ravi Pratap  <ravi@ximian.com>

* expression.cs (ArrayCreation::ValidateInitializers): Poke around
and try to implement.

* ../errors/cs0150.cs : Add.

* ../errors/cs0178.cs : Add.

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

mcs/mcs/ChangeLog
mcs/mcs/enum.cs
mcs/mcs/expression.cs

index 8aa7f30ccb3fbe5bc77a337a0db1343534549076..13911a4314231d56146f9e964b5d2029333d35ad 100755 (executable)
@@ -1,3 +1,24 @@
+2001-11-18  Ravi Pratap  <ravi@ximian.com>
+
+       * expression.cs (ArrayCreation::ValidateInitializers): Update to perform
+       some type checking etc.
+
+2001-11-17  Ravi Pratap  <ravi@ximian.com>
+
+       * expression.cs (ArrayCreation::ValidateInitializers): Implement
+       bits to provide dimension info is user skips doing that.
+
+       Update second constructor to store the rank correctly.
+
+2001-11-16  Ravi Pratap  <ravi@ximian.com>
+
+       * expression.cs (ArrayCreation::ValidateInitializers): Poke around
+       and try to implement.
+
+       * ../errors/cs0150.cs : Add.
+
+       * ../errors/cs0178.cs : Add.
+
 2001-11-16  Miguel de Icaza  <miguel@ximian.com>
 
        * statement.cs: Implement foreach on multi-dimensional arrays. 
index bf7a3b1de3444c1dfabe54ff4c003eeff60df881..5d7f2509619e61bc28690bbec4c5181e7126d3a4 100755 (executable)
@@ -2,6 +2,7 @@
 // enum.cs: Enum handling.
 //
 // Author: Miguel de Icaza (miguel@gnu.org)
+//         Ravi Pratap     (ravi@ximian.com)
 //
 // Licensed under the terms of the GNU GPL
 //
index b59a31da9992a295bc2da66d8b55848d0358e556..5cf7fdb86541dfc754c26b3f3b46d4565786a7e5 100755 (executable)
@@ -2891,6 +2891,8 @@ namespace CIR {
                
                bool IsBuiltinType = false;
 
+               int dimensions = 0;
+
                public ArrayCreation (string requested_type, ArrayList exprs,
                                      string rank, ArrayList initializers, Location l)
                {
@@ -2909,9 +2911,14 @@ namespace CIR {
                public ArrayCreation (string requested_type, string rank, ArrayList initializers, Location l)
                {
                        RequestedType = requested_type;
-                       Rank = rank;
                        Initializers = initializers;
                        loc = l;
+
+                       Rank = rank.Substring (0, rank.LastIndexOf ("["));
+
+                       string tmp = rank.Substring (rank.LastIndexOf ("["));
+
+                       dimensions = tmp.Length - 1;
                }
 
                public static string FormArrayType (string base_type, int idx_count, string rank)
@@ -2943,12 +2950,111 @@ namespace CIR {
 
                        return val.Substring (0, val.LastIndexOf ("["));
                }
+
+               void error178 ()
+               {
+                       Report.Error (178, loc, "Incorrectly structured array initializer");
+               }
+
+               bool ValidateInitializers (EmitContext ec)
+               {
+                       if (Initializers == null)
+                               return true;
+
+                       Type underlying_type = ec.TypeContainer.LookupType (RequestedType, false);
+
+                       ArrayList probe = Initializers;
+
+                       if (Arguments != null) {
+                               for (int i = 0; i < Arguments.Count; i++) {
+                                       Argument a = (Argument) Arguments [i];
+                                       
+                                       Expression e = Expression.Reduce (ec, a.Expr);
+                                       
+                                       if (!(e is Literal)) {
+                                               Report.Error (150, loc, "A constant value is expected");
+                                               return false;
+                                       }
+                                       
+                                       int value = (int) ((Literal) e).GetValue ();
                
+                                       if (probe == null) {
+                                               error178 ();
+                                               return false;
+                                       }
+                                               
+                                       if (value != probe.Count) {
+                                               error178 ();
+                                               return false;
+                                       }
+
+                                       if (probe [0] is ArrayList)
+                                               probe = (ArrayList) probe [0];
+                                       else {
+                                               for (int j = 0; j < probe.Count; ++j) {
+                                                       Expression tmp = (Expression) probe [j];
+                                                       
+                                                       tmp = tmp.Resolve (ec);
+
+                                                       Expression conv = ConvertImplicitRequired (ec, tmp,
+                                                                                                  underlying_type, loc);
+
+                                                       if (conv == null)
+                                                               return false;
+                                               }
+
+                                               probe = null;
+                                       }
+                               }
+
+                       } else {
+                               //
+                               // Here is where we update dimension info in the case
+                               // that the user skips doing that
+                               //
+
+                               Arguments = new ArrayList ();
+                               
+                               for (probe = Initializers; probe != null; ) {
+                                       Expression e = new IntLiteral (probe.Count);
+
+                                       Arguments.Add (new Argument (e, Argument.AType.Expression));
+
+                                       if (probe [0] is ArrayList)
+                                               probe = (ArrayList) probe [0];
+                                       else {
+                                               for (int j = 0; j < probe.Count; ++j) {
+                                                       Expression tmp = (Expression) probe [j];
+                                                       
+                                                       tmp = tmp.Resolve (ec);
+
+                                                       Expression conv = ConvertImplicitRequired (ec, tmp,
+                                                                                                  underlying_type, loc);
+
+                                                       if (conv == null)
+                                                               return false;
+                                               }
+                                               
+                                               probe = null;
+                                       }
+                               }
+
+                               if (Arguments.Count != dimensions) {
+                                       error178 ();
+                                       return false;
+                               }
+                       }
 
+                       return true;
+               }
+               
                public override Expression DoResolve (EmitContext ec)
                {
                        int arg_count;
-                       
+
+                       if (!ValidateInitializers (ec))
+                               return null;
+
                        if (Arguments == null)
                                arg_count = 0;
                        else