From 37d065680b292e871a2053c582832232082648d2 Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Thu, 3 Sep 2009 23:18:17 +0000 Subject: [PATCH] Patch to instantiate destructors used to destruct base and data members when they are needed. llvm-svn: 80967 --- clang/include/clang/AST/DeclCXX.h | 25 ++++--- clang/lib/AST/DeclCXX.cpp | 61 --------------- clang/lib/Sema/Sema.h | 6 ++ clang/lib/Sema/SemaDecl.cpp | 2 +- clang/lib/Sema/SemaDeclCXX.cpp | 75 +++++++++++++++++++ clang/lib/Sema/SemaExprCXX.cpp | 4 + .../test/CodeGenCXX/constructor-template.cpp | 7 ++ 7 files changed, 109 insertions(+), 71 deletions(-) diff --git a/clang/include/clang/AST/DeclCXX.h b/clang/include/clang/AST/DeclCXX.h index 37666d4c7590..ce6e925a9a6f 100644 --- a/clang/include/clang/AST/DeclCXX.h +++ b/clang/include/clang/AST/DeclCXX.h @@ -1095,12 +1095,13 @@ public: /// }; /// @endcode class CXXDestructorDecl : public CXXMethodDecl { +public: enum KindOfObjectToDestroy { VBASE = 0x1, DRCTNONVBASE = 0x2, ANYBASE = 0x3 }; - +private: /// ImplicitlyDefined - Whether this destructor was implicitly /// defined by the compiler. When false, the destructor was defined /// by the user. In C++03, this flag will have the same value as @@ -1181,12 +1182,24 @@ public: return NumBaseOrMemberDestructions; } - /// getBaseOrMember - get the generic 'member' representing either the field - /// or a base class. + /// setNumBaseOrMemberDestructions - Set number of base and non-static members + /// to destroy. + void setNumBaseOrMemberDestructions(unsigned numBaseOrMemberDestructions) { + NumBaseOrMemberDestructions = numBaseOrMemberDestructions; + } + + /// getBaseOrMemberToDestroy - get the generic 'member' representing either + /// the field or a base class. uintptr_t* getBaseOrMemberToDestroy() const { return BaseOrMemberDestructions; } + /// setBaseOrMemberToDestroy - set the generic 'member' representing either + /// the field or a base class. + void setBaseOrMemberDestructions(uintptr_t* baseOrMemberDestructions) { + BaseOrMemberDestructions = baseOrMemberDestructions; + } + /// isVbaseToDestroy - returns true, if object is virtual base. bool isVbaseToDestroy(uintptr_t Vbase) const { return (Vbase & VBASE) != 0; @@ -1229,12 +1242,6 @@ public: return reinterpret_cast(Base & ~0x02); return 0; } - - /// computeBaseOrMembersToDestroy - Compute information in current - /// destructor decl's AST of bases and non-static data members which will be - /// implicitly destroyed. We are storing the destruction in the order that - /// they should occur (which is the reverse of construction order). - void computeBaseOrMembersToDestroy(ASTContext &C); // Implement isa/cast/dyncast/etc. static bool classof(const Decl *D) { diff --git a/clang/lib/AST/DeclCXX.cpp b/clang/lib/AST/DeclCXX.cpp index 036752f5ca87..29a7c7fad9e4 100644 --- a/clang/lib/AST/DeclCXX.cpp +++ b/clang/lib/AST/DeclCXX.cpp @@ -522,67 +522,6 @@ CXXDestructorDecl::Destroy(ASTContext& C) { CXXMethodDecl::Destroy(C); } -void -CXXDestructorDecl::computeBaseOrMembersToDestroy(ASTContext &C) { - CXXRecordDecl *ClassDecl = cast(getDeclContext()); - llvm::SmallVector AllToDestruct; - - for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), - E = ClassDecl->vbases_end(); VBase != E; ++VBase) { - if (VBase->getType()->isDependentType()) - continue; - // Skip over virtual bases which have trivial destructors. - CXXRecordDecl *BaseClassDecl - = cast(VBase->getType()->getAs()->getDecl()); - if (BaseClassDecl->hasTrivialDestructor()) - continue; - uintptr_t Member = - reinterpret_cast(VBase->getType().getTypePtr()) | VBASE; - AllToDestruct.push_back(Member); - } - for (CXXRecordDecl::base_class_iterator Base = - ClassDecl->bases_begin(), - E = ClassDecl->bases_end(); Base != E; ++Base) { - if (Base->isVirtual()) - continue; - if (Base->getType()->isDependentType()) - continue; - // Skip over virtual bases which have trivial destructors. - CXXRecordDecl *BaseClassDecl - = cast(Base->getType()->getAs()->getDecl()); - if (BaseClassDecl->hasTrivialDestructor()) - continue; - - uintptr_t Member = - reinterpret_cast(Base->getType().getTypePtr()) | DRCTNONVBASE; - AllToDestruct.push_back(Member); - } - - // non-static data members. - for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), - E = ClassDecl->field_end(); Field != E; ++Field) { - QualType FieldType = C.getBaseElementType((*Field)->getType()); - - if (const RecordType* RT = FieldType->getAs()) { - // Skip over virtual bases which have trivial destructors. - CXXRecordDecl *BaseClassDecl = cast(RT->getDecl()); - if (BaseClassDecl->hasTrivialDestructor()) - continue; - uintptr_t Member = reinterpret_cast(*Field); - AllToDestruct.push_back(Member); - } - } - - unsigned NumDestructions = AllToDestruct.size(); - if (NumDestructions > 0) { - NumBaseOrMemberDestructions = NumDestructions; - BaseOrMemberDestructions = new (C) uintptr_t [NumDestructions]; - // Insert in reverse order. - for (int Idx = NumDestructions-1, i=0 ; Idx >= 0; --Idx) - BaseOrMemberDestructions[i++] = AllToDestruct[Idx]; - } -} - void CXXConstructorDecl::Destroy(ASTContext& C) { C.Deallocate(BaseOrMemberInitializers); diff --git a/clang/lib/Sema/Sema.h b/clang/lib/Sema/Sema.h index 766d18bde1ba..746c28af6201 100644 --- a/clang/lib/Sema/Sema.h +++ b/clang/lib/Sema/Sema.h @@ -2191,6 +2191,12 @@ public: llvm::SmallVectorImpl& Bases, llvm::SmallVectorImpl&Members); + /// computeBaseOrMembersToDestroy - Compute information in current + /// destructor decl's AST of bases and non-static data members which will be + /// implicitly destroyed. We are storing the destruction in the order that + /// they should occur (which is the reverse of construction order). + void computeBaseOrMembersToDestroy(CXXDestructorDecl *Destructor); + void AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl); virtual void ActOnMemInitializers(DeclPtrTy ConstructorDecl, diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index e7a327805a09..abf95283cdcf 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -3758,7 +3758,7 @@ Sema::DeclPtrTy Sema::ActOnFinishFunctionBody(DeclPtrTy D, StmtArg BodyArg, DiagnoseReturnInConstructorExceptionHandler(cast(Body)); if (CXXDestructorDecl *Destructor = dyn_cast(dcl)) - Destructor->computeBaseOrMembersToDestroy(Context); + computeBaseOrMembersToDestroy(Destructor); return D; } diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index 075c94545604..e5a4c6f81a66 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -1284,6 +1284,81 @@ void Sema::ActOnMemInitializers(DeclPtrTy ConstructorDecl, } } +void +Sema::computeBaseOrMembersToDestroy(CXXDestructorDecl *Destructor) { + CXXRecordDecl *ClassDecl = cast(Destructor->getDeclContext()); + llvm::SmallVector AllToDestruct; + + for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), + E = ClassDecl->vbases_end(); VBase != E; ++VBase) { + if (VBase->getType()->isDependentType()) + continue; + // Skip over virtual bases which have trivial destructors. + CXXRecordDecl *BaseClassDecl + = cast(VBase->getType()->getAs()->getDecl()); + if (BaseClassDecl->hasTrivialDestructor()) + continue; + if (const CXXDestructorDecl *Dtor = BaseClassDecl->getDestructor(Context)) + MarkDeclarationReferenced(Destructor->getLocation(), + const_cast(Dtor)); + + uintptr_t Member = + reinterpret_cast(VBase->getType().getTypePtr()) + | CXXDestructorDecl::VBASE; + AllToDestruct.push_back(Member); + } + for (CXXRecordDecl::base_class_iterator Base = + ClassDecl->bases_begin(), + E = ClassDecl->bases_end(); Base != E; ++Base) { + if (Base->isVirtual()) + continue; + if (Base->getType()->isDependentType()) + continue; + // Skip over virtual bases which have trivial destructors. + CXXRecordDecl *BaseClassDecl + = cast(Base->getType()->getAs()->getDecl()); + if (BaseClassDecl->hasTrivialDestructor()) + continue; + if (const CXXDestructorDecl *Dtor = BaseClassDecl->getDestructor(Context)) + MarkDeclarationReferenced(Destructor->getLocation(), + const_cast(Dtor)); + uintptr_t Member = + reinterpret_cast(Base->getType().getTypePtr()) + | CXXDestructorDecl::DRCTNONVBASE; + AllToDestruct.push_back(Member); + } + + // non-static data members. + for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), + E = ClassDecl->field_end(); Field != E; ++Field) { + QualType FieldType = Context.getBaseElementType((*Field)->getType()); + + if (const RecordType* RT = FieldType->getAs()) { + // Skip over virtual bases which have trivial destructors. + CXXRecordDecl *FieldClassDecl = cast(RT->getDecl()); + if (FieldClassDecl->hasTrivialDestructor()) + continue; + if (const CXXDestructorDecl *Dtor = + FieldClassDecl->getDestructor(Context)) + MarkDeclarationReferenced(Destructor->getLocation(), + const_cast(Dtor)); + uintptr_t Member = reinterpret_cast(*Field); + AllToDestruct.push_back(Member); + } + } + + unsigned NumDestructions = AllToDestruct.size(); + if (NumDestructions > 0) { + Destructor->setNumBaseOrMemberDestructions(NumDestructions); + uintptr_t *BaseOrMemberDestructions = + new (Context) uintptr_t [NumDestructions]; + // Insert in reverse order. + for (int Idx = NumDestructions-1, i=0 ; Idx >= 0; --Idx) + BaseOrMemberDestructions[i++] = AllToDestruct[Idx]; + Destructor->setBaseOrMemberDestructions(BaseOrMemberDestructions); + } +} + void Sema::ActOnDefaultCtorInitializers(DeclPtrTy CDtorDecl) { if (!CDtorDecl) return; diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp index b88ee8975234..c62159d001dd 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -757,6 +757,10 @@ Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, &DeleteArg, 1, Record, /*AllowMissing=*/true, OperatorDelete)) return ExprError(); + if (!Record->hasTrivialDestructor()) + if (const CXXDestructorDecl *Dtor = Record->getDestructor(Context)) + MarkDeclarationReferenced(StartLoc, + const_cast(Dtor)); } if (!OperatorDelete) { diff --git a/clang/test/CodeGenCXX/constructor-template.cpp b/clang/test/CodeGenCXX/constructor-template.cpp index 5fc6c2dfabb1..84e77d43da30 100644 --- a/clang/test/CodeGenCXX/constructor-template.cpp +++ b/clang/test/CodeGenCXX/constructor-template.cpp @@ -22,12 +22,14 @@ struct B { template class List { public: List(){ } // List*>::List() remains undefined. + ~List() {} }; template class Node { int i; public: Node(){ } // Node*>::Node() remains undefined. + ~Node() {} }; @@ -40,10 +42,15 @@ public: int main() { B *n = new B(4); BinomialNode *node = new BinomialNode(1); + delete node; } +// CHECK-LP64: __ZN4ListIP12BinomialNodeIiEED1Ev: +// CHECK-LP64: __ZN4ListIP12BinomialNodeIiEED2Ev: // CHECK-LP64: __ZN4NodeIP12BinomialNodeIiEEC1Ev: // CHECK-LP64: __ZN4ListIP12BinomialNodeIiEEC1Ev: +// CHECK-LP32: __ZN4ListIP12BinomialNodeIiEED1Ev: +// CHECK-LP32: __ZN4ListIP12BinomialNodeIiEED2Ev: // CHECK-LP32: __ZN4NodeIP12BinomialNodeIiEEC1Ev: // CHECK-LP32: __ZN4ListIP12BinomialNodeIiEEC1Ev: