Mercurial > projects > ddmd
diff dmd/TypeStruct.d @ 137:09c858522d55
merge
author | Trass3r |
---|---|
date | Mon, 13 Sep 2010 23:29:00 +0200 |
parents | af1bebfd96a4 |
children | 14feb7ae01a6 |
line wrap: on
line diff
--- a/dmd/TypeStruct.d Mon Sep 13 23:27:38 2010 +0200 +++ b/dmd/TypeStruct.d Mon Sep 13 23:29:00 2010 +0200 @@ -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; }