[cfe-commits] r69343 - in /cfe/trunk/lib/CodeGen: CGCXX.cpp CodeGenModule.cpp CodeGenModule.h Mangle.cpp Mangle.h

Anders Carlsson andersca at mac.com
Thu Apr 16 18:58:57 PDT 2009


Author: andersca
Date: Thu Apr 16 20:58:57 2009
New Revision: 69343

URL: http://llvm.org/viewvc/llvm-project?rev=69343&view=rev
Log:
Add support for generating (very basic) C++ destructors. These aren't called by anything yet.

Modified:
    cfe/trunk/lib/CodeGen/CGCXX.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.h
    cfe/trunk/lib/CodeGen/Mangle.cpp
    cfe/trunk/lib/CodeGen/Mangle.h

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGCXX.cpp Thu Apr 16 20:58:57 2009
@@ -124,27 +124,6 @@
   return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
 }
 
-const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D, 
-                                                 CXXCtorType Type) {
-  llvm::SmallString<256> Name;
-  llvm::raw_svector_ostream Out(Name);
-  mangleCXXCtor(D, Type, Context, Out);
-
-  Name += '\0';
-  return UniqueMangledName(Name.begin(), Name.end());
-}
-
-void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D, 
-                                       CXXCtorType Type) {
-  
-  llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
- 
-  CodeGenFunction(*this).GenerateCode(D, Fn);
-  
-  SetFunctionDefinitionAttributes(D, Fn);
-  SetLLVMFunctionAttributesForDefinition(D, Fn);
-}
-
 void
 CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, 
                                         CXXCtorType Type, 
@@ -190,10 +169,8 @@
                          E->arg_begin(), E->arg_end());
 }
 
-static bool canGenerateCXXConstructor(const CXXConstructorDecl *D, 
-                                      ASTContext &Context) {
-  const CXXRecordDecl *RD = D->getParent();
-  
+static bool canGenerateCXXstructor(const CXXRecordDecl *RD, 
+                                   ASTContext &Context) {
   // The class has base classes - we don't support that right now.
   if (RD->getNumBases() > 0)
     return false;
@@ -209,7 +186,7 @@
 }
 
 void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
-  if (!canGenerateCXXConstructor(D, getContext())) {
+  if (!canGenerateCXXstructor(D->getParent(), getContext())) {
     ErrorUnsupported(D, "C++ constructor", true);
     return;
   }
@@ -218,6 +195,17 @@
   EmitCXXConstructor(D, Ctor_Base);
 }
 
+void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D, 
+                                       CXXCtorType Type) {
+  
+  llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
+  
+  CodeGenFunction(*this).GenerateCode(D, Fn);
+  
+  SetFunctionDefinitionAttributes(D, Fn);
+  SetLLVMFunctionAttributesForDefinition(D, Fn);
+}
+
 llvm::Function *
 CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D, 
                                        CXXCtorType Type) {
@@ -227,3 +215,54 @@
   const char *Name = getMangledCXXCtorName(D, Type);
   return cast<llvm::Function>(GetOrCreateLLVMFunction(Name, FTy, D));
 }
