[cfe-commits] r89148 - in /cfe/trunk: lib/CodeGen/CGRtti.cpp test/CodeGenCXX/rtti.cpp

Mike Stump mrs at apple.com
Tue Nov 17 13:44:24 PST 2009


Author: mrs
Date: Tue Nov 17 15:44:24 2009
New Revision: 89148

URL: http://llvm.org/viewvc/llvm-project?rev=89148&view=rev
Log:
Add rtti support for non-member pointers.  WIP.

Modified:
    cfe/trunk/lib/CodeGen/CGRtti.cpp
    cfe/trunk/test/CodeGenCXX/rtti.cpp

Modified: cfe/trunk/lib/CodeGen/CGRtti.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGRtti.cpp?rev=89148&r1=89147&r2=89148&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGRtti.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGRtti.cpp Tue Nov 17 15:44:24 2009
@@ -11,8 +11,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "CodeGenModule.h"
 #include "clang/AST/RecordLayout.h"
+#include "CodeGenModule.h"
 using namespace clang;
 using namespace CodeGen;
 
@@ -46,11 +46,10 @@
     return llvm::ConstantExpr::getBitCast(C, Int8PtrTy);
   }
 
-  llvm::Constant *BuildName(const CXXRecordDecl *RD) {
+  llvm::Constant *BuildName(QualType Ty) {
     llvm::SmallString<256> OutName;
     llvm::raw_svector_ostream Out(OutName);
-    mangleCXXRttiName(CGM.getMangleContext(),
-                      CGM.getContext().getTagDeclType(RD), Out);
+    mangleCXXRttiName(CGM.getMangleContext(), Ty, Out);
 
     llvm::GlobalVariable::LinkageTypes linktype;
     linktype = llvm::GlobalValue::LinkOnceODRLinkage;
@@ -174,7 +173,7 @@
     } else
       C = BuildVtableRef("_ZTVN10__cxxabiv121__vmi_class_type_infoE");
     info.push_back(C);
-    info.push_back(BuildName(RD));
+    info.push_back(BuildName(CGM.getContext().getTagDeclType(RD)));
 
     // If we have no bases, there are no more fields.
     if (RD->getNumBases()) {
@@ -207,10 +206,6 @@
       }
     }
 
-    std::vector<const llvm::Type *> Types(info.size());
-    for (unsigned i=0; i<info.size(); ++i)
-      Types[i] = info[i]->getType();
-    // llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
     C = llvm::ConstantStruct::get(VMContext, &info[0], info.size(), false);
 
     if (GV == 0)
@@ -238,6 +233,82 @@
 #endif
   }
 
+  /// - BuildFlags - Build a __flags value for __pbase_type_info.
+  llvm::Constant *BuildInt(int f) {
+    return llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), f);
+  }
+
+  llvm::Constant *BuildType2(QualType Ty) {
+    if (const RecordType *RT = Ty.getTypePtr()->getAs<RecordType>())
+      if (const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()))
+      return Buildclass_type_info(RD);
+    return BuildType(Ty);
+  }
+
+  llvm::Constant *BuildPointerType(QualType Ty) {
+    const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
+    llvm::Constant *C;
+
+    llvm::SmallString<256> OutName;
+    llvm::raw_svector_ostream Out(OutName);
+    mangleCXXRtti(CGM.getMangleContext(), Ty, Out);
+
+    llvm::GlobalVariable *GV;
+    GV = CGM.getModule().getGlobalVariable(Out.str());
+    if (GV && !GV->isDeclaration())
+      return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
+
+    llvm::GlobalVariable::LinkageTypes linktype;
+    linktype = llvm::GlobalValue::LinkOnceODRLinkage;
+    std::vector<llvm::Constant *> info;
+
+    QualType PTy = Ty->getPointeeType();
+    // FIXME: ptr-mem data
+    QualType BTy;
+    // FIXME: ptr-mem data
+    bool PtrMem = false;
+
+    if (PtrMem)
+      C = BuildVtableRef("_ZTVN10__cxxabiv129__pointer_to_member_type_infoE");
+    else
+      C = BuildVtableRef("_ZTVN10__cxxabiv119__pointer_type_infoE");
+    info.push_back(C);
+    info.push_back(BuildName(Ty));
+    Qualifiers Q = PTy.getQualifiers();
+    PTy = CGM.getContext().getCanonicalType(PTy).getUnqualifiedType();
+    int flags = 0;
+    flags += Q.hasConst() ? 0x1 : 0;
+    flags += Q.hasVolatile() ? 0x2 : 0;
+    flags += Q.hasRestrict() ? 0x4 : 0;
+    flags += Ty.getTypePtr()->isIncompleteType() ? 0x8 : 0;
+    if (PtrMem && BTy.getTypePtr()->isIncompleteType())
+      flags += 0x10;
+      
+    info.push_back(BuildInt(flags));
+    info.push_back(BuildInt(0));
+    info.push_back(BuildType2(PTy));
+
+    if (PtrMem)
+      info.push_back(BuildType2(BTy));
+      
+    C = llvm::ConstantStruct::get(VMContext, &info[0], info.size(), false);
+
+    if (GV == 0)
+      GV = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
+                                    linktype, C, Out.str());
+    else {
+      llvm::GlobalVariable *OGV = GV;
+      GV = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
+                                    linktype, C, Out.str());
+      GV->takeName(OGV);
+      llvm::Constant *NewPtr
+        = llvm::ConstantExpr::getBitCast(GV, OGV->getType());
+      OGV->replaceAllUsesWith(NewPtr);
+      OGV->eraseFromParent();
+    }
+    return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
+  }
+
   llvm::Constant *BuildType(QualType Ty) {
     const clang::Type &Type
       = *CGM.getContext().getCanonicalType(Ty).getTypePtr();
@@ -262,9 +333,7 @@
       if (isa<BuiltinType>(PTy) && Q.empty())
         return BuildTypeRef(Ty);
 
-      assert(0 && "typeid expression");
-      const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
-      return llvm::Constant::getNullValue(Int8PtrTy);
+      return BuildPointerType(Ty);
     }
     }
   }

