diff dmd/StructLiteralExp.d @ 63:cab4c37afb89

A bunch of implementations
author korDen
date Mon, 23 Aug 2010 16:52:24 +0400
parents 51605de93870
children 4290d870944a
line wrap: on
line diff
--- a/dmd/StructLiteralExp.d	Mon Aug 23 03:21:32 2010 +0400
+++ b/dmd/StructLiteralExp.d	Mon Aug 23 16:52:24 2010 +0400
@@ -1,11 +1,16 @@
 module dmd.StructLiteralExp;
 
 import dmd.Expression;
+import dmd.expression.Util;
+import dmd.ErrorExp;
+import dmd.Dsymbol;
+import dmd.VarDeclaration;
 import dmd.StructDeclaration;
 import dmd.backend.elem;
 import dmd.InterState;
 import dmd.MATCH;
 import dmd.WANT;
+import dmd.TY;
 import dmd.Type;
 import dmd.OutBuffer;
 import dmd.Loc;
@@ -48,7 +53,95 @@
 
 	Expression semantic(Scope sc)
 	{
-		assert(false);
+		Expression e;
+		int nfields = sd.fields.dim - sd.isnested;
+
+version (LOGSEMANTIC) {
+		printf("StructLiteralExp.semantic('%s')\n", toChars());
+}
+		if (type)
+			return this;
+
+		// Run semantic() on each element
+		for (size_t i = 0; i < elements.dim; i++)
+		{	
+			e = cast(Expression)elements.data[i];
+			if (!e)
+				continue;
+			e = e.semantic(sc);
+			elements.data[i] = cast(void*)e;
+		}
+		expandTuples(elements);
+		size_t offset = 0;
+		for (size_t i = 0; i < elements.dim; i++)
+		{	
+			e = cast(Expression)elements.data[i];
+			if (!e)
+				continue;
+
+			if (!e.type)
+				error("%s has no value", e.toChars());
+			e = resolveProperties(sc, e);
+			if (i >= nfields)
+			{   
+				error("more initializers than fields of %s", sd.toChars());
+				return new ErrorExp();
+			}
+			Dsymbol s = cast(Dsymbol)sd.fields.data[i];
+			VarDeclaration v = s.isVarDeclaration();
+			assert(v);
+			if (v.offset < offset)
+				error("overlapping initialization for %s", v.toChars());
+			offset = v.offset + cast(uint)v.type.size();
+
+			Type telem = v.type;
+			while (!e.implicitConvTo(telem) && telem.toBasetype().ty == Tsarray)
+			{   
+				/* Static array initialization, as in:
+				 *	T[3][5] = e;
+				 */
+				telem = telem.toBasetype().nextOf();
+			}
+
+			e = e.implicitCastTo(sc, telem);
+
+			elements.data[i] = cast(void*)e;
+		}
+
+		/* Fill out remainder of elements[] with default initializers for fields[]
+		 */
+		for (size_t i = elements.dim; i < nfields; i++)
+		{	
+			Dsymbol s = cast(Dsymbol)sd.fields.data[i];
+			VarDeclaration v = s.isVarDeclaration();
+			assert(v);
+			assert(!v.isThisDeclaration());
+
+			if (v.offset < offset)
+			{   
+				e = null;
+				sd.hasUnions = 1;
+			}
+			else
+			{
+				if (v.init)
+				{   
+					e = v.init.toExpression();
+					if (!e)
+						error("cannot make expression out of initializer for %s", v.toChars());
+				}
+				else
+				{	
+					e = v.type.defaultInit(Loc(0));
+					e.loc = loc;
+				}
+				offset = v.offset + cast(uint)v.type.size();
+			}
+			elements.push(cast(void*)e);
+		}
+
+		type = sd.type;
+		return this;
 	}
 
 	Expression getField(Type type, uint offset)
@@ -102,7 +195,7 @@
 		return this;
 	}
 
-	Expression interpret(InterState* istate)
+	Expression interpret(InterState istate)
 	{
 		assert(false);
 	}