Add typeid for the builtin types.  WIP.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@89028 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGCXXExpr.cpp b/lib/CodeGen/CGCXXExpr.cpp
index 75740af..71ddca7 100644
--- a/lib/CodeGen/CGCXXExpr.cpp
+++ b/lib/CodeGen/CGCXXExpr.cpp
@@ -347,7 +347,7 @@
   QualType Ty = E->getType();
   const llvm::Type *LTy = ConvertType(Ty)->getPointerTo();
   if (E->isTypeOperand()) {
-    QualType Ty = E->getTypeOperand();
+    Ty = E->getTypeOperand();
     CanQualType CanTy = CGM.getContext().getCanonicalType(Ty);
     Ty = CanTy.getUnqualifiedType().getNonReferenceType();
     if (const RecordType *RT = Ty->getAs<RecordType>()) {
@@ -356,9 +356,7 @@
         return Builder.CreateBitCast(CGM.GenerateRttiRef(RD), LTy);
       return Builder.CreateBitCast(CGM.GenerateRtti(RD), LTy);
     }
-    // FIXME: return the rtti for the non-class static type.
-    ErrorUnsupported(E, "typeid expression");
-    return 0;
+    return Builder.CreateBitCast(CGM.GenerateRttiNonClass(Ty), LTy);
   }
   Expr *subE = E->getExprOperand();
   if (const RecordType *RT = Ty->getAs<RecordType>()) {
@@ -397,11 +395,12 @@
       V = Builder.CreateLoad(V);
       return V;
     }      
-    return CGM.GenerateRtti(RD);
+    return Builder.CreateBitCast(CGM.GenerateRtti(RD), LTy);
   }
-  // FIXME: return rtti for the non-class static type.
-  ErrorUnsupported(E, "typeid expression");
-  return 0;
+  Ty = subE->getType();
+  CanQualType CanTy = CGM.getContext().getCanonicalType(Ty);
+  Ty = CanTy.getUnqualifiedType().getNonReferenceType();
+  return Builder.CreateBitCast(CGM.GenerateRttiNonClass(Ty), LTy);
 }
 
 llvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *V,
diff --git a/lib/CodeGen/CGRtti.cpp b/lib/CodeGen/CGRtti.cpp
index ce8f2c8..5ac2095 100644
--- a/lib/CodeGen/CGRtti.cpp
+++ b/lib/CodeGen/CGRtti.cpp
@@ -76,7 +76,7 @@
     return llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), c);
   }
 
-  llvm::Constant *Buildclass_type_infoRef(const CXXRecordDecl *RD) {
+  llvm::Constant *BuildTypeRef(QualType Ty) {
     const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
     llvm::Constant *C;
 
@@ -85,8 +85,7 @@
 
     llvm::SmallString<256> OutName;
     llvm::raw_svector_ostream Out(OutName);
-    mangleCXXRtti(CGM.getMangleContext(), CGM.getContext().getTagDeclType(RD),
-                  Out);
+    mangleCXXRtti(CGM.getMangleContext(), Ty, Out);
 
     C = CGM.getModule().getGlobalVariable(Out.str());
     if (C)
@@ -100,6 +99,10 @@
     return llvm::ConstantExpr::getBitCast(C, Int8PtrTy);
   }
 
+  llvm::Constant *Buildclass_type_infoRef(const CXXRecordDecl *RD) {
+    return BuildTypeRef(CGM.getContext().getTagDeclType(RD));
+  }
+
   /// CalculateFlags - Calculate the flags for the __vmi_class_type_info
   /// datastructure.  1 for non-diamond repeated inheritance, 2 for a dimond
   /// shaped class.
@@ -234,6 +237,24 @@
     return Rtti;
 #endif
   }
+
+  llvm::Constant *BuildType(QualType Ty) {
+    const clang::Type &Type
+      = *CGM.getContext().getCanonicalType(Ty).getTypePtr();
+    switch (Type.getTypeClass()) {
+    default: {
+      // FIXME: Add all the missing types, such as pointer, array...
+      assert(0 && "typeid expression");
+      const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
+      return llvm::Constant::getNullValue(Int8PtrTy);
+    }
+
+    case Type::Builtin: {
+      // We expect all type_info objects for builtin types to be in the library.
+      return BuildTypeRef(Ty);
+    }
+    }
+  }
 };
 
 llvm::Constant *CodeGenModule::GenerateRttiRef(const CXXRecordDecl *RD) {
@@ -247,3 +268,9 @@
 
   return b.Buildclass_type_info(RD);
 }
+
+llvm::Constant *CodeGenModule::GenerateRttiNonClass(QualType Ty) {
+  RttiBuilder b(*this);
+
+  return b.BuildType(Ty);
+}
diff --git a/lib/CodeGen/CodeGenModule.h b/lib/CodeGen/CodeGenModule.h
index 85e4d3d..c8562d6 100644
--- a/lib/CodeGen/CodeGenModule.h
+++ b/lib/CodeGen/CodeGenModule.h
@@ -229,6 +229,9 @@
   /// GenerateRttiRef - Generate a reference to the rtti information for the
   /// given type.
   llvm::Constant *GenerateRttiRef(const CXXRecordDecl *RD);
+  /// GenerateRttiNonClass - Generate the rtti information for the given
+  /// non-class type.
+  llvm::Constant *GenerateRttiNonClass(QualType Ty);
 
   /// BuildThunk - Build a thunk for the given method
   llvm::Constant *BuildThunk(const CXXMethodDecl *MD, bool Extern, int64_t nv,