diff dmd/StructLiteralExp.d @ 79:43073c7c7769

updated to 2.035 also implemented a few missing functions still crashes in Import.importAll though
author Trass3r
date Mon, 30 Aug 2010 03:57:51 +0200
parents 2e2a5c3f943a
children be2ab491772e
line wrap: on
line diff
--- a/dmd/StructLiteralExp.d	Sun Aug 29 14:39:08 2010 +0100
+++ b/dmd/StructLiteralExp.d	Mon Aug 30 03:57:51 2010 +0200
@@ -27,6 +27,7 @@
 import dmd.HdrGenState;
 import dmd.backend.dt_t;
 import dmd.InlineScanState;
+import dmd.ArrayLiteralExp;
 import dmd.ArrayTypes;
 import dmd.TOK;
 
@@ -41,7 +42,7 @@
 class StructLiteralExp : Expression
 {
 	StructDeclaration sd;		// which aggregate this is for
-    Expressions elements;	// parallels sd->fields[] with
+	Expressions elements;	// parallels sd.fields[] with
 				// NULL entries for fields to skip
 
     Symbol* sym;		// back end symbol to initialize with literal
@@ -99,7 +100,7 @@
 				error("more initializers than fields of %s", sd.toChars());
 				return new ErrorExp();
 			}
-			Dsymbol s = cast(Dsymbol)sd.fields.data[i];
+			Dsymbol s = sd.fields[i];
 			VarDeclaration v = s.isVarDeclaration();
 			assert(v);
 			if (v.offset < offset)
@@ -124,7 +125,7 @@
 		 */
 		for (size_t i = elements.dim; i < nfields; i++)
 		{	
-			Dsymbol s = cast(Dsymbol)sd.fields.data[i];
+			Dsymbol s = sd.fields[i];
 			VarDeclaration v = s.isVarDeclaration();
 			assert(v);
 			assert(!v.isThisDeclaration());
@@ -158,12 +159,67 @@
 
 	Expression getField(Type type, uint offset)
 	{
-		assert(false);
+		//printf("StructLiteralExp.getField(this = %s, type = %s, offset = %u)\n",
+//		/*toChars()*/"", type.toChars(), offset);
+		Expression e = null;
+		int i = getFieldIndex(type, offset);
+
+		if (i != -1)
+		{
+			//printf("\ti = %d\n", i);
+			assert(i < elements.dim);
+			e = cast(Expression)elements.data[i];
+			if (e)
+			{
+				//writef("e = %s, e.type = %s\n", e.toChars(), e.type.toChars());
+	
+				/* If type is a static array, and e is an initializer for that array,
+				 * then the field initializer should be an array literal of e.
+				 */
+				if (e.type != type && type.ty == Tsarray)
+				{
+					TypeSArray tsa = cast(TypeSArray)type;
+					size_t length = cast(size_t) tsa.dim.toInteger();
+					Expressions z = new Expressions;
+					z.setDim(length);
+					for (int q = 0; q < length; ++q)
+						z.data[q] = cast(void*) e.copy();
+					e = new ArrayLiteralExp(loc, z);
+					e.type = type;
+				}
+				else
+				{
+					e = e.copy();
+					e.type = type;
+				}
+			}
+		}
+		return e;
 	}
 
 	int getFieldIndex(Type type, uint offset)
 	{
-		assert(false);
+		/* Find which field offset is by looking at the field offsets
+		 */
+		if (elements.dim)
+		{
+			foreach (size_t i, Dsymbol s; sd.fields)
+			{
+				VarDeclaration v = s.isVarDeclaration();
+				assert(v);
+	
+				if (offset == v.offset && type.size() == v.type.size())
+				{
+					Expression e = cast(Expression)elements.data[i];
+					if (e)
+					{
+						return i;
+					}
+					break;
+				}
+			}
+		}
+		return -1;
 	}
 
 	override elem* toElem(IRState* irs)
@@ -185,9 +241,8 @@
 			 * can spill over into the fields.
 			 */
 			size_t offset = 0;
-			for (size_t i = 0; i < sd.fields.dim; i++)
+			foreach (Dsymbol s; sd.fields)
 			{
-				Dsymbol s = cast(Dsymbol)sd.fields.data[i];
 				VarDeclaration v = s.isVarDeclaration();
 				assert(v);
 
@@ -209,7 +264,7 @@
 				if (!el)
 					continue;
 
-				Dsymbol s = cast(Dsymbol)sd.fields.data[i];
+				Dsymbol s = sd.fields[i];
 				VarDeclaration v = s.isVarDeclaration();
 				assert(v);
 				assert(!v.isThisDeclaration());
@@ -299,11 +354,12 @@
 			}
 		}
 
-version (DMDV2) {
+version (DMDV2)
+{
 		if (sd.isnested)
 		{	// Initialize the hidden 'this' pointer
 			assert(sd.fields.dim);
-			Dsymbol s = cast(Dsymbol)sd.fields.data[sd.fields.dim - 1];
+			Dsymbol s = sd.fields[sd.fields.dim - 1];
 			ThisDeclaration v = s.isThisDeclaration();
 			assert(v);
 
@@ -335,17 +391,41 @@
 
 	override bool checkSideEffect(int flag)
 	{
-		assert(false);
+		bool f = 0;
+
+		for (size_t i = 0; i < elements.dim; i++)
+		{
+			Expression e = cast(Expression)elements.data[i];
+			if (!e)
+				continue;
+	
+			f |= e.checkSideEffect(2);
+		}
+		if (flag == 0 && f == 0)
+		Expression.checkSideEffect(0);
+		return f;
 	}
 
 	override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
 	{
-		assert(false);
+		buf.writestring(sd.toChars());
+		buf.writeByte('(');
+		argsToCBuffer(buf, elements, hgs);
+		buf.writeByte(')');
 	}
 
 	override void toMangleBuffer(OutBuffer buf)
 	{
-		assert(false);
+		size_t dim = elements ? elements.dim : 0;
+		buf.printf("S%u", dim);
+		for (size_t i = 0; i < dim; i++)
+	    {
+			Expression e = cast(Expression)elements.data[i];
+			if (e)
+				e.toMangleBuffer(buf);
+			else
+				buf.writeByte('v');	// 'v' for void
+	    }
 	}
 
 	override void scanForNestedRef(Scope sc)
@@ -379,20 +459,26 @@
 		assert(false);
 	}
 
+version(DMDV2)
+{
 	override int isLvalue()
 	{
-		assert(false);
+		return 1;
 	}
+}
 
 	override Expression toLvalue(Scope sc, Expression e)
 	{
-		assert(false);
+		return this;
 	}
 
+version(DMDV2)
+{
 	override bool canThrow()
 	{
 		return arrayExpressionCanThrow(elements);
 	}
+}
 
 	override MATCH implicitConvTo(Type t)
 	{