diff --git a/dmd/attrib.c b/dmd/attrib.c index d3148bfc..4c7a9bbd 100644 --- a/dmd/attrib.c +++ b/dmd/attrib.c @@ -501,9 +501,6 @@ AlignDeclaration::AlignDeclaration(Loc loc, unsigned sa, Array *decl) { this->loc = loc; salign = sa; - - if (global.params.warnings && salign != 1) - warning("%s: align(%d) is not implemented and specified to be unportable anyway, use align(1) and manual fillers instead", loc.toChars(), salign); } Dsymbol *AlignDeclaration::syntaxCopy(Dsymbol *s) @@ -517,21 +514,33 @@ Dsymbol *AlignDeclaration::syntaxCopy(Dsymbol *s) void AlignDeclaration::semantic(Scope *sc) { +// LDC +// we only support packed structs, as from the spec: align(1) struct Packed { ... } +// other alignments are simply ignored. my tests show this is what llvm-gcc does too ... + //printf("\tAlignDeclaration::semantic '%s'\n",toChars()); if (decl) { unsigned salign_save = sc->structalign; - sc->structalign = salign; for (unsigned i = 0; i < decl->dim; i++) { Dsymbol *s = (Dsymbol *)decl->data[i]; - s->semantic(sc); + if (s->isStructDeclaration() && salign == 1) + { + sc->structalign = salign; + s->semantic(sc); + sc->structalign = salign_save; + } + else + { + s->semantic(sc); + } } sc->structalign = salign_save; } else - sc->structalign = salign; + assert(0 && "what kind of align use triggers this?"); } @@ -658,12 +667,17 @@ void AnonDeclaration::semantic(Scope *sc) //printf("sc->offset = %d\n", sc->offset); // Add members of aad to ad - //printf("\tadding members of aad to '%s'\n", ad->toChars()); + //printf("\tadding members of aad (%p) to '%s'\n", &aad, ad->toChars()); for (unsigned i = 0; i < aad.fields.dim; i++) { VarDeclaration *v = (VarDeclaration *)aad.fields.data[i]; v->offset += sc->offset; + + // LDC + if (!v->anonDecl) + v->anonDecl = this; + ad->fields.push(v); } diff --git a/dmd/declaration.c b/dmd/declaration.c index b0cecad5..39af81da 100644 --- a/dmd/declaration.c +++ b/dmd/declaration.c @@ -621,6 +621,9 @@ VarDeclaration::VarDeclaration(Loc loc, Type *type, Identifier *id, Initializer onstack = 0; canassign = 0; value = NULL; + + // LDC + anonDecl = NULL; } Dsymbol *VarDeclaration::syntaxCopy(Dsymbol *s) diff --git a/dmd/declaration.h b/dmd/declaration.h index fbd0186d..7fed9e9a 100644 --- a/dmd/declaration.h +++ b/dmd/declaration.h @@ -37,6 +37,7 @@ struct StructDeclaration; struct TupleType; struct InterState; struct IRState; +struct AnonDeclaration; enum PROT; enum LINK; @@ -269,6 +270,9 @@ struct VarDeclaration : Declaration // Eliminate need for dynamic_cast VarDeclaration *isVarDeclaration() { return (VarDeclaration *)this; } + + // LDC + AnonDeclaration* anonDecl; }; /**************************************************************/ diff --git a/dmd/parse.c b/dmd/parse.c index f2f7f0e7..d2a82a45 100644 --- a/dmd/parse.c +++ b/dmd/parse.c @@ -349,6 +349,9 @@ Array *Parser::parseDeclDefs(int once) case TOKalign: { unsigned n; + // LDC better align code locations + Loc alignloc = loc; + s = NULL; nextToken(); if (token.value == TOKlparen) @@ -367,7 +370,7 @@ Array *Parser::parseDeclDefs(int once) n = global.structalign; // default a = parseBlock(); - s = new AlignDeclaration(loc, n, a); + s = new AlignDeclaration(alignloc, n, a); break; } diff --git a/dmd/scope.h b/dmd/scope.h index dc4350b3..ba568654 100644 --- a/dmd/scope.h +++ b/dmd/scope.h @@ -30,6 +30,7 @@ struct AnonymousAggregateDeclaration; struct FuncDeclaration; struct DocComment; struct EnclosingHandler; +struct AnonDeclaration; enum LINK; enum PROT; diff --git a/gen/classes.cpp b/gen/classes.cpp index 725aacff..79f433a1 100644 --- a/gen/classes.cpp +++ b/gen/classes.cpp @@ -57,7 +57,7 @@ static void LLVM_AddBaseClassInterfaces(ClassDeclaration* target, BaseClasses* b ////////////////////////////////////////////////////////////////////////////////////////// -static void LLVM_AddBaseClassData(BaseClasses* bcs) +static void LLVM_AddBaseClassData(IrStruct* irstruct, BaseClasses* bcs) { // add base class data members first for (int j=0; jdim; j++) @@ -69,7 +69,7 @@ static void LLVM_AddBaseClassData(BaseClasses* bcs) continue; // recursively add baseclass data - LLVM_AddBaseClassData(&bc->base->baseclasses); + LLVM_AddBaseClassData(irstruct, &bc->base->baseclasses); Array* arr = &bc->base->fields; if (arr->dim == 0) @@ -80,7 +80,9 @@ static void LLVM_AddBaseClassData(BaseClasses* bcs) for (int k=0; k < arr->dim; k++) { VarDeclaration* v = (VarDeclaration*)(arr->data[k]); - v->toObjFile(0); // TODO: multiobj + Logger::println("Adding field: %s %s", v->type->toChars(), v->toChars()); + // init fields, used to happen in VarDeclaration::toObjFile + irstruct->addField(v); } } } @@ -143,9 +145,19 @@ void DtoResolveClass(ClassDeclaration* cd) fieldtypes.push_back(getVoidPtrType()); // add base class data fields first - LLVM_AddBaseClassData(&cd->baseclasses); + LLVM_AddBaseClassData(irstruct, &cd->baseclasses); - // then add own members, if any + // add own fields + Array* fields = &cd->fields; + for (int k=0; k < fields->dim; k++) + { + VarDeclaration* v = (VarDeclaration*)fields->data[k]; + Logger::println("Adding field: %s %s", v->type->toChars(), v->toChars()); + // init fields, used to happen in VarDeclaration::toObjFile + irstruct->addField(v); + } + + // then add other members of us, if any if(cd->members) { for (int k=0; k < cd->members->dim; k++) { Dsymbol* dsym = (Dsymbol*)(cd->members->data[k]); diff --git a/gen/structs.cpp b/gen/structs.cpp index 03f58573..85c9f8dd 100644 --- a/gen/structs.cpp +++ b/gen/structs.cpp @@ -92,7 +92,7 @@ void DtoResolveStruct(StructDeclaration* sd) TypeStruct* ts = (TypeStruct*)sd->type->toBasetype(); // this struct is a forward declaration - // didn't even know had those ... + // didn't even know D had those ... if (sd->sizeok != 1) { sd->ir.irStruct = new IrStruct(ts); @@ -102,10 +102,21 @@ void DtoResolveStruct(StructDeclaration* sd) bool ispacked = (ts->alignsize() == 1); + // create the IrStruct IrStruct* irstruct = new IrStruct(ts); sd->ir.irStruct = irstruct; gIR->structs.push_back(irstruct); + // add fields + Array* fields = &sd->fields; + for (int k=0; k < fields->dim; k++) + { + VarDeclaration* v = (VarDeclaration*)fields->data[k]; + Logger::println("Adding field: %s %s", v->type->toChars(), v->toChars()); + // init fields, used to happen in VarDeclaration::toObjFile + irstruct->addField(v); + } + irstruct->packed = ispacked; bool thisModule = false; diff --git a/gen/toobj.cpp b/gen/toobj.cpp index c27b79fb..38ce4cd5 100644 --- a/gen/toobj.cpp +++ b/gen/toobj.cpp @@ -991,19 +991,10 @@ void VarDeclaration::toObjFile(int multiobj) else gIR->constInitList.push_back(this); } - - // inside aggregate declaration. declare a field. else { - Logger::println("Aggregate var declaration: '%s' offset=%d", toChars(), offset); - - const LLType* _type = DtoType(type); - this->ir.irField = new IrField(this); - - // add the field in the IRStruct - gIR->topstruct()->offsets.insert(std::make_pair(offset, IrStruct::Offset(this, _type))); + assert(ir.irField != 0); } - Logger::println("VarDeclaration::toObjFile is done"); } diff --git a/ir/irstruct.cpp b/ir/irstruct.cpp index e822059e..6472744f 100644 --- a/ir/irstruct.cpp +++ b/ir/irstruct.cpp @@ -1,8 +1,12 @@ #include "gen/llvm.h" + #include "mtype.h" #include "aggregate.h" +#include "declaration.h" + #include "ir/irstruct.h" #include "gen/irstate.h" +#include "gen/tollvm.h" IrInterface::IrInterface(BaseClass* b) { @@ -56,3 +60,15 @@ IrStruct::IrStruct(Type* t) IrStruct::~IrStruct() { } + +void IrStruct::addField(VarDeclaration* v) +{ + // might already have its irField, as classes derive each other without getting copies of the VarDeclaration + if (!v->ir.irField) + { + assert(!v->ir.isSet()); + v->ir.irField = new IrField(v); + } + const LLType* _type = DtoType(v->type); + offsets.insert(std::make_pair(v->offset, IrStruct::Offset(v, _type))); +} diff --git a/ir/irstruct.h b/ir/irstruct.h index 8d47f4d1..db6b3354 100644 --- a/ir/irstruct.h +++ b/ir/irstruct.h @@ -53,6 +53,8 @@ public: IrStruct(Type*); virtual ~IrStruct(); + void addField(VarDeclaration* v); + Type* type; llvm::PATypeHolder recty; OffsetMap offsets;