const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
- // FIXME: It isn't just virtual as written, but all virtual functions.
- if (MD->isVirtualAsWritten()) {
+ if (MD->isVirtual()) {
ErrorUnsupported(CE, "virtual dispatch");
}
return UniqueMangledName(Name.begin(), Name.end());
}
+llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) {
+ const llvm::FunctionType *FTy;
+ FTy = llvm::FunctionType::get(llvm::Type::VoidTy,
+ std::vector<const llvm::Type*>(), false);
+
+ llvm::SmallString<256> OutName;
+ llvm::raw_svector_ostream Out(OutName);
+ QualType ClassTy;
+ // FIXME: What is the design on getTagDeclType when it requires casting
+ // away const? mutable?
+ ClassTy = getContext().getTagDeclType(const_cast<CXXRecordDecl*>(RD));
+ mangleCXXVtable(ClassTy, getContext(), Out);
+ const char *Name = OutName.c_str();
+ llvm::Value *vtable = CGM.CreateRuntimeFunction(FTy, Name);
+ llvm::SmallVector<CXXMethodDecl *,32> methods;
+ typedef CXXRecordDecl::method_iterator meth_iter;
+ for (meth_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
+ ++mi) {
+ if (mi->isVirtual())
+ methods.push_back(*mi);
+ }
+
+ llvm::Type *Ptr8Ty;
+ Ptr8Ty = llvm::PointerType::get(llvm::Type::Int8Ty, 0);
+ vtable = Builder.CreateBitCast(vtable, Ptr8Ty);
+ // FIXME: finish layout for virtual bases and fix for 32-bit
+ int64_t offset = 16;
+ vtable = Builder.CreateGEP(vtable,
+ llvm::ConstantInt::get(llvm::Type::Int64Ty,
+ offset));
+ return vtable;
+}
+
/// EmitCtorPrologue - This routine generates necessary code to initialize
/// base classes and non-static data members belonging to this constructor.
void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD) {
const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
- assert(!ClassDecl->isPolymorphic()
- && "FIXME: virtual table initialization unsupported");
assert(ClassDecl->getNumVBases() == 0
&& "FIXME: virtual base initialization unsupported");
+ llvm::Value *LoadOfThis = 0;
+
for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
E = CD->init_end();
B != E; ++B) {
CXXBaseOrMemberInitializer *Member = (*B);
if (Member->isBaseInitializer()) {
- llvm::Value *LoadOfThis = LoadCXXThis();
+ LoadOfThis = LoadCXXThis();
Type *BaseType = Member->getBaseClass();
CXXRecordDecl *BaseClassDecl =
cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
assert(!getContext().getAsArrayType(FieldType)
&& "FIXME. Field arrays initialization unsupported");
- llvm::Value *LoadOfThis = LoadCXXThis();
+ LoadOfThis = LoadCXXThis();
LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
if (FieldType->getAs<RecordType>()) {
EmitStoreThroughLValue(RValue::get(RHS), LHS, FieldType);
}
}
+
+ // Initialize the vtable pointer
+ if (ClassDecl->isPolymorphic() || ClassDecl->getNumVBases()) {
+ if (!LoadOfThis)
+ LoadOfThis = LoadCXXThis();
+ llvm::Value *VtableField;
+ llvm::Type *Ptr8Ty, *PtrPtr8Ty;
+ Ptr8Ty = llvm::PointerType::get(llvm::Type::Int8Ty, 0);
+ PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
+ VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
+ llvm::Value *vtable = GenerateVtable(ClassDecl);
+ Builder.CreateStore(vtable, VtableField);
+ }
}
/// EmitDtorEpilogue - Emit all code that comes at the end of class's
bool mangle(const NamedDecl *D);
void mangleGuardVariable(const VarDecl *D);
+ void mangleCXXVtable(QualType Type);
void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type);
void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type);
mangle(D);
}
+void CXXNameMangler::mangleCXXVtable(QualType T) {
+ // <special-name> ::= TV <type> # virtual table
+ Out << "_ZTV";
+ mangleType(T);
+}
+
void CXXNameMangler::mangleGuardVariable(const VarDecl *D)
{
// <special-name> ::= GV <object name> # Guard variable for one-time
- // # initialization
+ // # initialization
Out << "_ZGV";
mangleName(D);
os.flush();
}
-
-
-}
+ void mangleCXXVtable(QualType Type, ASTContext &Context,
+ llvm::raw_ostream &os) {
+ CXXNameMangler Mangler(Context, os);
+ Mangler.mangleCXXVtable(Type);
+
+ os.flush();
+ }
+}