[cfe-commits] r88857 - in /cfe/trunk/lib/Sema: Sema.h SemaExprCXX.cpp
Anders Carlsson
andersca at mac.com
Sun Nov 15 10:45:20 PST 2009
Author: andersca
Date: Sun Nov 15 12:45:20 2009
New Revision: 88857
URL: http://llvm.org/viewvc/llvm-project?rev=88857&view=rev
Log:
Factor finding a deallocation function for a record type out into a separate function.
Modified:
cfe/trunk/lib/Sema/Sema.h
cfe/trunk/lib/Sema/SemaExprCXX.cpp
Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=88857&r1=88856&r2=88857&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Sun Nov 15 12:45:20 2009
@@ -2112,6 +2112,9 @@
void DeclareGlobalAllocationFunction(DeclarationName Name, QualType Return,
QualType Argument);
+ bool FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD,
+ DeclarationName Name, FunctionDecl* &Operator);
+
/// ActOnCXXDelete - Parsed a C++ 'delete' expression
virtual OwningExprResult ActOnCXXDelete(SourceLocation StartLoc,
bool UseGlobal, bool ArrayForm,
Modified: cfe/trunk/lib/Sema/SemaExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprCXX.cpp?rev=88857&r1=88856&r2=88857&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprCXX.cpp Sun Nov 15 12:45:20 2009
@@ -753,6 +753,59 @@
((DeclContext *)TUScope->getEntity())->addDecl(Alloc);
}
+bool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD,
+ DeclarationName Name,
+ FunctionDecl* &Operator) {
+ LookupResult Found;
+ // Try to find operator delete/operator delete[] in class scope.
+ LookupQualifiedName(Found, RD, Name, LookupOrdinaryName);
+
+ if (Found.isAmbiguous()) {
+ DiagnoseAmbiguousLookup(Found, Name, StartLoc);
+ return true;
+ }
+
+ for (LookupResult::iterator F = Found.begin(), FEnd = Found.end();
+ F != FEnd; ++F) {
+ if (CXXMethodDecl *Delete = dyn_cast<CXXMethodDecl>(*F))
+ if (Delete->isUsualDeallocationFunction()) {
+ Operator = Delete;
+ return false;
+ }
+ }
+
+ // We did find operator delete/operator delete[] declarations, but
+ // none of them were suitable.
+ if (!Found.empty()) {
+ Diag(StartLoc, diag::err_no_suitable_delete_member_function_found)
+ << Name << RD;
+
+ for (LookupResult::iterator F = Found.begin(), FEnd = Found.end();
+ F != FEnd; ++F) {
+ Diag((*F)->getLocation(),
+ diag::note_delete_member_function_declared_here)
+ << Name;
+ }
+
+ return true;
+ }
+
+ // Look for a global declaration.
+ DeclareGlobalNewDelete();
+ DeclContext *TUDecl = Context.getTranslationUnitDecl();
+
+ CXXNullPtrLiteralExpr Null(Context.VoidPtrTy, SourceLocation());
+ Expr* DeallocArgs[1];
+ DeallocArgs[0] = &Null;
+ if (FindAllocationOverload(StartLoc, SourceRange(), Name,
+ DeallocArgs, 1, TUDecl, /*AllowMissing=*/false,
+ Operator))
+ return true;
+
+ assert(Operator && "Did not find a deallocation function!");
+ return false;
+}
+
/// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in:
/// @code ::delete ptr; @endcode
/// or
@@ -844,52 +897,21 @@
DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName(
ArrayForm ? OO_Array_Delete : OO_Delete);
- LookupResult Found;
- if (Pointee->isRecordType() && !UseGlobal) {
- CXXRecordDecl *Record
- = cast<CXXRecordDecl>(Pointee->getAs<RecordType>()->getDecl());
-
- // Try to find operator delete/operator delete[] in class scope.
- LookupQualifiedName(Found, Record, DeleteName, LookupOrdinaryName);
-
- if (Found.isAmbiguous()) {
- DiagnoseAmbiguousLookup(Found, DeleteName, StartLoc);
- return ExprError();
- }
-
- // FIXME: Diagnose ambiguity properly
- for (LookupResult::iterator F = Found.begin(), FEnd = Found.end();
- F != FEnd; ++F) {
- if (CXXMethodDecl *Delete = dyn_cast<CXXMethodDecl>(*F))
- if (Delete->isUsualDeallocationFunction()) {
- OperatorDelete = Delete;
- break;
- }
- }
-
- if (!OperatorDelete && !Found.empty()) {
- // We did find operator delete/operator delete[] declarations, but
- // none of them were suitable.
- Diag(StartLoc, diag::err_no_suitable_delete_member_function_found)
- << DeleteName << Record;
-
- for (LookupResult::iterator F = Found.begin(), FEnd = Found.end();
- F != FEnd; ++F) {
- Diag((*F)->getLocation(),
- diag::note_delete_member_function_declared_here)
- << DeleteName;
- }
+ if (const RecordType *RT = Pointee->getAs<RecordType>()) {
+ CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
+
+ if (!UseGlobal &&
+ FindDeallocationFunction(StartLoc, RD, DeleteName, OperatorDelete))
return ExprError();
- }
- if (!Record->hasTrivialDestructor())
- if (const CXXDestructorDecl *Dtor = Record->getDestructor(Context))
+ if (!RD->hasTrivialDestructor())
+ if (const CXXDestructorDecl *Dtor = RD->getDestructor(Context))
MarkDeclarationReferenced(StartLoc,
const_cast<CXXDestructorDecl*>(Dtor));
}
-
+
if (!OperatorDelete) {
- // Didn't find a member overload. Look for a global one.
+ // Look for a global declaration.
DeclareGlobalNewDelete();
DeclContext *TUDecl = Context.getTranslationUnitDecl();
if (FindAllocationOverload(StartLoc, SourceRange(), DeleteName,
More information about the cfe-commits
mailing list