Modified: cfe/trunk/test/CodeGenCXX/rtti.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/rtti.cpp?rev=89148&r1=89147&r2=89148&view=diff

==============================================================================
--- cfe/trunk/test/CodeGenCXX/rtti.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/rtti.cpp Tue Nov 17 15:44:24 2009
@@ -31,6 +31,16 @@
   virtual void foo() { }
 } d1;
 
+// CHECK:     __ZTSPVi:
+// CHECK-NEXT: .asciz "PVi"
+
+// CHECK:     __ZTIPVi:
+// CHECK-NEXT: .quad  (__ZTVN10__cxxabiv119__pointer_type_infoE) + 16
+// CHECK-NEXT: .quad  __ZTSPVi
+// CHECK-NEXT: .long  2
+// CHECK-NEXT: .space 4
+// CHECK-NEXT: .quad  __ZTIi
+
 // CHECK:__ZTI7test1_D:
 // CHECK-NEXT: .quad (__ZTVN10__cxxabiv120__si_class_type_infoE) + 16
 // CHECK-NEXT: .quad __ZTS7test1_D
@@ -116,9 +126,9 @@
 // CHECK-LL-NEXT:  %vtable = load %"class.std::type_info"*** %0
 // CHECK-LL-NEXT:  %1 = getelementptr inbounds %"class.std::type_info"** %vtable, i64 -1
 // CHECK-LL-NEXT:  %2 = load %"class.std::type_info"** %1
-// CHECK-LL-NEXT:  %call = call zeroext i1 @_ZNKSt9type_infoeqERKS_(%"class.std::type_info"* %2, %"class.std::type_info"* bitcast (%1* @_ZTI7test1_D to %"class.std::type_info"*))
+// CHECK-LL-NEXT:  %call = call zeroext i1 @_ZNKSt9type_infoeqERKS_(%"class.std::type_info"* %2, %"class.std::type_info"* bitcast (%2* @_ZTI7test1_D to %"class.std::type_info"*))
 
-// CHECK-LL:       %call2 = call zeroext i1 @_ZNKSt9type_infoeqERKS_(%"class.std::type_info"* bitcast (%0* @_ZTI2NP to %"class.std::type_info"*), %"class.std::type_info"* bitcast (%1* @_ZTI7test1_D to %"class.std::type_info"*))
+// CHECK-LL:       %call2 = call zeroext i1 @_ZNKSt9type_infoeqERKS_(%"class.std::type_info"* bitcast (%0* @_ZTI2NP to %"class.std::type_info"*), %"class.std::type_info"* bitcast (%2* @_ZTI7test1_D to %"class.std::type_info"*))
 
 // CHECK-LL:       %3 = bitcast %class.test1_B7* %tmp5 to %"class.std::type_info"***
 // CHECK-LL-NEXT:  %4 = icmp ne %"class.std::type_info"*** %3, null
@@ -130,8 +140,14 @@
 // CHECK-LL-NEXT:  %vtable6 = load %"class.std::type_info"*** %3
 // CHECK-LL-NEXT:  %7 = getelementptr inbounds %"class.std::type_info"** %vtable6, i64 -1
 // CHECK-LL-NEXT:  %8 = load %"class.std::type_info"** %7
-// CHECK-LL-NEXT:  %call7 = call zeroext i1 @_ZNKSt9type_infoeqERKS_(%"class.std::type_info"* %8, %"class.std::type_info"* bitcast (%1* @_ZTI7test1_D to %"class.std::type_info"*))
+// CHECK-LL-NEXT:  %call7 = call zeroext i1 @_ZNKSt9type_infoeqERKS_(%"class.std::type_info"* %8, %"class.std::type_info"* bitcast (%2* @_ZTI7test1_D to %"class.std::type_info"*))
 
 // CHECK-LL:       %call10 = call zeroext i1 @_ZNKSt9type_infoeqERKS_(%"class.std::type_info"* bitcast (i8** @_ZTIi to %"class.std::type_info"*), %"class.std::type_info"* bitcast (i8** @_ZTIf to %"class.std::type_info"*))
 
 // CHECK-LL:       %call13 = call zeroext i1 @_ZNKSt9type_infoeqERKS_(%"class.std::type_info"* bitcast (i8** @_ZTIPi to %"class.std::type_info"*), %"class.std::type_info"* bitcast (i8** @_ZTIPKi to %"class.std::type_info"*))
+
+int test3() {
+  if (typeid(volatile int *) == typeid(int *))
+    return 1;
+  return 0;
+}





More information about the cfe-commits mailing list