Add beginnigs of rtti generation, wire up more of -fno-exceptions.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@77751 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGCXX.cpp b/lib/CodeGen/CGCXX.cpp
index 62dabf7..356d318 100644
--- a/lib/CodeGen/CGCXX.cpp
+++ b/lib/CodeGen/CGCXX.cpp
@@ -490,10 +490,42 @@
   return UniqueMangledName(Name.begin(), Name.end());
 }
 
+llvm::Constant *CodeGenFunction::GenerateRtti(const CXXRecordDecl *RD) {
+  llvm::Type *Ptr8Ty;
+  Ptr8Ty = llvm::PointerType::get(llvm::Type::Int8Ty, 0);
+  llvm::Constant *rtti = llvm::Constant::getNullValue(Ptr8Ty);
+
+  if (!getContext().getLangOptions().Rtti)
+    return rtti;
+
+  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));
+  mangleCXXRtti(ClassTy, getContext(), Out);
+  const char *Name = OutName.c_str();
+  llvm::GlobalVariable::LinkageTypes linktype;
+  linktype = llvm::GlobalValue::WeakAnyLinkage;
+  std::vector<llvm::Constant *> info;
+  assert (0 && "FIXME: implement rtti descriptor");
+  // FIXME: descriptor
+  info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
+  assert (0 && "FIXME: implement rtti ts");
+  // FIXME: TS
+  info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
+
+  llvm::Constant *C;
+  llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, info.size());
+  C = llvm::ConstantArray::get(type, info);
+  rtti = new llvm::GlobalVariable(CGM.getModule(), type, true, linktype, C,
+                                  Name);
+  rtti = llvm::ConstantExpr::getBitCast(rtti, Ptr8Ty);
+  return rtti;
+}
+
 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;
@@ -512,7 +544,7 @@
   m = llvm::Constant::getNullValue(Ptr8Ty);
   int64_t offset = 0;
   methods.push_back(m); offset += LLVMPointerWidth;
-  methods.push_back(m); offset += LLVMPointerWidth;
+  methods.push_back(GenerateRtti(RD)); offset += LLVMPointerWidth;
   for (meth_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
        ++mi) {
     if (mi->isVirtual()) {
@@ -526,7 +558,6 @@
   C = llvm::ConstantArray::get(type, methods);
   llvm::Value *vtable = new llvm::GlobalVariable(CGM.getModule(), type, true,
                                                  linktype, C, Name);
-  // CGM.CreateRuntimeFunction(FTy, Name);
   vtable = Builder.CreateBitCast(vtable, Ptr8Ty);
   // FIXME: finish layout for virtual bases
   vtable = Builder.CreateGEP(vtable,
diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h
index 5661343..b814ed7 100644
--- a/lib/CodeGen/CodeGenFunction.h
+++ b/lib/CodeGen/CodeGenFunction.h
@@ -358,6 +358,7 @@
   /// legal to call this function even if there is no current insertion point.
   void FinishFunction(SourceLocation EndLoc=SourceLocation());
 
+  llvm::Constant *GenerateRtti(const CXXRecordDecl *RD);
   llvm::Value *GenerateVtable(const CXXRecordDecl *RD);
 
   void EmitCtorPrologue(const CXXConstructorDecl *CD);
diff --git a/lib/CodeGen/Mangle.cpp b/lib/CodeGen/Mangle.cpp
index 36ee2a4..341e230 100644
--- a/lib/CodeGen/Mangle.cpp
+++ b/lib/CodeGen/Mangle.cpp
@@ -42,6 +42,7 @@
     void mangleGuardVariable(const VarDecl *D);
     
     void mangleCXXVtable(QualType Type);
+    void mangleCXXRtti(QualType Type);
     void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type);
     void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type);
 
@@ -165,6 +166,12 @@
   mangleType(T);
 }
 
+void CXXNameMangler::mangleCXXRtti(QualType T) {
+  // <special-name> ::= TI <type>  # typeinfo structure
+  Out << "_ZTI";
+  mangleType(T);
+}
+
 void CXXNameMangler::mangleGuardVariable(const VarDecl *D)
 {
   //  <special-name> ::= GV <object name>	# Guard variable for one-time 
@@ -822,4 +829,12 @@
 
     os.flush();
   }
+
+  void mangleCXXRtti(QualType Type, ASTContext &Context,
+                     llvm::raw_ostream &os) {
+    CXXNameMangler Mangler(Context, os);
+    Mangler.mangleCXXRtti(Type);
+
+    os.flush();
+  }
 }
diff --git a/lib/CodeGen/Mangle.h b/lib/CodeGen/Mangle.h
index 1a5ca63..8f0a32d 100644
--- a/lib/CodeGen/Mangle.h
+++ b/lib/CodeGen/Mangle.h
@@ -37,6 +37,7 @@
   void mangleGuardVariable(const VarDecl *D, ASTContext &Context,
                            llvm::raw_ostream &os);
   void mangleCXXVtable(QualType T, ASTContext &Context, llvm::raw_ostream &os);
+  void mangleCXXRtti(QualType T, ASTContext &Context, llvm::raw_ostream &os);
   void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
                      ASTContext &Context, llvm::raw_ostream &os);
   void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp
index e3612c7..8158b72 100644
--- a/lib/Driver/Tools.cpp
+++ b/lib/Driver/Tools.cpp
@@ -480,7 +480,6 @@
                       
   // Forward -f options which we can pass directly.
   Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
-  Args.AddLastArg(CmdArgs, options::OPT_fexceptions);
   Args.AddLastArg(CmdArgs, options::OPT_ffreestanding);
   Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
   Args.AddLastArg(CmdArgs, options::OPT_fgnu_runtime);
@@ -530,6 +529,20 @@
       CmdArgs.push_back("-fblocks=0");
   }
 
+  // -fexceptions default varies depending on platform and language; only
+  // pass if specified.
+ if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
+                               options::OPT_fno_exceptions)) {
+    if (A->getOption().matches(options::OPT_fexceptions))
+      CmdArgs.push_back("-fexceptions");
+    else
+      CmdArgs.push_back("-fexceptions=0");
+  }
+
+  // -frtti is default, only pass non-default.
+  if (!Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti))
+    CmdArgs.push_back("-frtti=0");
+
   // -fsigned-char/-funsigned-char default varies depending on platform; only
   // pass if specified.
   if (Arg *A = Args.getLastArg(options::OPT_fsigned_char,
@@ -1669,6 +1682,7 @@
         // Derived from darwin_iphoneos_libgcc spec.
         CmdArgs.push_back("-lgcc_s.10.5");
       } else if (Args.hasArg(options::OPT_shared_libgcc) ||
+                 // FIXME: -fexceptions -fno-exceptions means no exceptions
                  Args.hasArg(options::OPT_fexceptions) ||
                  Args.hasArg(options::OPT_fgnu_runtime)) {
         // FIXME: This is probably broken on 10.3?