From 6da09c01b3e34baae224185b52fe8c4ad0bdaec4 Mon Sep 17 00:00:00 2001 From: Tomas Lindquist Olsen Date: Sun, 18 Nov 2007 08:25:07 +0100 Subject: [PATCH] [svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable) --- dmd/mtype.c | 23 ++++++++++++++++++++--- gen/classes.cpp | 36 +++++++++++++++++++++++++++--------- gen/irstate.cpp | 10 +--------- gen/irstate.h | 1 - gen/tollvm.cpp | 18 ++++++++---------- gen/toobj.cpp | 10 ++++++++++ llvmdc.kdevelop.filelist | 1 + test/classinfo2.d | 17 +++++++++++++++++ 8 files changed, 84 insertions(+), 32 deletions(-) create mode 100644 test/classinfo2.d diff --git a/dmd/mtype.c b/dmd/mtype.c index f778f11e..e74f6b6c 100644 --- a/dmd/mtype.c +++ b/dmd/mtype.c @@ -4502,9 +4502,24 @@ L1: e->type = t; // do this so we don't get redundant dereference } else - { /* For class objects, the classinfo reference is the first - * entry in the vtbl[] - */ + { + /* For class objects, the classinfo reference is the first + * entry in the vtbl[] + */ +#if IN_LLVM + + e = e->castTo(sc, t->pointerTo()->pointerTo()); + e = new PtrExp(e->loc, e); + e->type = t->pointerTo(); + e = new PtrExp(e->loc, e); + e->type = t; + if (sym->isInterfaceDeclaration()) + { + assert(0 && "No interfaces yet!"); + } + +#else + e = new PtrExp(e->loc, e); e->type = t->pointerTo(); if (sym->isInterfaceDeclaration()) @@ -4526,6 +4541,8 @@ L1: e->type = t->pointerTo(); } e = new PtrExp(e->loc, e, t); + +#endif } return e; } diff --git a/gen/classes.cpp b/gen/classes.cpp index 99e6b9a5..e3010221 100644 --- a/gen/classes.cpp +++ b/gen/classes.cpp @@ -46,6 +46,10 @@ void DtoResolveClass(ClassDeclaration* cd) if (cd->baseClass) { DtoResolveClass(cd->baseClass); } + // resolve typeinfo + //DtoResolveClass(ClassDeclaration::typeinfo); + // resolve classinfo + //DtoResolveClass(ClassDeclaration::classinfo); Logger::println("DtoResolveClass(%s)", cd->toPrettyChars()); LOG_SCOPE; @@ -89,7 +93,10 @@ void DtoResolveClass(ClassDeclaration* cd) llvm::cast(spa.get())->refineAbstractTypeTo(structtype); structtype = isaStruct(spa.get()); - ts->llvmType = new llvm::PATypeHolder(structtype); + if (!ts->llvmType) + ts->llvmType = new llvm::PATypeHolder(structtype); + else + *ts->llvmType = structtype; bool needs_definition = false; if (cd->parent->isModule()) { @@ -118,7 +125,17 @@ void DtoResolveClass(ClassDeclaration* cd) sinits_ty.push_back(fpty); } else if (ClassDeclaration* cd = dsym->isClassDeclaration()) { - const llvm::Type* cty = llvm::PointerType::get(llvm::Type::Int8Ty); + //Logger::println("*** ClassDeclaration in vtable: %s", cd->toChars()); + const llvm::Type* cinfoty; + if (cd != ClassDeclaration::classinfo) { + cd = ClassDeclaration::classinfo; + DtoResolveClass(cd); + cinfoty = cd->type->llvmType->get(); + } + else { + cinfoty = ts->llvmType->get(); + } + const llvm::Type* cty = llvm::PointerType::get(cd->type->llvmType->get()); sinits_ty.push_back(cty); } else @@ -192,6 +209,9 @@ void DtoDeclareClass(ClassDeclaration* cd) gIR->constInitList.push_back(cd); if (needs_definition) gIR->defineList.push_back(cd); + + // classinfo + DtoDeclareClassInfo(cd); } ////////////////////////////////////////////////////////////////////////////////////////// @@ -236,7 +256,7 @@ void DtoConstInitClass(ClassDeclaration* cd) const llvm::StructType* structtype = isaStruct(ts->llvmType->get()); // generate initializer - Logger::cout() << cd->toPrettyChars() << " | " << *structtype << '\n'; + /*Logger::cout() << cd->toPrettyChars() << " | " << *structtype << '\n'; for(size_t i=0; igetNumElements(); ++i) { Logger::cout() << "s#" << i << " = " << *structtype->getElementType(i) << '\n'; @@ -244,7 +264,7 @@ void DtoConstInitClass(ClassDeclaration* cd) for(size_t i=0; i