diff dmd/TypeStruct.d @ 135:af1bebfd96a4 dmd2037

dmd 2.038
author Eldar Insafutdinov <e.insafutdinov@gmail.com>
date Mon, 13 Sep 2010 22:19:42 +0100
parents 60bb0fe4563e
children 14feb7ae01a6
line wrap: on
line diff
--- a/dmd/TypeStruct.d	Sat Sep 11 13:03:39 2010 +0100
+++ b/dmd/TypeStruct.d	Mon Sep 13 22:19:42 2010 +0100
@@ -37,6 +37,7 @@
 import dmd.VarExp;
 import dmd.CommaExp;
 import dmd.ThisExp;
+import dmd.StructLiteralExp;
 import dmd.SymbolDeclaration;
 import dmd.TypeInfoDeclaration;
 import dmd.TypeInfoStructDeclaration;
@@ -377,18 +378,44 @@
 	
     override Expression defaultInit(Loc loc)
 	{
-		Symbol* s;
-		Declaration d;
-
 	version (LOGDEFAULTINIT) {
 		printf("TypeStruct::defaultInit() '%s'\n", toChars());
 	}
-		s = sym.toInitializer();
-		d = new SymbolDeclaration(sym.loc, s, sym);
+		Symbol *s = sym.toInitializer();
+		Declaration d = new SymbolDeclaration(sym.loc, s, sym);
 		assert(d);
 		d.type = this;
 		return new VarExp(sym.loc, d);
 	}
+    
+    /***************************************
+     * Use when we prefer the default initializer to be a literal,
+     * rather than a global immutable variable.
+     */
+    Expression defaultInitLiteral(Loc loc)
+    {
+	version (LOGDEFAULTINIT) {
+        printf("TypeStruct::defaultInitLiteral() '%s'\n", toChars());
+    }
+        auto structelems = new Expressions();
+        structelems.setDim(sym.fields.dim);
+        for (size_t j = 0; j < structelems.dim; j++)
+        {
+	        auto vd = cast(VarDeclaration)(sym.fields[j]);
+	        Expression e;
+	        if (vd.init)
+	            e = vd.init.toExpression();
+	        else
+	            e = vd.type.defaultInitLiteral();
+	        structelems[j] = e;
+        }
+        auto structinit = new StructLiteralExp(loc, cast(StructDeclaration)sym, structelems);
+        // Why doesn't the StructLiteralExp constructor do this, when
+        // sym->type != NULL ?
+        structinit.type = sym.type;
+        return structinit;
+    }
+
 	
     override bool isZeroInit(Loc loc)
 	{
@@ -403,7 +430,7 @@
 		for (size_t i = 0; i < sym.fields.dim; i++)
 		{   
 			VarDeclaration v = cast(VarDeclaration)sym.fields[i];
-			if (v.isConst() || v.isInvariant())
+			if (v.isConst() || v.isImmutable())
 				return false;
 		}
 		return true;
@@ -480,6 +507,7 @@
 	
     override bool hasPointers()
 	{
+        // Probably should cache this information in sym rather than recompute
 		StructDeclaration s = sym;
 
 		sym.size(Loc(0));		// give error for forward references
@@ -503,7 +531,7 @@
 			m = MATCHexact;		// exact match
 			if (mod != to.mod)
 			{
-				if (to.mod == MODconst)
+				if (MODimplicitConv(mod, to.mod))
 					m = MATCHconst;
 				else
 				{	/* Check all the fields. If they can all be converted,
@@ -547,7 +575,8 @@
 	{
 		if (equals(to))
 			return MATCHexact;
-		if (ty == to.ty && sym == (cast(TypeStruct)to).sym && to.mod == MODconst)
+		if (ty == to.ty && sym == (cast(TypeStruct)to).sym &&
+            MODimplicitConv(mod, to.mod))
 			return MATCHconst;
 		return MATCHnomatch;
 	}