[cfe-commits] r102840 - in /cfe/trunk/lib/Sema: Sema.h SemaDeclCXX.cpp SemaExpr.cpp

Douglas Gregor dgregor at apple.com
Sat May 1 08:04:51 PDT 2010


Author: dgregor
Date: Sat May  1 10:04:51 2010
New Revision: 102840

URL: http://llvm.org/viewvc/llvm-project?rev=102840&view=rev
Log:
Added an RAII object that helps set up/tear down the Sema context
information required to implicitly define a C++ special member
function. Use it rather than explicitly setting CurContext on entry
and exit, which is fragile. 

Use this RAII object for the implicitly-defined default constructor,
copy constructor, copy assignment operator, and destructor.

Modified:
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=102840&r1=102839&r2=102840&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Sat May  1 10:04:51 2010
@@ -2182,10 +2182,9 @@
                                      CXXConstructorDecl *Constructor,
                                      unsigned TypeQuals);
 
-  /// DefineImplicitOverloadedAssign - Checks for feasibility of
-  /// defining implicit this overloaded assignment operator.
-  void DefineImplicitOverloadedAssign(SourceLocation CurrentLocation,
-                                      CXXMethodDecl *MethodDecl);
+  /// \brief Defined and implicitly-declared copy assignment operator.
+  void DefineImplicitCopyAssignment(SourceLocation CurrentLocation,
+                                    CXXMethodDecl *MethodDecl);
 
   /// getAssignOperatorMethod - Returns the default copy assignmment operator
   /// for the class.

Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=102840&r1=102839&r2=102840&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Sat May  1 10:04:51 2010
@@ -4033,6 +4033,30 @@
   return DeclPtrTy::make(AliasDecl);
 }
 
+namespace {
+  /// \brief Scoped object used to handle the state changes required in Sema
+  /// to implicitly define the body of a C++ member function;
+  class ImplicitlyDefinedFunctionScope {
+    Sema &S;
+    DeclContext *PreviousContext;
+    
+  public:
+    ImplicitlyDefinedFunctionScope(Sema &S, CXXMethodDecl *Method)
+      : S(S), PreviousContext(S.CurContext) 
+    {
+      S.CurContext = Method;
+      S.PushFunctionScope();
+      S.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
+    }
+    
+    ~ImplicitlyDefinedFunctionScope() {
+      S.PopExpressionEvaluationContext();
+      S.PopFunctionOrBlockScope();
+      S.CurContext = PreviousContext;
+    }
+  };
+}
+
 void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation,
                                             CXXConstructorDecl *Constructor) {
   assert((Constructor->isImplicit() && Constructor->isDefaultConstructor() &&
@@ -4042,8 +4066,7 @@
   CXXRecordDecl *ClassDecl = Constructor->getParent();
   assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor");
 
-  DeclContext *PreviousContext = CurContext;
-  CurContext = Constructor;
+  ImplicitlyDefinedFunctionScope Scope(*this, Constructor);
   if (SetBaseOrMemberInitializers(Constructor, 0, 0, /*AnyErrors=*/false)) {
     Diag(CurrentLocation, diag::note_member_synthesized_at) 
       << CXXConstructor << Context.getTagDeclType(ClassDecl);
@@ -4051,7 +4074,6 @@
   } else {
     Constructor->setUsed();
   }
-  CurContext = PreviousContext;
 }
 
 void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation,
@@ -4061,8 +4083,7 @@
   CXXRecordDecl *ClassDecl = Destructor->getParent();
   assert(ClassDecl && "DefineImplicitDestructor - invalid destructor");
 
-  DeclContext *PreviousContext = CurContext;
-  CurContext = Destructor;
+  ImplicitlyDefinedFunctionScope Scope(*this, Destructor);
 
   MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(),
                                          Destructor->getParent());
@@ -4074,27 +4095,23 @@
       << CXXDestructor << Context.getTagDeclType(ClassDecl);
 
     Destructor->setInvalidDecl();
-    CurContext = PreviousContext;
-
     return;
   }
-  CurContext = PreviousContext;
 
   Destructor->setUsed();
 }
 
-void Sema::DefineImplicitOverloadedAssign(SourceLocation CurrentLocation,
-                                          CXXMethodDecl *MethodDecl) {
+void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation,
+                                        CXXMethodDecl *MethodDecl) {
   assert((MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() &&
           MethodDecl->getOverloadedOperator() == OO_Equal &&
           !MethodDecl->isUsed()) &&
-         "DefineImplicitOverloadedAssign - call it for implicit assignment op");
+         "DefineImplicitCopyAssignment called for wrong function");
 
   CXXRecordDecl *ClassDecl
     = cast<CXXRecordDecl>(MethodDecl->getDeclContext());
 
-  DeclContext *PreviousContext = CurContext;
-  CurContext = MethodDecl;
+  ImplicitlyDefinedFunctionScope Scope(*this, MethodDecl);
 
   // C++[class.copy] p12
   // Before the implicitly-declared copy assignment operator for a class is
@@ -4151,8 +4168,6 @@
   }
   if (!err)
     MethodDecl->setUsed();
-
-  CurContext = PreviousContext;
 }
 
 CXXMethodDecl *
@@ -4195,8 +4210,7 @@
   CXXRecordDecl *ClassDecl = CopyConstructor->getParent();
   assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor");
 
-  DeclContext *PreviousContext = CurContext;
-  CurContext = CopyConstructor;
+  ImplicitlyDefinedFunctionScope Scope(*this, CopyConstructor);
 
   // C++ [class.copy] p209
   // Before the implicitly-declared copy constructor for a class is
@@ -4238,8 +4252,6 @@
     }
   }
   CopyConstructor->setUsed();
-
-  CurContext = PreviousContext;
 }
 
 Sema::OwningExprResult

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=102840&r1=102839&r2=102840&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Sat May  1 10:04:51 2010
@@ -7507,7 +7507,7 @@
     if (MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() &&
         MethodDecl->getOverloadedOperator() == OO_Equal) {
       if (!MethodDecl->isUsed())
-        DefineImplicitOverloadedAssign(Loc, MethodDecl);
+        DefineImplicitCopyAssignment(Loc, MethodDecl);
     }
   }
   if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {





More information about the cfe-commits mailing list