Skip to content

Commit 9452350

Browse files
committed
Slightly refactor RTTIBuilder
1 parent 90752e8 commit 9452350

File tree

8 files changed

+44
-61
lines changed

8 files changed

+44
-61
lines changed

gen/classes.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,8 @@ LLConstant *DtoDefineClassInfo(ClassDeclaration *cd) {
613613
assert(cd->type->ty == Tclass);
614614

615615
IrAggr *ir = getIrAggr(cd);
616-
getClassInfoType(); // check declaration in object.d
617-
ClassDeclaration *cinfo = Type::typeinfoclass;
616+
Type *const cinfoType = getClassInfoType(); // check declaration in object.d
617+
ClassDeclaration *const cinfo = Type::typeinfoclass;
618618

619619
if (cinfo->fields.dim != 12) {
620620
error(Loc(), "Unexpected number of fields in `object.ClassInfo`; "
@@ -623,7 +623,7 @@ LLConstant *DtoDefineClassInfo(ClassDeclaration *cd) {
623623
}
624624

625625
// use the rtti builder
626-
RTTIBuilder b(cinfo);
626+
RTTIBuilder b(cinfoType);
627627

628628
LLConstant *c;
629629

@@ -664,7 +664,7 @@ LLConstant *DtoDefineClassInfo(ClassDeclaration *cd) {
664664
if (cd->baseClass && !cd->isInterfaceDeclaration()) {
665665
b.push_classinfo(cd->baseClass);
666666
} else {
667-
b.push_null(cinfo->type);
667+
b.push_null(cinfoType);
668668
}
669669

670670
// destructor

gen/moduleinfo.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,9 @@ llvm::Constant *buildLocalClasses(Module *m, size_t &count) {
194194
}
195195

196196
llvm::GlobalVariable *genModuleInfo(Module *m) {
197-
getModuleInfoType(); // check declaration in object.d
198-
auto moduleInfoDecl = Module::moduleinfo;
197+
// check declaration in object.d
198+
const auto moduleInfoType = getModuleInfoType();
199+
const auto moduleInfoDecl = Module::moduleinfo;
199200

200201
// The "new-style" ModuleInfo records are variable-length, with the presence
201202
// of the various fields indicated by a certain flag bit. The base struct
@@ -261,7 +262,7 @@ llvm::GlobalVariable *genModuleInfo(Module *m) {
261262
}
262263

263264
// Now, start building the initialiser for the ModuleInfo instance.
264-
RTTIBuilder b(moduleInfoDecl);
265+
RTTIBuilder b(moduleInfoType);
265266

266267
b.push_uint(flags);
267268
b.push_uint(0); // index

gen/rttibuilder.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,16 @@
2020
#include "ir/iraggr.h"
2121
#include "ir/irfunction.h"
2222

23-
RTTIBuilder::RTTIBuilder(AggregateDeclaration *base_class) {
24-
DtoResolveDsymbol(base_class);
23+
RTTIBuilder::RTTIBuilder(Type *baseType) {
24+
const auto ad = isAggregate(baseType);
25+
assert(ad && "not an aggregate type");
2526

26-
base = base_class;
27-
basetype = static_cast<TypeClass *>(base->type);
27+
DtoResolveDsymbol(ad);
2828

29-
baseir = getIrAggr(base);
30-
assert(baseir && "no IrStruct for TypeInfo base class");
29+
if (ad->isClassDeclaration()) {
30+
const auto baseir = getIrAggr(ad);
31+
assert(baseir && "no IrAggr for TypeInfo base class");
3132

32-
prevFieldEnd = 0;
33-
34-
if (base->isClassDeclaration()) {
3533
// just start with adding the vtbl
3634
push(baseir->getVtblSymbol());
3735
// and monitor

gen/rttibuilder.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,25 @@
1818
#include "llvm/ADT/SmallVector.h"
1919
#include "llvm/IR/Constant.h"
2020

21-
class AggregateDeclaration;
2221
class ClassDeclaration;
2322
class Dsymbol;
2423
class FuncDeclaration;
25-
struct IrGlobal;
26-
struct IrAggr;
2724
class Type;
28-
class TypeClass;
2925
namespace llvm {
3026
class StructType;
3127
class GlobalVariable;
3228
}
3329

3430
class RTTIBuilder {
35-
AggregateDeclaration *base;
36-
TypeClass *basetype;
37-
IrAggr *baseir;
38-
3931
/// The offset (in bytes) at which the previously pushed field ended.
40-
uint64_t prevFieldEnd;
32+
uint64_t prevFieldEnd = 0;
4133

4234
public:
4335
// 15 is enough for any D2 ClassInfo including 64 bit pointer alignment
4436
// padding
4537
llvm::SmallVector<llvm::Constant *, 15> inits;
4638

47-
explicit RTTIBuilder(AggregateDeclaration *base_class);
39+
explicit RTTIBuilder(Type *baseType);
4840

4941
void push(llvm::Constant *C);
5042
void push_null(Type *T);

gen/runtime.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ LazyClassType invariantTypeInfoTy(Type::typeinfoinvariant,
183183
LazyClassType sharedTypeInfoTy(Type::typeinfoshared, "TypeInfo_Shared");
184184
LazyClassType inoutTypeInfoTy(Type::typeinfowild, "TypeInfo_Inout");
185185
LazyClassType throwableTy(ClassDeclaration::throwable, "Throwable");
186+
LazyClassType cppTypeInfoPtrTy(ClassDeclaration::cpp_type_info_ptr,
187+
"__cpp_type_info_ptr");
186188

187189
using LazyAggregateType = LazyType<AggregateDeclaration>;
188190
template <> const char *LazyAggregateType::getKind() { return "struct"; }
@@ -411,6 +413,7 @@ Type *getInvariantTypeInfoType() { return invariantTypeInfoTy.get(); }
411413
Type *getSharedTypeInfoType() { return sharedTypeInfoTy.get(); }
412414
Type *getInoutTypeInfoType() { return inoutTypeInfoTy.get(); }
413415
Type *getThrowableType() { return throwableTy.get(); }
416+
Type *getCppTypeInfoPtrType() { return cppTypeInfoPtrTy.get(); }
414417
Type *getModuleInfoType() { return moduleInfoTy.get(); }
415418

416419
////////////////////////////////////////////////////////////////////////////////

gen/runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Type *getInvariantTypeInfoType();
5555
Type *getSharedTypeInfoType();
5656
Type *getInoutTypeInfoType();
5757
Type *getThrowableType();
58+
Type *getCppTypeInfoPtrType();
5859
Type *getModuleInfoType();
5960

6061
#endif // LDC_GEN_RUNTIME_H

gen/trycatchfinally.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,13 @@ void TryCatchScope::emitCatchBodies(IRState &irs, llvm::Value *ehPtrSlot) {
174174
mangleBuf.printf("%d%s", 18, "_cpp_type_info_ptr");
175175
const auto wrapperMangle = getIRMangledVarName(mangleBuf.peekString(), LINKd);
176176

177-
RTTIBuilder b(ClassDeclaration::cpp_type_info_ptr);
177+
const auto cppTypeInfoPtrType = getCppTypeInfoPtrType();
178+
RTTIBuilder b(cppTypeInfoPtrType);
178179
b.push(cpp_ti);
179180

180181
auto wrapperType = llvm::cast<llvm::StructType>(
181-
static_cast<IrTypeClass*>(ClassDeclaration::cpp_type_info_ptr->type->ctype)->getMemoryLLType());
182+
static_cast<IrTypeClass *>(cppTypeInfoPtrType->ctype)
183+
->getMemoryLLType());
182184
auto wrapperInit = b.get_constant(wrapperType);
183185

184186
ci = getOrCreateGlobal(

gen/typinf.cpp

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ class LLVMDefineVisitor : public Visitor {
144144
decl->toChars());
145145
LOG_SCOPE;
146146

147-
getTypeInfoType(); // check declaration in object.d
148-
RTTIBuilder b(Type::dtypeinfo);
147+
RTTIBuilder b(getTypeInfoType());
149148
b.finalize(gvar);
150149
}
151150

@@ -156,8 +155,7 @@ class LLVMDefineVisitor : public Visitor {
156155
decl->toChars());
157156
LOG_SCOPE;
158157

159-
getEnumTypeInfoType(); // check declaration in object.d
160-
RTTIBuilder b(Type::typeinfoenum);
158+
RTTIBuilder b(getEnumTypeInfoType());
161159

162160
assert(decl->tinfo->ty == Tenum);
163161
TypeEnum *tc = static_cast<TypeEnum *>(decl->tinfo);
@@ -192,8 +190,7 @@ class LLVMDefineVisitor : public Visitor {
192190
decl->toChars());
193191
LOG_SCOPE;
194192

195-
getPointerTypeInfoType(); // check declaration in object.d
196-
RTTIBuilder b(Type::typeinfopointer);
193+
RTTIBuilder b(getPointerTypeInfoType());
197194
// TypeInfo base
198195
b.push_typeinfo(decl->tinfo->nextOf());
199196
// finish
@@ -207,8 +204,7 @@ class LLVMDefineVisitor : public Visitor {
207204
decl->toChars());
208205
LOG_SCOPE;
209206

210-
getArrayTypeInfoType(); // check declaration in object.d
211-
RTTIBuilder b(Type::typeinfoarray);
207+
RTTIBuilder b(getArrayTypeInfoType());
212208
// TypeInfo base
213209
b.push_typeinfo(decl->tinfo->nextOf());
214210
// finish
@@ -225,8 +221,7 @@ class LLVMDefineVisitor : public Visitor {
225221
assert(decl->tinfo->ty == Tsarray);
226222
TypeSArray *tc = static_cast<TypeSArray *>(decl->tinfo);
227223

228-
getStaticArrayTypeInfoType(); // check declaration in object.d
229-
RTTIBuilder b(Type::typeinfostaticarray);
224+
RTTIBuilder b(getStaticArrayTypeInfoType());
230225

231226
// value typeinfo
232227
b.push_typeinfo(tc->nextOf());
@@ -249,8 +244,7 @@ class LLVMDefineVisitor : public Visitor {
249244
assert(decl->tinfo->ty == Taarray);
250245
TypeAArray *tc = static_cast<TypeAArray *>(decl->tinfo);
251246

252-
getAssociativeArrayTypeInfoType(); // check declaration in object.d
253-
RTTIBuilder b(Type::typeinfoassociativearray);
247+
RTTIBuilder b(getAssociativeArrayTypeInfoType());
254248

255249
// value typeinfo
256250
b.push_typeinfo(tc->nextOf());
@@ -269,8 +263,7 @@ class LLVMDefineVisitor : public Visitor {
269263
decl->toChars());
270264
LOG_SCOPE;
271265

272-
getFunctionTypeInfoType(); // check declaration in object.d
273-
RTTIBuilder b(Type::typeinfofunction);
266+
RTTIBuilder b(getFunctionTypeInfoType());
274267
// TypeInfo base
275268
b.push_typeinfo(decl->tinfo->nextOf());
276269
// string deco
@@ -289,8 +282,7 @@ class LLVMDefineVisitor : public Visitor {
289282
assert(decl->tinfo->ty == Tdelegate);
290283
Type *ret_type = decl->tinfo->nextOf()->nextOf();
291284

292-
getDelegateTypeInfoType(); // check declaration in object.d
293-
RTTIBuilder b(Type::typeinfodelegate);
285+
RTTIBuilder b(getDelegateTypeInfoType());
294286
// TypeInfo base
295287
b.push_typeinfo(ret_type);
296288
// string deco
@@ -311,8 +303,9 @@ class LLVMDefineVisitor : public Visitor {
311303
TypeStruct *tc = static_cast<TypeStruct *>(decl->tinfo);
312304
StructDeclaration *sd = tc->sym;
313305

314-
getStructTypeInfoType(); // check declaration in object.d
315-
auto structTypeInfoDecl = Type::typeinfostruct;
306+
// check declaration in object.d
307+
const auto structTypeInfoType = getStructTypeInfoType();
308+
const auto structTypeInfoDecl = Type::typeinfostruct;
316309

317310
// On x86_64, class TypeInfo_Struct contains 2 additional fields
318311
// (m_arg1/m_arg2) which are used for the X86_64 System V ABI varargs
@@ -329,7 +322,7 @@ class LLVMDefineVisitor : public Visitor {
329322
fatal();
330323
}
331324

332-
RTTIBuilder b(structTypeInfoDecl);
325+
RTTIBuilder b(structTypeInfoType);
333326

334327
// handle opaque structs
335328
if (!sd->members) {
@@ -492,8 +485,7 @@ class LLVMDefineVisitor : public Visitor {
492485
TypeClass *tc = static_cast<TypeClass *>(decl->tinfo);
493486
DtoResolveClass(tc->sym);
494487

495-
getInterfaceTypeInfoType(); // check declaration in object.d
496-
RTTIBuilder b(Type::typeinfointerface);
488+
RTTIBuilder b(getInterfaceTypeInfoType());
497489

498490
// TypeInfo base
499491
b.push_classinfo(tc->sym);
@@ -527,8 +519,7 @@ class LLVMDefineVisitor : public Visitor {
527519
LLArrayType *arrTy = LLArrayType::get(tiTy, dim);
528520
LLConstant *arrC = LLConstantArray::get(arrTy, arrInits);
529521

530-
getTupleTypeInfoType(); // check declaration in object.d
531-
RTTIBuilder b(Type::typeinfotypelist);
522+
RTTIBuilder b(getTupleTypeInfoType());
532523

533524
// push TypeInfo[]
534525
b.push_array(arrC, dim, getTypeInfoType(), nullptr);
@@ -544,8 +535,7 @@ class LLVMDefineVisitor : public Visitor {
544535
decl->toChars());
545536
LOG_SCOPE;
546537

547-
getConstTypeInfoType(); // check declaration in object.d
548-
RTTIBuilder b(Type::typeinfoconst);
538+
RTTIBuilder b(getConstTypeInfoType());
549539
// TypeInfo base
550540
b.push_typeinfo(merge(decl->tinfo->mutableOf()));
551541
// finish
@@ -559,8 +549,7 @@ class LLVMDefineVisitor : public Visitor {
559549
decl->toChars());
560550
LOG_SCOPE;
561551

562-
getInvariantTypeInfoType(); // check declaration in object.d
563-
RTTIBuilder b(Type::typeinfoinvariant);
552+
RTTIBuilder b(getInvariantTypeInfoType());
564553
// TypeInfo base
565554
b.push_typeinfo(merge(decl->tinfo->mutableOf()));
566555
// finish
@@ -574,8 +563,7 @@ class LLVMDefineVisitor : public Visitor {
574563
decl->toChars());
575564
LOG_SCOPE;
576565

577-
getSharedTypeInfoType(); // check declaration in object.d
578-
RTTIBuilder b(Type::typeinfoshared);
566+
RTTIBuilder b(getSharedTypeInfoType());
579567
// TypeInfo base
580568
b.push_typeinfo(merge(decl->tinfo->unSharedOf()));
581569
// finish
@@ -589,8 +577,7 @@ class LLVMDefineVisitor : public Visitor {
589577
decl->toChars());
590578
LOG_SCOPE;
591579

592-
getInoutTypeInfoType(); // check declaration in object.d
593-
RTTIBuilder b(Type::typeinfowild);
580+
RTTIBuilder b(getInoutTypeInfoType());
594581
// TypeInfo base
595582
b.push_typeinfo(merge(decl->tinfo->mutableOf()));
596583
// finish
@@ -607,8 +594,7 @@ class LLVMDefineVisitor : public Visitor {
607594
assert(decl->tinfo->ty == Tvector);
608595
TypeVector *tv = static_cast<TypeVector *>(decl->tinfo);
609596

610-
getVectorTypeInfoType(); // check declaration in object.d
611-
RTTIBuilder b(Type::typeinfovector);
597+
RTTIBuilder b(getVectorTypeInfoType());
612598
// TypeInfo base
613599
b.push_typeinfo(tv->basetype);
614600
// finish

0 commit comments

Comments
 (0)