]> granicus.if.org Git - clang/commitdiff
Handle pure virtual member functions.
authorAnders Carlsson <andersca@mac.com>
Mon, 29 Mar 2010 05:40:50 +0000 (05:40 +0000)
committerAnders Carlsson <andersca@mac.com>
Mon, 29 Mar 2010 05:40:50 +0000 (05:40 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@99807 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGVtable.cpp

index 2f725df8148fb4a47210d38ff36b699d3d1ffcf2..1283c69d97c8fb8c95f623e553d80900c7e739ab 100644 (file)
@@ -4049,6 +4049,8 @@ CodeGenVTables::CreateVTableInitializer(const CXXRecordDecl *RD,
   
   unsigned NextVTableThunkIndex = 0;
   
+  llvm::Constant* PureVirtualFn = 0;
+
   for (unsigned I = 0; I != NumComponents; ++I) {
     VtableComponent Component = 
       VtableComponent::getFromOpaqueInteger(Components[I]);
@@ -4091,22 +4093,37 @@ CodeGenVTables::CreateVTableInitializer(const CXXRecordDecl *RD,
         break;
       }
 
-      // Check if we should use a thunk.
-      if (NextVTableThunkIndex < VTableThunks.size() &&
-          VTableThunks[NextVTableThunkIndex].first == I) {
-        const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second;
-        
-        Init = CGM.GetAddrOfThunk(GD, Thunk);
+      if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) {
+        // We have a pure virtual member function.
+        if (!PureVirtualFn) {
+          const llvm::FunctionType *Ty = 
+            llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()), 
+                                    /*isVarArg=*/false);
+          PureVirtualFn = 
+            CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual");
+          PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn, 
+                                                         Int8PtrTy);
+        }
         
-        NextVTableThunkIndex++;
+        Init = PureVirtualFn;
       } else {
-        const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
-        const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVtable(MD);
+        // Check if we should use a thunk.
+        if (NextVTableThunkIndex < VTableThunks.size() &&
+            VTableThunks[NextVTableThunkIndex].first == I) {
+          const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second;
+        
+          Init = CGM.GetAddrOfThunk(GD, Thunk);
+        
+          NextVTableThunkIndex++;
+        } else {
+          const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
+          const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVtable(MD);
         
-        Init = CGM.GetAddrOfFunction(GD, Ty);
+          Init = CGM.GetAddrOfFunction(GD, Ty);
+        }
+
+        Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
       }
-      
-      Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
       break;
     }
 
@@ -4137,11 +4154,8 @@ GetGlobalVariable(llvm::Module &Module, llvm::StringRef Name,
   
   if (GV) {
     // Check if the variable has the right type.
-    if (GV->getType()->getElementType() == Ty) {
-      // Set the correct linkage.
-      GV->setLinkage(Linkage);
+    if (GV->getType()->getElementType() == Ty)
       return GV;
-    }
 
     assert(GV->isDeclaration() && "Declaration has wrong type!");
     
@@ -4221,6 +4235,9 @@ CodeGenVTables::EmitVTableDefinition(llvm::GlobalVariable *VTable,
     CreateVTableInitializer(RD, getVTableComponentsData(RD),
                             getNumVTableComponents(RD), Thunks);
   VTable->setInitializer(Init);
+  
+  // Set the correct linkage.
+  VTable->setLinkage(Linkage);
 }
 
 llvm::GlobalVariable *