+
+const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D, 
+                                                 CXXCtorType Type) {
+  llvm::SmallString<256> Name;
+  llvm::raw_svector_ostream Out(Name);
+  mangleCXXCtor(D, Type, Context, Out);
+  
+  Name += '\0';
+  return UniqueMangledName(Name.begin(), Name.end());
+}
+
+void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
+  if (!canGenerateCXXstructor(D->getParent(), getContext())) {
+    ErrorUnsupported(D, "C++ destructor", true);
+    return;
+  }
+  
+  EmitCXXDestructor(D, Dtor_Complete);
+  EmitCXXDestructor(D, Dtor_Base);
+}
+
+void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D, 
+                                      CXXDtorType Type) {
+  llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
+  
+  CodeGenFunction(*this).GenerateCode(D, Fn);
+  
+  SetFunctionDefinitionAttributes(D, Fn);
+  SetLLVMFunctionAttributesForDefinition(D, Fn);
+}
+
+llvm::Function *
+CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D, 
+                                      CXXDtorType Type) {
+  const llvm::FunctionType *FTy =
+    getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
+  
+  const char *Name = getMangledCXXDtorName(D, Type);
+  return cast<llvm::Function>(GetOrCreateLLVMFunction(Name, FTy, D));
+}
+
+const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D, 
+                                                 CXXDtorType Type) {
+  llvm::SmallString<256> Name;
+  llvm::raw_svector_ostream Out(Name);
+  mangleCXXDtor(D, Type, Context, Out);
+  
+  Name += '\0';
+  return UniqueMangledName(Name.begin(), Name.end());
+}
+

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Thu Apr 16 20:58:57 2009
@@ -1367,6 +1367,9 @@
   case Decl::CXXConstructor:
     EmitCXXConstructors(cast<CXXConstructorDecl>(D));
     break;
+  case Decl::CXXDestructor:
+    EmitCXXDestructors(cast<CXXDestructorDecl>(D));
+    break;
         
   // Objective-C Decls
     

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=69343&r1=69342&r2=69343&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.h Thu Apr 16 20:58:57 2009
@@ -56,6 +56,7 @@
   class CompileOptions;
   class Diagnostic;
   class AnnotateAttr;
+  class CXXDestructorDecl;
 
 namespace CodeGen {
 
@@ -240,6 +241,11 @@
   /// given type.
   llvm::Function *GetAddrOfCXXConstructor(const CXXConstructorDecl *D, 
                                           CXXCtorType Type);
+
+  /// GetAddrOfCXXDestructor - Return the address of the constructor of the
+  /// given type.
+  llvm::Function *GetAddrOfCXXDestructor(const CXXDestructorDecl *D, 
+                                         CXXDtorType Type);
   
   /// getBuiltinLibFunction - Given a builtin id for a function like
   /// "__builtin_fabsf", return a Function* for "fabsf".
@@ -331,6 +337,8 @@
   const char *getMangledName(const NamedDecl *ND);
   const char *getMangledCXXCtorName(const CXXConstructorDecl *D, 
                                     CXXCtorType Type);
+  const char *getMangledCXXDtorName(const CXXDestructorDecl *D, 
+                                    CXXDtorType Type);
   
   enum GVALinkage {
     GVA_Internal,
@@ -392,6 +400,14 @@
   /// a C++ constructor Decl.
   void EmitCXXConstructor(const CXXConstructorDecl *D, CXXCtorType Type);
   
+  /// EmitCXXDestructors - Emit destructors (base, complete) from a 
+  /// C++ destructor Decl.
+  void EmitCXXDestructors(const CXXDestructorDecl *D);
+  
+  /// EmitCXXDestructor - Emit a single destructor with the given type from
+  /// a C++ destructor Decl.
+  void EmitCXXDestructor(const CXXDestructorDecl *D, CXXDtorType Type);
+  
   // FIXME: Hardcoding priority here is gross.
   void AddGlobalCtor(llvm::Function * Ctor, int Priority=65535);
   void AddGlobalDtor(llvm::Function * Dtor, int Priority=65535);

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

==============================================================================
--- cfe/trunk/lib/CodeGen/Mangle.cpp (original)
+++ cfe/trunk/lib/CodeGen/Mangle.cpp Thu Apr 16 20:58:57 2009
@@ -29,16 +29,19 @@
     ASTContext &Context;
     llvm::raw_ostream &Out;
 
-    const CXXConstructorDecl *Ctor;
+    const CXXMethodDecl *Structor;
+    unsigned StructorType;
     CXXCtorType CtorType;
     
   public:
     CXXNameMangler(ASTContext &C, llvm::raw_ostream &os)
-      : Context(C), Out(os), Ctor(0), CtorType(Ctor_Complete) { }
+      : Context(C), Out(os), Structor(0), StructorType(0) { }
 
     bool mangle(const NamedDecl *D);
     void mangleGuardVariable(const VarDecl *D);
+    
     void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type);
+    void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type);
 
   private:
     bool mangleFunctionDecl(const FunctionDecl *FD);
