[cfe-commits] r58695 - in /cfe/trunk: include/clang/AST/Expr.h include/clang/AST/ExprCXX.h include/clang/AST/StmtNodes.def lib/AST/Expr.cpp lib/AST/ExprCXX.cpp lib/AST/StmtPrinter.cpp lib/AST/StmtSerialization.cpp lib/Sema/SemaDeclCXX.cpp lib/Sema/SemaExprCXX.cpp

Douglas Gregor doug.gregor at gmail.com
Tue Nov 4 06:32:22 PST 2008


Author: dgregor
Date: Tue Nov  4 08:32:21 2008
New Revision: 58695

URL: http://llvm.org/viewvc/llvm-project?rev=58695&view=rev
Log:
Create a new expression class, CXXThisExpr, to handle the C++ 'this' primary expression. Remove CXXThis from PredefinedExpr

Modified:
    cfe/trunk/include/clang/AST/Expr.h
    cfe/trunk/include/clang/AST/ExprCXX.h
    cfe/trunk/include/clang/AST/StmtNodes.def
    cfe/trunk/lib/AST/Expr.cpp
    cfe/trunk/lib/AST/ExprCXX.cpp
    cfe/trunk/lib/AST/StmtPrinter.cpp
    cfe/trunk/lib/AST/StmtSerialization.cpp
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp
    cfe/trunk/lib/Sema/SemaExprCXX.cpp

Modified: cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=58695&r1=58694&r2=58695&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Tue Nov  4 08:32:21 2008
@@ -223,7 +223,6 @@
     Func,
     Function,
     PrettyFunction,
-    CXXThis,
     ObjCSuper // super
   };
   

Modified: cfe/trunk/include/clang/AST/ExprCXX.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ExprCXX.h?rev=58695&r1=58694&r2=58695&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/ExprCXX.h (original)
+++ cfe/trunk/include/clang/AST/ExprCXX.h Tue Nov  4 08:32:21 2008
@@ -154,6 +154,39 @@
   virtual child_iterator child_end();
 };
 
+/// CXXThisExpr - Represents the "this" expression in C++, which is a
+/// pointer to the object on which the current member function is
+/// executing (C++ [expr.prim]p3). Example:
+///
+/// @code
+/// class Foo {
+/// public:
+///   void bar();
+///   void test() { this->bar(); }
+/// };
+/// @endcode
+class CXXThisExpr : public Expr {
+  SourceLocation Loc;
+
+public:
+  CXXThisExpr(SourceLocation L, QualType Type) 
+    : Expr(CXXThisExprClass, Type), Loc(L) { }
+
+  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
+
+  static bool classof(const Stmt *T) { 
+    return T->getStmtClass() == CXXThisExprClass;
+  }
+  static bool classof(const CXXThisExpr *) { return true; }
+
+  // Iterators
+  virtual child_iterator child_begin();
+  virtual child_iterator child_end();
+  
+  virtual void EmitImpl(llvm::Serializer& S) const;
+  static CXXThisExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);  
+};
+
 ///  CXXThrowExpr - [C++ 15] C++ Throw Expression.  This handles
 ///  'throw' and 'throw' assignment-expression.  When
 ///  assignment-expression isn't present, Op will be null.

Modified: cfe/trunk/include/clang/AST/StmtNodes.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/StmtNodes.def?rev=58695&r1=58694&r2=58695&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/StmtNodes.def (original)
+++ cfe/trunk/include/clang/AST/StmtNodes.def Tue Nov  4 08:32:21 2008
@@ -97,10 +97,11 @@
 STMT(64, CXXConstCastExpr       , CXXNamedCastExpr)
 STMT(65, CXXFunctionalCastExpr  , Expr)
 STMT(66, CXXBoolLiteralExpr     , Expr)
-STMT(67, CXXThrowExpr           , Expr)
-STMT(68, CXXDefaultArgExpr      , Expr)
-STMT(69, CXXZeroInitValueExpr   , Expr)
-STMT(70, CXXConditionDeclExpr   , DeclRefExpr)
+STMT(67, CXXThisExpr            , Expr)
+STMT(68, CXXThrowExpr           , Expr)
+STMT(69, CXXDefaultArgExpr      , Expr)
+STMT(70, CXXZeroInitValueExpr   , Expr)
+STMT(71, CXXConditionDeclExpr   , DeclRefExpr)
 
 // Obj-C Expressions.
 STMT(80, ObjCStringLiteral    , Expr)

Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=58695&r1=58694&r2=58695&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Tue Nov  4 08:32:21 2008
@@ -417,9 +417,7 @@
   case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
     return LV_Valid;
   case PredefinedExprClass:
-    return (cast<PredefinedExpr>(this)->getIdentType()
-               == PredefinedExpr::CXXThis
-            ? LV_InvalidExpression : LV_Valid);
+    return LV_Valid;
   case VAArgExprClass:
     return LV_Valid;
   case CXXDefaultArgExprClass:
@@ -439,6 +437,8 @@
     if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType())
       return LV_Valid;
     break;
+  case CXXThisExprClass:
+    return LV_InvalidExpression;
   default:
     break;
   }

Modified: cfe/trunk/lib/AST/ExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprCXX.cpp?rev=58695&r1=58694&r2=58695&view=diff

==============================================================================
--- cfe/trunk/lib/AST/ExprCXX.cpp (original)
+++ cfe/trunk/lib/AST/ExprCXX.cpp Tue Nov  4 08:32:21 2008
@@ -33,6 +33,10 @@
   return child_iterator();
 }
 
+// CXXThisExpr
+Stmt::child_iterator CXXThisExpr::child_begin() { return child_iterator(); }
+Stmt::child_iterator CXXThisExpr::child_end() { return child_iterator(); }
+
 // CXXThrowExpr
 Stmt::child_iterator CXXThrowExpr::child_begin() { return &Op; }
 Stmt::child_iterator CXXThrowExpr::child_end() {

Modified: cfe/trunk/lib/AST/StmtPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/StmtPrinter.cpp?rev=58695&r1=58694&r2=58695&view=diff

==============================================================================
--- cfe/trunk/lib/AST/StmtPrinter.cpp (original)
+++ cfe/trunk/lib/AST/StmtPrinter.cpp Tue Nov  4 08:32:21 2008
@@ -839,6 +839,10 @@
   OS << (Node->getValue() ? "true" : "false");
 }
 
+void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
+  OS << "this";
+}
+
 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
   if (Node->getSubExpr() == 0)
     OS << "throw";

Modified: cfe/trunk/lib/AST/StmtSerialization.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/StmtSerialization.cpp?rev=58695&r1=58694&r2=58695&view=diff

==============================================================================
--- cfe/trunk/lib/AST/StmtSerialization.cpp (original)
+++ cfe/trunk/lib/AST/StmtSerialization.cpp Tue Nov  4 08:32:21 2008
@@ -212,7 +212,10 @@
 
     case CXXConstCastExprClass:
       return CXXConstCastExpr::CreateImpl(D, C, SC);
-      
+
+    case CXXThisExprClass:
+      return CXXThisExpr::CreateImpl(D, C);
+
     case CXXZeroInitValueExprClass:
       return CXXZeroInitValueExpr::CreateImpl(D, C);
   }
@@ -1329,6 +1332,17 @@
   }
 }
 
+void CXXThisExpr::EmitImpl(llvm::Serializer& S) const {
+  S.Emit(getType());
+  S.Emit(Loc);
+}
+
+CXXThisExpr* CXXThisExpr::CreateImpl(llvm::Deserializer& D, ASTContext&) {
+  QualType Ty = QualType::ReadVal(D);
+  SourceLocation Loc = SourceLocation::ReadVal(D);
+  return new CXXThisExpr(Loc, Ty);
+}
+
 void CXXZeroInitValueExpr::EmitImpl(Serializer& S) const {
   S.Emit(getType());
   S.Emit(TyBeginLoc);

Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=58695&r1=58694&r2=58695&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Tue Nov  4 08:32:21 2008
@@ -46,7 +46,7 @@
 
     bool VisitExpr(Expr *Node);
     bool VisitDeclRefExpr(DeclRefExpr *DRE);
-    bool VisitPredefinedExpr(PredefinedExpr *PE);
+    bool VisitCXXThisExpr(CXXThisExpr *ThisE);
   };
 
   /// VisitExpr - Visit all of the children of this expression.
@@ -88,18 +88,14 @@
     return false;
   }
 
-  /// VisitPredefinedExpr - Visit a predefined expression, which could
-  /// refer to "this".
-  bool CheckDefaultArgumentVisitor::VisitPredefinedExpr(PredefinedExpr *PE) {
-    if (PE->getIdentType() == PredefinedExpr::CXXThis) {
-      // C++ [dcl.fct.default]p8:
-      //   The keyword this shall not be used in a default argument of a
-      //   member function.
-      return S->Diag(PE->getSourceRange().getBegin(),
-                     diag::err_param_default_argument_references_this,
-                     PE->getSourceRange());
-    }
-    return false;
+  /// VisitCXXThisExpr - Visit a C++ "this" expression.
+  bool CheckDefaultArgumentVisitor::VisitCXXThisExpr(CXXThisExpr *ThisE) {
+    // C++ [dcl.fct.default]p8:
+    //   The keyword this shall not be used in a default argument of a
+    //   member function.
+    return S->Diag(ThisE->getSourceRange().getBegin(),
+                   diag::err_param_default_argument_references_this,
+                   ThisE->getSourceRange());
   }
 }
 

Modified: cfe/trunk/lib/Sema/SemaExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprCXX.cpp?rev=58695&r1=58694&r2=58695&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExprCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprCXX.cpp Tue Nov  4 08:32:21 2008
@@ -708,8 +708,7 @@
 
   if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext))
     if (MD->isInstance())
-      return new PredefinedExpr(ThisLoc, MD->getThisType(Context),
-                                PredefinedExpr::CXXThis);
+      return new CXXThisExpr(ThisLoc, MD->getThisType(Context));
 
   return Diag(ThisLoc, diag::err_invalid_this_use);
 }





More information about the cfe-commits mailing list