std::vector<llvm::Constant*> IrStruct::createClassDefaultInitializer() { ClassDeclaration* cd = aggrdecl->isClassDeclaration(); assert(cd && "invalid class aggregate"); IF_LOG Logger::println("Building class default initializer %s @ %s", cd->toPrettyChars(), cd->loc.toChars()); LOG_SCOPE; IF_LOG Logger::println("Instance size: %u", cd->structsize); // find the fields that contribute to the default initializer. // these will define the default type. std::vector<llvm::Constant*> constants; constants.reserve(32); // add vtbl constants.push_back(getVtblSymbol()); // add monitor constants.push_back(getNullValue(DtoType(Type::tvoid->pointerTo()))); // we start right after the vtbl and monitor size_t offset = PTRSIZE * 2; size_t field_index = 2; // add data members recursively addBaseClassInits(constants, cd, offset, field_index); // tail padding? if (offset < cd->structsize) add_zeros(constants, cd->structsize - offset); return constants; }
llvm::Constant * IrAggr::createInitializerConstant(const VarInitMap &explicitInitializers) { IF_LOG Logger::println("Creating initializer constant for %s", aggrdecl->toChars()); LOG_SCOPE; llvm::SmallVector<llvm::Constant *, 16> constants; unsigned offset = 0; if (type->ty == Tclass) { // add vtbl constants.push_back(getVtblSymbol()); offset += Target::ptrsize; // add monitor (except for C++ classes) if (!aggrdecl->isClassDeclaration()->isCPPclass()) { constants.push_back(getNullValue(getVoidPtrType())); offset += Target::ptrsize; } } // Add the initializers for the member fields. While we are traversing the // class hierarchy, use the opportunity to populate interfacesWithVtbls if // we haven't done so previously (due to e.g. ClassReferenceExp, we can // have multiple initializer constants for a single class). addFieldInitializers(constants, explicitInitializers, aggrdecl, offset, interfacesWithVtbls.empty()); // tail padding? const size_t structsize = aggrdecl->size(Loc()); if (offset < structsize) add_zeros(constants, offset, structsize); assert(!constants.empty()); // get LL field types llvm::SmallVector<llvm::Type *, 16> types; types.reserve(constants.size()); for (auto c : constants) types.push_back(c->getType()); auto llStructType = getLLStructType(); bool isCompatible = (types.size() == llStructType->getNumElements()); if (isCompatible) { for (size_t i = 0; i < types.size(); i++) { if (types[i] != llStructType->getElementType(i)) { isCompatible = false; break; } } } // build constant LLStructType *llType = isCompatible ? llStructType : LLStructType::get(gIR->context(), types, isPacked()); llvm::Constant *c = LLConstantStruct::get(llType, constants); IF_LOG Logger::cout() << "final initializer: " << *c << std::endl; return c; }
llvm::Type* IrTypeClass::buildType() { IF_LOG Logger::println("Building class type %s @ %s", cd->toPrettyChars(), cd->loc.toChars()); LOG_SCOPE; IF_LOG Logger::println("Instance size: %u", cd->structsize); // find the fields that contribute to the default initializer. // these will define the default type. std::vector<llvm::Type*> defaultTypes; defaultTypes.reserve(32); // add vtbl defaultTypes.push_back(llvm::PointerType::get(vtbl_type, 0)); // interfaces are just a vtable if (cd->isInterfaceDeclaration()) { num_interface_vtbls = cd->vtblInterfaces ? cd->vtblInterfaces->dim : 0; } // classes have monitor and fields else { // add monitor defaultTypes.push_back(llvm::PointerType::get(llvm::Type::getInt8Ty(gIR->context()), 0)); // we start right after the vtbl and monitor size_t offset = PTRSIZE * 2; size_t field_index = 2; // add data members recursively addBaseClassData(defaultTypes, cd, offset, field_index); #if 1 // tail padding? if (offset < cd->structsize) { field_index += add_zeros(defaultTypes, cd->structsize - offset); offset = cd->structsize; } #endif } // errors are fatal during codegen if (global.errors) fatal(); // set struct body isaStruct(type)->setBody(defaultTypes, false); // VTBL // set vtbl type body vtbl_type->setBody(buildVtblType(ClassDeclaration::classinfo->type, &cd->vtbl)); IF_LOG Logger::cout() << "class type: " << *type << std::endl; return get(); }
void AggrTypeBuilder::addTailPadding(unsigned aggregateSize) { // tail padding? if (m_offset < aggregateSize) { add_zeros(m_defaultTypes, m_offset, aggregateSize); } }
void AggrTypeBuilder::alignCurrentOffset(unsigned alignment) { m_overallAlignment = std::max(alignment, m_overallAlignment); unsigned aligned = (m_offset + alignment - 1) & ~(alignment - 1); if (m_offset < aligned) { m_fieldIndex += add_zeros(m_defaultTypes, m_offset, aligned); m_offset = aligned; } }
llvm::Constant* IrAggr::createInitializerConstant( const VarInitMap& explicitInitializers, llvm::StructType* initializerType) { IF_LOG Logger::println("Creating initializer constant for %s", aggrdecl->toChars()); LOG_SCOPE; llvm::SmallVector<llvm::Constant*, 16> constants; unsigned offset = 0; if (type->ty == Tclass) { // add vtbl constants.push_back(getVtblSymbol()); // add monitor constants.push_back(getNullValue(DtoType(Type::tvoid->pointerTo()))); // we start right after the vtbl and monitor offset = Target::ptrsize * 2; } // Add the initializers for the member fields. While we are traversing the // class hierarchy, use the opportunity to populate interfacesWithVtbls if // we haven't done so previously (due to e.g. ClassReferenceExp, we can // have multiple initializer constants for a single class). addFieldInitializers(constants, explicitInitializers, aggrdecl, offset, interfacesWithVtbls.empty()); // tail padding? const size_t structsize = aggrdecl->size(Loc()); if (offset < structsize) { add_zeros(constants, offset, structsize); } // get initializer type if (!initializerType || initializerType->isOpaque()) { llvm::SmallVector<llvm::Constant*, 16>::iterator itr, end = constants.end(); llvm::SmallVector<llvm::Type*, 16> types; types.reserve(constants.size()); for (itr = constants.begin(); itr != end; ++itr) types.push_back((*itr)->getType()); if (!initializerType) initializerType = LLStructType::get(gIR->context(), types, isPacked()); else initializerType->setBody(types, isPacked()); } // build constant assert(!constants.empty()); llvm::Constant* c = LLConstantStruct::get(initializerType, constants); IF_LOG Logger::cout() << "final initializer: " << *c << std::endl; return c; }
void IrTypeClass::addBaseClassData( std::vector<llvm::Type *> & defaultTypes, ClassDeclaration * base, size_t & offset, size_t & field_index) { if (base->baseClass) { addBaseClassData(defaultTypes, base->baseClass, offset, field_index); } // FIXME: merge code with structs in IrTypeAggr // mirror the sd->fields array but only fill in contributors size_t n = base->fields.dim; LLSmallVector<VarDeclaration*, 16> data(n, NULL); default_fields.reserve(n); // first fill in the fields with explicit initializers VarDeclarationIter field_it(base->fields); for (; field_it.more(); field_it.next()) { // init is !null for explicit inits if (field_it->init != NULL) { IF_LOG Logger::println("adding explicit initializer for struct field %s", field_it->toChars()); data[field_it.index] = *field_it; size_t f_begin = field_it->offset; size_t f_end = f_begin + field_it->type->size(); // make sure there is no overlap for (size_t i = 0; i < field_it.index; i++) { if (data[i] != NULL) { VarDeclaration* vd = data[i]; size_t v_begin = vd->offset; size_t v_end = v_begin + vd->type->size(); if (v_begin >= f_end || v_end <= f_begin) continue; base->error(vd->loc, "has overlapping initialization for %s and %s", field_it->toChars(), vd->toChars()); } } } } if (global.errors) { fatal(); } // fill in default initializers field_it = VarDeclarationIter(base->fields); for (;field_it.more(); field_it.next()) { if (data[field_it.index]) continue; size_t f_begin = field_it->offset; size_t f_end = f_begin + field_it->type->size(); // make sure it doesn't overlap anything explicit bool overlaps = false; for (size_t i = 0; i < n; i++) { if (data[i]) { size_t v_begin = data[i]->offset; size_t v_end = v_begin + data[i]->type->size(); if (v_begin >= f_end || v_end <= f_begin) continue; overlaps = true; break; } } // if no overlap was found, add the default initializer if (!overlaps) { IF_LOG Logger::println("adding default initializer for struct field %s", field_it->toChars()); data[field_it.index] = *field_it; } } // ok. now we can build a list of llvm types. and make sure zeros are inserted if necessary. // first we sort the list by offset std::sort(data.begin(), data.end(), var_offset_sort_cb); // add types to list for (size_t i = 0; i < n; i++) { VarDeclaration* vd = data[i]; if (vd == NULL) continue; assert(vd->offset >= offset && "it's a bug... most likely DMD bug 2481"); // add to default field list if (cd == base) default_fields.push_back(vd); // get next aligned offset for this type size_t alignedoffset = realignOffset(offset, vd->type); // insert explicit padding? if (alignedoffset < vd->offset) { field_index += add_zeros(defaultTypes, vd->offset - alignedoffset); } // add default type defaultTypes.push_back(DtoType(vd->type)); // advance offset to right past this field offset = vd->offset + vd->type->size(); // create ir field vd->aggrIndex = (unsigned)field_index; ++field_index; } // any interface implementations? if (base->vtblInterfaces && base->vtblInterfaces->dim > 0) { bool new_instances = (base == cd); ArrayIter<BaseClass> it2(*base->vtblInterfaces); VarDeclarationIter interfaces_idx(ClassDeclaration::classinfo->fields, 3); Type* first = interfaces_idx->type->nextOf()->pointerTo(); // align offset offset = (offset + PTRSIZE - 1) & ~(PTRSIZE - 1); for (; !it2.done(); it2.next()) { BaseClass* b = it2.get(); IF_LOG Logger::println("Adding interface vtbl for %s", b->base->toPrettyChars()); FuncDeclarations arr; b->fillVtbl(cd, &arr, new_instances); llvm::Type* ivtbl_type = llvm::StructType::get(gIR->context(), buildVtblType(first, &arr)); defaultTypes.push_back(llvm::PointerType::get(ivtbl_type, 0)); offset += PTRSIZE; // add to the interface map addInterfaceToMap(b->base, field_index); field_index++; // inc count num_interface_vtbls++; } } #if 0 // tail padding? if (offset < base->structsize) { field_index += add_zeros(defaultTypes, base->structsize - offset); offset = base->structsize; } #endif }
std::vector<llvm::Value*> DtoStructLiteralValues(const StructDeclaration* sd, const std::vector<llvm::Value*>& inits, bool isConst) { // get arrays size_t nvars = sd->fields.dim; VarDeclaration** vars = (VarDeclaration**)sd->fields.data; assert(inits.size() == nvars); // first locate all explicit initializers std::vector<VarDeclaration*> explicitInits; for (size_t i=0; i < nvars; i++) { if (inits[i]) { explicitInits.push_back(vars[i]); } } // vector of values to build aggregate from std::vector<llvm::Value*> values; // offset trackers size_t lastoffset = 0; size_t lastsize = 0; // index of next explicit init size_t exidx = 0; // number of explicit inits size_t nex = explicitInits.size(); // for through each field and build up the struct, padding with zeros size_t i; for (i=0; i<nvars; i++) { VarDeclaration* var = vars[i]; // get var info size_t os = var->offset; size_t sz = var->type->size(); // get next explicit VarDeclaration* nextVar = NULL; size_t nextOs = 0; if (exidx < nex) { nextVar = explicitInits[exidx]; nextOs = nextVar->offset; } // none, rest is defaults else { break; } // not explicit initializer, default initialize if there is room, otherwise skip if (!inits[i]) { // default init if there is room // (past current offset) and (small enough to fit before next explicit) if ((os >= lastoffset + lastsize) && (os+sz <= nextOs)) { // add any 0 padding needed before this field if (os > lastoffset + lastsize) { //printf("1added %lu zeros\n", os - lastoffset - lastsize); add_zeros(values, os - lastoffset - lastsize); } // get field default init IrField* f = var->ir.irField; assert(f); values.push_back(f->getDefaultInit()); lastoffset = os; lastsize = sz; //printf("added default: %s : %lu (%lu)\n", var->toChars(), os, sz); } // skip continue; } assert(nextVar == var); // add any 0 padding needed before this field if (!isConst && os > lastoffset + lastsize) { //printf("added %lu zeros\n", os - lastoffset - lastsize); add_zeros(values, os - lastoffset - lastsize); } // add the expression value values.push_back(inits[i]); // update offsets lastoffset = os; #if DMDV2 // sometimes size of the initializer is less than size of the variable, // so make sure that lastsize is correct if (inits[i]->getType()->isSized()) lastsize = ceil(gTargetData->getTypeSizeInBits(inits[i]->getType()) / 8.0); else #endif lastsize = sz; // go to next explicit init exidx++; //printf("added field: %s : %lu (%lu)\n", var->toChars(), os, sz); } // fill out rest with default initializers LLType* structtype = DtoType(sd->type); size_t structsize = getTypePaddedSize(structtype); // FIXME: this could probably share some code with the above if (structsize > lastoffset+lastsize) { for (/*continue from first loop*/; i < nvars; i++) { VarDeclaration* var = vars[i]; // get var info size_t os = var->offset; size_t sz = var->type->size(); // skip? if (os < lastoffset + lastsize) continue; // add any 0 padding needed before this field if (os > lastoffset + lastsize) { //printf("2added %lu zeros\n", os - lastoffset - lastsize); add_zeros(values, os - lastoffset - lastsize); } // get field default init IrField* f = var->ir.irField; assert(f); values.push_back(f->getDefaultInit()); lastoffset = os; lastsize = sz; //printf("2added default: %s : %lu (%lu)\n", var->toChars(), os, sz); } } // add any 0 padding needed at the end of the literal if (structsize > lastoffset+lastsize) { //printf("3added %lu zeros\n", structsize - lastoffset - lastsize); add_zeros(values, structsize - lastoffset - lastsize); } return values; }
void IrStruct::addBaseClassInits( std::vector<llvm::Constant*>& constants, ClassDeclaration* base, size_t& offset, size_t& field_index) { if (base->baseClass) { addBaseClassInits(constants, base->baseClass, offset, field_index); } IrTypeClass* tc = stripModifiers(base->type)->irtype->isClass(); assert(tc); // go through fields IrTypeAggr::iterator it; for (it = tc->def_begin(); it != tc->def_end(); ++it) { VarDeclaration* vd = *it; IF_LOG Logger::println("Adding default field %s %s (+%u)", vd->type->toChars(), vd->toChars(), vd->offset); LOG_SCOPE; assert(vd->offset >= offset && "default fields not sorted by offset"); // get next aligned offset for this type size_t alignedoffset = realignOffset(offset, vd->type); // insert explicit padding? if (alignedoffset < vd->offset) { add_zeros(constants, vd->offset - alignedoffset); } // add default type constants.push_back(get_default_initializer(vd, vd->init)); // advance offset to right past this field offset = vd->offset + vd->type->size(); } // has interface vtbls? if (base->vtblInterfaces && base->vtblInterfaces->dim > 0) { // false when it's not okay to use functions from super classes bool newinsts = (base == aggrdecl->isClassDeclaration()); size_t inter_idx = interfacesWithVtbls.size(); offset = (offset + PTRSIZE - 1) & ~(PTRSIZE - 1); ArrayIter<BaseClass> it2(*base->vtblInterfaces); for (; !it2.done(); it2.next()) { BaseClass* b = it2.get(); constants.push_back(getInterfaceVtbl(b, newinsts, inter_idx)); offset += PTRSIZE; // add to the interface list interfacesWithVtbls.push_back(b); inter_idx++; } } }
void IrAggr::addFieldInitializers( llvm::SmallVectorImpl<llvm::Constant *> &constants, const VarInitMap &explicitInitializers, AggregateDeclaration *decl, unsigned &offset, bool populateInterfacesWithVtbls) { if (ClassDeclaration *cd = decl->isClassDeclaration()) { if (cd->baseClass) { addFieldInitializers(constants, explicitInitializers, cd->baseClass, offset, populateInterfacesWithVtbls); } // has interface vtbls? if (cd->vtblInterfaces && cd->vtblInterfaces->dim > 0) { // Align interface infos to pointer size. unsigned aligned = (offset + Target::ptrsize - 1) & ~(Target::ptrsize - 1); if (offset < aligned) { add_zeros(constants, offset, aligned); offset = aligned; } // false when it's not okay to use functions from super classes bool newinsts = (cd == aggrdecl->isClassDeclaration()); size_t inter_idx = interfacesWithVtbls.size(); for (auto bc : *cd->vtblInterfaces) { constants.push_back(getInterfaceVtbl(bc, newinsts, inter_idx)); offset += Target::ptrsize; inter_idx++; if (populateInterfacesWithVtbls) interfacesWithVtbls.push_back(bc); } } } AggrTypeBuilder b(false, offset); b.addAggregate(decl, &explicitInitializers, AggrTypeBuilder::Aliases::Skip); offset = b.currentOffset(); const size_t baseLLFieldIndex = constants.size(); const size_t numNewLLFields = b.defaultTypes().size(); constants.resize(constants.size() + numNewLLFields, nullptr); // add explicit and non-overlapping implicit initializers for (const auto &pair : b.varGEPIndices()) { const auto field = pair.first; const size_t fieldIndex = pair.second; const auto explicitIt = explicitInitializers.find(field); llvm::Constant *init = (explicitIt != explicitInitializers.end() ? explicitIt->second : getDefaultInitializer(field)); constants[baseLLFieldIndex + fieldIndex] = FillSArrayDims(field->type, init); } // zero out remaining padding fields for (size_t i = 0; i < numNewLLFields; i++) { auto &init = constants[baseLLFieldIndex + i]; if (!init) init = llvm::Constant::getNullValue(b.defaultTypes()[i]); } }
void AggrTypeBuilder::addAggregate( AggregateDeclaration *ad, const AggrTypeBuilder::VarInitMap *explicitInits, AggrTypeBuilder::Aliases aliases) { const size_t n = ad->fields.dim; if (n == 0) return; // prioritize overlapping fields LLSmallVector<FieldPriority, 16> priorities; priorities.reserve(n); for (auto f : ad->fields) { priorities.push_back(prioritize(f, explicitInits)); IF_LOG Logger::println("Field priority for %s: %d", f->toChars(), priorities.back()); } // mirror the ad->fields array but only fill in contributors LLSmallVector<VarDeclaration *, 16> data(n, nullptr); // list of pairs: alias => actual field (same offset, same LL type) LLSmallVector<std::pair<VarDeclaration *, VarDeclaration *>, 16> aliasPairs; // one pass per priority in descending order const auto minMaxPriority = std::minmax_element(priorities.begin(), priorities.end()); for (int p = *minMaxPriority.second; p >= *minMaxPriority.first; p--) { // iterate over fields of that priority, in declaration order for (size_t index = 0; index < n; ++index) { if (priorities[index] != p) continue; VarDeclaration *field = ad->fields[index]; const size_t f_begin = field->offset; const size_t f_end = f_begin + field->type->size(); // skip empty fields if (f_begin == f_end) continue; // check for overlapping existing fields bool overlaps = false; if (field->overlapped) { for (const auto vd : data) { if (!vd) continue; const size_t v_begin = vd->offset; const size_t v_end = v_begin + vd->type->size(); if (v_begin < f_end && v_end > f_begin) { if (aliases == Aliases::AddToVarGEPIndices && v_begin == f_begin && DtoMemType(vd->type) == DtoMemType(field->type)) { aliasPairs.push_back(std::make_pair(field, vd)); } overlaps = true; break; } } } if (!overlaps) data[index] = field; } } // Now we can build a list of LLVM types for the actual LL fields. // Make sure to zero out any padding and set the GEP indices for the directly // indexable variables. // first we sort the list by offset std::sort(data.begin(), data.end(), var_offset_sort_cb); for (const auto vd : data) { if (!vd) continue; assert(vd->offset >= m_offset && "Variable overlaps previous field."); // Add an explicit field for any padding so we can zero it, as per TDPL // §7.1.1. if (m_offset < vd->offset) { m_fieldIndex += add_zeros(m_defaultTypes, m_offset, vd->offset); m_offset = vd->offset; } // add default type m_defaultTypes.push_back(DtoMemType(vd->type)); // advance offset to right past this field m_offset += getMemberSize(vd->type); // set the field index m_varGEPIndices[vd] = m_fieldIndex; // let any aliases reuse this field/GEP index for (const auto &pair : aliasPairs) { if (pair.second == vd) m_varGEPIndices[pair.first] = m_fieldIndex; } ++m_fieldIndex; } }
void IrAggr::addFieldInitializers( llvm::SmallVectorImpl<llvm::Constant*>& constants, const VarInitMap& explicitInitializers, AggregateDeclaration* decl, unsigned& offset, bool populateInterfacesWithVtbls ) { if (ClassDeclaration* cd = decl->isClassDeclaration()) { if (cd->baseClass) { addFieldInitializers(constants, explicitInitializers, cd->baseClass, offset, populateInterfacesWithVtbls); } } // Build up vector with one-to-one mapping to field indices. const size_t n = decl->fields.dim; llvm::SmallVector<VarInitConst, 16> data(n); // Fill in explicit initializers. for (size_t i = 0; i < n; ++i) { VarDeclaration* vd = decl->fields[i]; VarInitMap::const_iterator expl = explicitInitializers.find(vd); if (expl != explicitInitializers.end()) data[i] = *expl; } // Fill in implicit initializers for (size_t i = 0; i < n; i++) { if (data[i].first) continue; VarDeclaration* vd = decl->fields[i]; /* Skip void initializers for unions. DMD bug 3991: union X { int a = void; dchar b = 'a'; } */ if (decl->isUnionDeclaration() && vd->init && vd->init->isVoidInitializer()) continue; unsigned vd_begin = vd->offset; unsigned vd_end = vd_begin + vd->type->size(); /* Skip zero size fields like zero-length static arrays, LDC issue 812: class B { ubyte[0] test; } */ if (vd_begin == vd_end) continue; // make sure it doesn't overlap any explicit initializers. bool overlaps = false; if (type->ty == Tstruct) { // Only structs and unions can have overlapping fields. for (size_t j = 0; j < n; ++j) { if (i == j || !data[j].first) continue; VarDeclaration* it = decl->fields[j]; unsigned f_begin = it->offset; unsigned f_end = f_begin + it->type->size(); if (vd_begin >= f_end || vd_end <= f_begin) continue; overlaps = true; break; } } // add if no overlap found if (!overlaps) { IF_LOG Logger::println("Implicit initializer: %s @+%u", vd->toChars(), vd->offset); LOG_SCOPE; data[i].first = vd; data[i].second = get_default_initializer(vd, NULL); } } // Sort data array by offset. // TODO: Figure out whether this is really necessary, fields should already // be in offset order. Not having do do this would mean we could use a plain // llvm::Constant* vector for initializers and avoid all the VarInitConst business. std::sort(data.begin(), data.end(), struct_init_data_sort); // build array of constants and make sure explicit zero padding is inserted when necessary. for (size_t i = 0; i < n; i++) { VarDeclaration* vd = data[i].first; if (vd == NULL) continue; // Explicitly zero the padding as per TDPL §7.1.1. Otherwise, it would // be left uninitialized by LLVM. if (offset < vd->offset) { add_zeros(constants, offset, vd->offset); offset = vd->offset; } IF_LOG Logger::println("adding field %s", vd->toChars()); constants.push_back(FillSArrayDims(vd->type, data[i].second)); offset += getMemberSize(vd->type); } if (ClassDeclaration* cd = decl->isClassDeclaration()) { // has interface vtbls? if (cd->vtblInterfaces && cd->vtblInterfaces->dim > 0) { // Align interface infos to pointer size. unsigned aligned = (offset + Target::ptrsize - 1) & ~(Target::ptrsize - 1); if (offset < aligned) { add_zeros(constants, offset, aligned); offset = aligned; } // false when it's not okay to use functions from super classes bool newinsts = (cd == aggrdecl->isClassDeclaration()); size_t inter_idx = interfacesWithVtbls.size(); offset = (offset + Target::ptrsize - 1) & ~(Target::ptrsize - 1); for (BaseClasses::iterator I = cd->vtblInterfaces->begin(), E = cd->vtblInterfaces->end(); I != E; ++I) { constants.push_back(getInterfaceVtbl(*I, newinsts, inter_idx)); offset += Target::ptrsize; inter_idx++; if (populateInterfacesWithVtbls) interfacesWithVtbls.push_back(*I); } } } }
void AggrTypeBuilder::addAggregate(AggregateDeclaration *ad) { // mirror the ad->fields array but only fill in contributors const size_t n = ad->fields.dim; LLSmallVector<VarDeclaration*, 16> data(n, NULL); unsigned int errors = global.errors; // first fill in the fields with explicit initializers for (size_t index = 0; index < n; ++index) { VarDeclaration *field = ad->fields[index]; // init is !null for explicit inits if (field->init != NULL && !field->init->isVoidInitializer()) { IF_LOG Logger::println("adding explicit initializer for struct field %s", field->toChars()); size_t f_size = field->type->size(); size_t f_begin = field->offset; size_t f_end = f_begin + f_size; if (f_size == 0) continue; data[index] = field; // make sure there is no overlap for (size_t i = 0; i < index; i++) { if (data[i] != NULL) { VarDeclaration* vd = data[i]; size_t v_begin = vd->offset; size_t v_end = v_begin + vd->type->size(); if (v_begin >= f_end || v_end <= f_begin) continue; ad->error(vd->loc, "has overlapping initialization for %s and %s", field->toChars(), vd->toChars()); } } } } if (errors != global.errors) { // There was an overlapping initialization. // Return if errors are gagged otherwise abort. if (global.gag) return; fatal(); } // fill in default initializers for (size_t index = 0; index < n; ++index) { if (data[index]) continue; VarDeclaration *field = ad->fields[index]; size_t f_size = field->type->size(); size_t f_begin = field->offset; size_t f_end = f_begin + f_size; if (f_size == 0) continue; // make sure it doesn't overlap anything explicit bool overlaps = false; for (size_t i = 0; i < n; i++) { if (data[i]) { size_t v_begin = data[i]->offset; size_t v_end = v_begin + data[i]->type->size(); if (v_begin >= f_end || v_end <= f_begin) continue; overlaps = true; break; } } // if no overlap was found, add the default initializer if (!overlaps) { IF_LOG Logger::println("adding default initializer for struct field %s", field->toChars()); data[index] = field; } } // // ok. now we can build a list of llvm types. and make sure zeros are inserted if necessary. // // first we sort the list by offset std::sort(data.begin(), data.end(), var_offset_sort_cb); // add types to list for (size_t i = 0; i < n; i++) { VarDeclaration* vd = data[i]; if (vd == NULL) continue; assert(vd->offset >= m_offset && "it's a bug... most likely DMD bug 2481"); // get next aligned offset for this type size_t alignedoffset = m_offset; if (!m_packed) { alignedoffset = realignOffset(alignedoffset, vd->type); } // insert explicit padding? if (alignedoffset < vd->offset) { m_fieldIndex += add_zeros(m_defaultTypes, alignedoffset, vd->offset); } // add default type m_defaultTypes.push_back(DtoType(vd->type)); // advance offset to right past this field m_offset = vd->offset + vd->type->size(); // set the field index m_varGEPIndices[vd] = m_fieldIndex; ++m_fieldIndex; } }