@@ -63,6 +66,7 @@
     void mangleType(const ObjCInterfaceType *T);
     void mangleExpression(Expr *E);
     void mangleCXXCtorType(CXXCtorType T);
+    void mangleCXXDtorType(CXXDtorType T);
   };
 }
 
@@ -132,9 +136,18 @@
 
 void CXXNameMangler::mangleCXXCtor(const CXXConstructorDecl *D, 
                                    CXXCtorType Type) {
-  assert(!Ctor && "Ctor already set!");
-  Ctor = D;
-  CtorType = Type;
+  assert(!Structor && "Structor already set!");
+  Structor = D;
+  StructorType = Type;
+  
+  mangle(D);
+}
+
+void CXXNameMangler::mangleCXXDtor(const CXXDestructorDecl *D, 
+                                   CXXDtorType Type) {
+  assert(!Structor && "Structor already set!");
+  Structor = D;
+  StructorType = Type;
   
   mangle(D);
 }
@@ -198,10 +211,10 @@
     break;
 
   case DeclarationName::CXXConstructorName:
-    if (ND == Ctor)
+    if (ND == Structor)
       // If the named decl is the C++ constructor we're mangling, use the 
       // type we were given.
-      mangleCXXCtorType(CtorType);
+      mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
     else
       // Otherwise, use the complete constructor name. This is relevant if a
       // class with a constructor is declared within a constructor.
@@ -209,13 +222,14 @@
     break;
 
   case DeclarationName::CXXDestructorName:
-    // <ctor-dtor-name> ::= D0  # deleting destructor
-    //                  ::= D1  # complete object destructor
-    //                  ::= D2  # base object destructor
-    //
-    // FIXME: We don't even have all of these destructors in the AST
-    // yet.
-    Out << "D0";
+    if (ND == Structor)
+      // If the named decl is the C++ destructor we're mangling, use the 
+      // type we were given.
+      mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
+    else
+      // Otherwise, use the complete destructor name. This is relevant if a
+      // class with a destructor is declared within a destructor.
+      mangleCXXDtorType(Dtor_Complete);
     break;
 
   case DeclarationName::CXXConversionFunctionName:
@@ -612,6 +626,24 @@
   }
 }
 
+void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
+  // <ctor-dtor-name> ::= D0  # deleting destructor
+  //                  ::= D1  # complete object destructor
+  //                  ::= D2  # base object destructor
+  //
+  switch (T) {
+  case Dtor_Deleting:
+    Out << "D0";
+    break;
+  case Dtor_Complete:
+    Out << "D1";
+    break;
+  case Dtor_Base:
+    Out << "D2";
+    break;
+  }
+}
+
 namespace clang {
   /// \brief Mangles the name of the declaration D and emits that name
   /// to the given output stream.
@@ -648,5 +680,15 @@
     
     os.flush();
   }
+  
+  void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
+                     ASTContext &Context, llvm::raw_ostream &os) {
+    CXXNameMangler Mangler(Context, os);
+    Mangler.mangleCXXDtor(D, Type);
+    
+    os.flush();
+  }
+  
+  
 }
 

Modified: cfe/trunk/lib/CodeGen/Mangle.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/Mangle.h?rev=69343&r1=69342&r2=69343&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/Mangle.h (original)
+++ cfe/trunk/lib/CodeGen/Mangle.h Thu Apr 16 20:58:57 2009
@@ -27,6 +27,7 @@
 namespace clang {
   class ASTContext;
   class CXXConstructorDecl;
+  class CXXDestructorDecl;
   class NamedDecl;
   class VarDecl;
   
@@ -36,6 +37,8 @@
                            llvm::raw_ostream &os);
   void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
                      ASTContext &Context, llvm::raw_ostream &os);
+  void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
+                     ASTContext &Context, llvm::raw_ostream &os);
 }
 
 #endif 





More information about the cfe-commits mailing list