llvm::Constant * IrField::getDefaultInit() { if (constInit) return constInit; constInit = get_default_initializer(V, V->init); return constInit; }
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); } } // 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); } } } }