Mercurial > projects > ddmd
diff dmd/StructInitializer.d @ 2:7427ded8caf7
Removed unreferenced modules
First step at fixing GC issues - now calling GC.malloc instead of malloc (ditto calloc and realloc), get rid of free
author | korDen |
---|---|
date | Sun, 25 Oct 2009 03:20:59 +0300 |
parents | 10317f0c89a5 |
children | b7d29f613539 |
line wrap: on
line diff
--- a/dmd/StructInitializer.d Sat Oct 24 17:25:59 2009 +0400 +++ b/dmd/StructInitializer.d Sun Oct 25 03:20:59 2009 +0300 @@ -1,16 +1,28 @@ module dmd.StructInitializer; import dmd.Initializer; +import dmd.TOK; +import dmd.FuncLiteralDeclaration; +import dmd.TypeFunction; import dmd.ArrayTypes; import dmd.Array; import dmd.Loc; import dmd.Type; import dmd.Scope; import dmd.Identifier; +import dmd.CompoundStatement; import dmd.AggregateDeclaration; import dmd.OutBuffer; import dmd.HdrGenState; import dmd.Expression; +import dmd.TypeStruct; +import dmd.TY; +import dmd.VarDeclaration; +import dmd.Dsymbol; +import dmd.Util; +import dmd.ExpInitializer; +import dmd.FuncExp; +import dmd.LINK; import dmd.backend.dt_t; @@ -24,23 +36,136 @@ this(Loc loc) { - assert(false); super(loc); + ad = null; + + field = new Identifiers(); + value = new Initializers(); } Initializer syntaxCopy() { - assert(false); + StructInitializer ai = new StructInitializer(loc); + + assert(field.dim == value.dim); + ai.field.setDim(field.dim); + ai.value.setDim(value.dim); + for (int i = 0; i < field.dim; i++) + { + ai.field.data[i] = field.data[i]; + + Initializer init = cast(Initializer)value.data[i]; + init = init.syntaxCopy(); + ai.value.data[i] = cast(void*)init; + } + + return ai; } void addInit(Identifier field, Initializer value) { - assert(false); + //printf("StructInitializer.addInit(field = %p, value = %p)\n", field, value); + this.field.push(cast(void*)field); + this.value.push(cast(void*)value); } Initializer semantic(Scope sc, Type t) { - assert(false); + TypeStruct ts; + int errors = 0; + + //printf("StructInitializer.semantic(t = %s) %s\n", t.toChars(), toChars()); + vars.setDim(field.dim); + t = t.toBasetype(); + if (t.ty == Tstruct) + { + uint i; + uint fieldi = 0; + + ts = cast(TypeStruct)t; + ad = ts.sym; + for (i = 0; i < field.dim; i++) + { + Identifier id = cast(Identifier)field.data[i]; + Initializer val = cast(Initializer)value.data[i]; + Dsymbol s; + VarDeclaration v; + + if (id is null) + { + if (fieldi >= ad.fields.dim) + { + error(loc, "too many initializers for %s", ad.toChars()); + field.remove(i); + i--; + continue; + } + else + { + s = cast(Dsymbol)ad.fields.data[fieldi]; + } + } + else + { + //s = ad.symtab.lookup(id); + s = ad.search(loc, id, 0); + if (!s) + { + error(loc, "'%s' is not a member of '%s'", id.toChars(), t.toChars()); + continue; + } + + // Find out which field index it is + for (fieldi = 0; 1; fieldi++) + { + if (fieldi >= ad.fields.dim) + { + s.error("is not a per-instance initializable field"); + break; + } + if (s == cast(Dsymbol)ad.fields.data[fieldi]) + break; + } + } + if (s && (v = s.isVarDeclaration()) !is null) + { + val = val.semantic(sc, v.type); + value.data[i] = cast(void*)val; + vars.data[i] = cast(void*)v; + } + else + { + error(loc, "%s is not a field of %s", id ? id.toChars() : s.toChars(), ad.toChars()); + errors = 1; + } + fieldi++; + } + } + else if (t.ty == Tdelegate && value.dim == 0) + { + /* Rewrite as empty delegate literal { } + */ + Arguments arguments = new Arguments; + Type tf = new TypeFunction(arguments, null, 0, LINK.LINKd); + FuncLiteralDeclaration fd = new FuncLiteralDeclaration(loc, Loc(0), tf, TOK.TOKdelegate, null); + fd.fbody = new CompoundStatement(loc, new Statements()); + fd.endloc = loc; + Expression e = new FuncExp(loc, fd); + ExpInitializer ie = new ExpInitializer(loc, e); + return ie.semantic(sc, t); + } + else + { + error(loc, "a struct is not a valid initializer for a %s", t.toChars()); + errors = 1; + } + if (errors) + { + field.setDim(0); + value.setDim(0); + vars.setDim(0); + } + return this; } Expression toExpression()