void TypeInfoStructDeclaration::llvmDefine() { Logger::println("TypeInfoStructDeclaration::llvmDefine() %s", toChars()); LOG_SCOPE; // make sure struct is resolved assert(tinfo->ty == Tstruct); TypeStruct *tc = static_cast<TypeStruct *>(tinfo); StructDeclaration *sd = tc->sym; // can't emit typeinfo for forward declarations if (sd->sizeok != 1) { sd->error("cannot emit TypeInfo for forward declaration"); fatal(); } sd->codegen(Type::sir); IrStruct* irstruct = sd->ir.irStruct; RTTIBuilder b(Type::typeinfostruct); // char[] name b.push_string(sd->toPrettyChars()); // void[] init // never emit a null array, even for zero initialized typeinfo // the size() method uses this array! size_t init_size = getTypeStoreSize(tc->irtype->getType()); b.push_void_array(init_size, irstruct->getInitSymbol()); // toX functions ground work static TypeFunction *tftohash; static TypeFunction *tftostring; if (!tftohash) { Scope sc; tftohash = new TypeFunction(NULL, Type::thash_t, 0, LINKd); #if DMDV2 tftohash ->mod = MODconst; #endif tftohash = static_cast<TypeFunction *>(tftohash->semantic(0, &sc)); #if DMDV2 Type *retType = Type::tchar->invariantOf()->arrayOf(); #else Type *retType = Type::tchar->arrayOf(); #endif tftostring = new TypeFunction(NULL, retType, 0, LINKd); tftostring = static_cast<TypeFunction *>(tftostring->semantic(0, &sc)); } // this one takes a parameter, so we need to build a new one each time // to get the right type. can we avoid this? TypeFunction *tfcmpptr; { Scope sc; Parameters *arguments = new Parameters; #if STRUCTTHISREF // arg type is ref const T Parameter *arg = new Parameter(STCref, tc->constOf(), NULL, NULL); #else // arg type is const T* Parameter *arg = new Parameter(STCin, tc->pointerTo(), NULL, NULL); #endif arguments->push(arg); tfcmpptr = new TypeFunction(arguments, Type::tint32, 0, LINKd); #if DMDV2 tfcmpptr->mod = MODconst; #endif tfcmpptr = static_cast<TypeFunction *>(tfcmpptr->semantic(0, &sc)); } // well use this module for all overload lookups Module *gm = getModule(); // toHash FuncDeclaration* fd = find_method_overload(sd, Id::tohash, tftohash, gm); b.push_funcptr(fd); // opEquals #if DMDV2 fd = sd->xeq; #else fd = find_method_overload(sd, Id::eq, tfcmpptr, gm); #endif b.push_funcptr(fd); // opCmp fd = find_method_overload(sd, Id::cmp, tfcmpptr, gm); b.push_funcptr(fd); // toString fd = find_method_overload(sd, Id::tostring, tftostring, gm); b.push_funcptr(fd); // uint m_flags; unsigned hasptrs = tc->hasPointers() ? 1 : 0; b.push_uint(hasptrs); #if DMDV2 ClassDeclaration* tscd = Type::typeinfostruct; assert((!global.params.is64bit && tscd->fields.dim == 11) || (global.params.is64bit && tscd->fields.dim == 13)); //void function(void*) xdtor; b.push_funcptr(sd->dtor); //void function(void*) xpostblit; FuncDeclaration *xpostblit = sd->postblit; if (xpostblit && sd->postblit->storage_class & STCdisable) xpostblit = 0; b.push_funcptr(xpostblit); //uint m_align; b.push_uint(tc->alignsize()); if (global.params.is64bit) { // TypeInfo m_arg1; // TypeInfo m_arg2; TypeTuple *tup = tc->toArgTypes(); assert(tup->arguments->dim <= 2); for (unsigned i = 0; i < 2; i++) { if (i < tup->arguments->dim) { Type *targ = static_cast<Parameter *>(tup->arguments->data[i])->type; targ = targ->merge(); b.push_typeinfo(targ); } else b.push_null(Type::typeinfo->type); } } // immutable(void)* m_RTInfo; // The cases where getRTInfo is null are not quite here, but the code is // modelled after what DMD does. if (sd->getRTInfo) b.push(sd->getRTInfo->toConstElem(gIR)); else if (!tc->hasPointers()) b.push_size_as_vp(0); // no pointers else b.push_size_as_vp(1); // has pointers #endif // finish b.finalize(ir.irGlobal); }
void visit(TypeInfoStructDeclaration *decl) override { IF_LOG Logger::println("TypeInfoStructDeclaration::llvmDefine() %s", decl->toChars()); LOG_SCOPE; // make sure struct is resolved assert(decl->tinfo->ty == Tstruct); TypeStruct *tc = static_cast<TypeStruct *>(decl->tinfo); StructDeclaration *sd = tc->sym; // check declaration in object.d const auto structTypeInfoType = getStructTypeInfoType(); const auto structTypeInfoDecl = Type::typeinfostruct; // On x86_64, class TypeInfo_Struct contains 2 additional fields // (m_arg1/m_arg2) which are used for the X86_64 System V ABI varargs // implementation. They are not present on any other cpu/os. const bool isX86_64 = global.params.targetTriple->getArch() == llvm::Triple::x86_64; const unsigned expectedFields = 11 + (isX86_64 ? 2 : 0); const unsigned actualFields = structTypeInfoDecl->fields.dim - 1; // union of xdtor/xdtorti counts as 2 overlapping fields if (actualFields != expectedFields) { error(Loc(), "Unexpected number of `object.TypeInfo_Struct` fields; " "druntime version does not match compiler"); fatal(); } RTTIBuilder b(structTypeInfoType); // handle opaque structs if (!sd->members) { Logger::println("is opaque struct, emitting dummy TypeInfo_Struct"); b.push_null_void_array(); // name b.push_null_void_array(); // m_init b.push_null_vp(); // xtoHash b.push_null_vp(); // xopEquals b.push_null_vp(); // xopCmp b.push_null_vp(); // xtoString b.push_uint(0); // m_flags b.push_null_vp(); // xdtor/xdtorti b.push_null_vp(); // xpostblit b.push_uint(0); // m_align if (isX86_64) { b.push_null_vp(); // m_arg1 b.push_null_vp(); // m_arg2 } b.push_null_vp(); // m_RTInfo b.finalize(gvar); return; } // can't emit typeinfo for forward declarations if (sd->sizeok != SIZEOKdone) { sd->error("cannot emit `TypeInfo` for forward declaration"); fatal(); } DtoResolveStruct(sd); if (TemplateInstance *ti = sd->isInstantiated()) { if (!ti->needsCodegen()) { assert(ti->minst || sd->requestTypeInfo); // We won't emit ti, so emit the special member functions in here. if (sd->xeq && sd->xeq != StructDeclaration::xerreq && sd->xeq->semanticRun >= PASSsemantic3) { Declaration_codegen(sd->xeq); } if (sd->xcmp && sd->xcmp != StructDeclaration::xerrcmp && sd->xcmp->semanticRun >= PASSsemantic3) { Declaration_codegen(sd->xcmp); } if (FuncDeclaration *ftostr = search_toString(sd)) { if (ftostr->semanticRun >= PASSsemantic3) Declaration_codegen(ftostr); } if (sd->xhash && sd->xhash->semanticRun >= PASSsemantic3) { Declaration_codegen(sd->xhash); } if (sd->postblit && sd->postblit->semanticRun >= PASSsemantic3) { Declaration_codegen(sd->postblit); } if (sd->dtor && sd->dtor->semanticRun >= PASSsemantic3) { Declaration_codegen(sd->dtor); } if (sd->tidtor && sd->tidtor->semanticRun >= PASSsemantic3) { Declaration_codegen(sd->tidtor); } } } IrAggr *iraggr = getIrAggr(sd); // string name b.push_string(sd->toPrettyChars()); // void[] m_init // The protocol is to write a null pointer for zero-initialized arrays. The // length field is always needed for tsize(). llvm::Constant *initPtr; if (tc->isZeroInit(Loc())) { initPtr = getNullValue(getVoidPtrType()); } else { initPtr = iraggr->getInitSymbol(); } b.push_void_array(getTypeStoreSize(DtoType(tc)), initPtr); // function xtoHash FuncDeclaration *fd = sd->xhash; b.push_funcptr(fd); // function xopEquals fd = sd->xeq; b.push_funcptr(fd); // function xopCmp fd = sd->xcmp; b.push_funcptr(fd); // function xtoString fd = search_toString(sd); b.push_funcptr(fd); // uint m_flags unsigned hasptrs = tc->hasPointers() ? 1 : 0; b.push_uint(hasptrs); // function xdtor/xdtorti b.push_funcptr(sd->tidtor); // function xpostblit FuncDeclaration *xpostblit = sd->postblit; if (xpostblit && sd->postblit->storage_class & STCdisable) { xpostblit = nullptr; } b.push_funcptr(xpostblit); // uint m_align b.push_uint(DtoAlignment(tc)); if (isX86_64) { // TypeInfo m_arg1 // TypeInfo m_arg2 Type *t = sd->arg1type; for (unsigned i = 0; i < 2; i++) { if (t) { t = merge(t); b.push_typeinfo(t); } else { b.push_null(getTypeInfoType()); } t = sd->arg2type; } } // immutable(void)* m_RTInfo // The cases where getRTInfo is null are not quite here, but the code is // modelled after what DMD does. if (sd->getRTInfo) { b.push(toConstElem(sd->getRTInfo, gIR)); } else if (!tc->hasPointers()) { b.push_size_as_vp(0); // no pointers } else { b.push_size_as_vp(1); // has pointers } // finish b.finalize(gvar); }
void visit(TypeInfoStructDeclaration *decl) { IF_LOG Logger::println("TypeInfoStructDeclaration::llvmDefine() %s", decl->toChars()); LOG_SCOPE; // make sure struct is resolved assert(decl->tinfo->ty == Tstruct); TypeStruct *tc = static_cast<TypeStruct *>(decl->tinfo); StructDeclaration *sd = tc->sym; // handle opaque structs if (!sd->members) { RTTIBuilder b(Type::typeinfostruct); b.finalize(getIrGlobal(decl)); return; } // can't emit typeinfo for forward declarations if (sd->sizeok != SIZEOKdone) { sd->error("cannot emit TypeInfo for forward declaration"); fatal(); } DtoResolveStruct(sd); IrAggr* iraggr = getIrAggr(sd); RTTIBuilder b(Type::typeinfostruct); // char[] name b.push_string(sd->toPrettyChars()); // void[] init // The protocol is to write a null pointer for zero-initialized arrays. The // length field is always needed for tsize(). llvm::Constant *initPtr; if (tc->isZeroInit(Loc())) initPtr = getNullValue(getVoidPtrType()); else initPtr = iraggr->getInitSymbol(); b.push_void_array(getTypeStoreSize(DtoType(tc)), initPtr); // well use this module for all overload lookups // toHash FuncDeclaration* fd = sd->xhash; b.push_funcptr(fd); // opEquals fd = sd->xeq; b.push_funcptr(fd); // opCmp fd = sd->xcmp; b.push_funcptr(fd); // toString fd = search_toString(sd); b.push_funcptr(fd); // uint m_flags; unsigned hasptrs = tc->hasPointers() ? 1 : 0; b.push_uint(hasptrs); // On x86_64, class TypeInfo_Struct contains 2 additional fields // (m_arg1/m_arg2) which are used for the X86_64 System V ABI varargs // implementation. They are not present on any other cpu/os. assert((global.params.targetTriple.getArch() != llvm::Triple::x86_64 && Type::typeinfostruct->fields.dim == 11) || (global.params.targetTriple.getArch() == llvm::Triple::x86_64 && Type::typeinfostruct->fields.dim == 13)); //void function(void*) xdtor; b.push_funcptr(sd->dtor); //void function(void*) xpostblit; FuncDeclaration *xpostblit = sd->postblit; if (xpostblit && sd->postblit->storage_class & STCdisable) xpostblit = 0; b.push_funcptr(xpostblit); //uint m_align; b.push_uint(tc->alignsize()); if (global.params.is64bit) { // TypeInfo m_arg1; // TypeInfo m_arg2; Type *t = sd->arg1type; for (unsigned i = 0; i < 2; i++) { if (t) { t = t->merge(); b.push_typeinfo(t); } else b.push_null(Type::dtypeinfo->type); t = sd->arg2type; } } // immutable(void)* m_RTInfo; // The cases where getRTInfo is null are not quite here, but the code is // modelled after what DMD does. if (sd->getRTInfo) b.push(toConstElem(sd->getRTInfo, gIR)); else if (!tc->hasPointers()) b.push_size_as_vp(0); // no pointers else b.push_size_as_vp(1); // has pointers // finish b.finalize(getIrGlobal(decl)); }