diff dmd/ArrayInitializer.d @ 156:0c8cc2a10f99

+ ArrayInitializer.toAssocArrayLiteral() + FuncDeclaration.isUnique() + TypeEnum.alignsize() + TypeEnum.toChars() + TypeEnum.getProperty() + VarExp.equals() + expression.Util.fpunique() * fixed missing default in TypeBasic
author trass3r
date Wed, 15 Sep 2010 18:24:39 +0200
parents 60bb0fe4563e
children e3afd1303184
line wrap: on
line diff
--- a/dmd/ArrayInitializer.d	Wed Sep 15 17:31:22 2010 +0200
+++ b/dmd/ArrayInitializer.d	Wed Sep 15 18:24:39 2010 +0200
@@ -15,6 +15,7 @@
 import dmd.IntegerExp;
 import dmd.Expression;
 import dmd.ArrayLiteralExp;
+import dmd.AssocArrayLiteralExp;
 import dmd.Scope;
 import dmd.ErrorExp;
 import dmd.OutBuffer;
@@ -270,13 +271,46 @@
 	Lno:
 	    return null;
 	}
-	
-    Expression toAssocArrayLiteral()
+
+	/********************************
+	 * If possible, convert array initializer to associative array initializer.
+	 */
+	Expression toAssocArrayLiteral()
 	{
-		assert(false);
+		Expression e;
+
+		// writef("ArrayInitializer::toAssocArrayInitializer()\n");
+		// static int i; if (++i == 2) halt();
+		Expressions keys = new Expressions();
+		keys.setDim(value.dim);
+		Expressions values = new Expressions();
+		values.setDim(value.dim);
+
+		foreach (size_t i, Initializer iz; value)
+		{
+			e = index[i];
+			if (!e)
+				goto Lno;
+			keys[i] = e;
+
+			if (!iz)
+				goto Lno;
+			e = iz.toExpression();
+			if (!e)
+				goto Lno;
+			values[i] = e;
+		}
+		e = new AssocArrayLiteralExp(loc, keys, values);
+		return e;
+
+	Lno:
+		delete keys;
+		delete values;
+		error(loc, "not an associative array initializer");
+		return new ErrorExp();
 	}
 	
-    override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
+	override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
 	{
 		assert(false);
 	}