From: Douglas Gregor Date: Tue, 4 Nov 2008 14:32:21 +0000 (+0000) Subject: Create a new expression class, CXXThisExpr, to handle the C++ 'this' primary expressi... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=796da18402f286b897782a298ae3b20c459c102e;p=clang Create a new expression class, CXXThisExpr, to handle the C++ 'this' primary expression. Remove CXXThis from PredefinedExpr git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@58695 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index 49796ccbc0..bc6c454ca1 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -223,7 +223,6 @@ public: Func, Function, PrettyFunction, - CXXThis, ObjCSuper // super }; diff --git a/include/clang/AST/ExprCXX.h b/include/clang/AST/ExprCXX.h index 61d25e9a97..73ef6160a6 100644 --- a/include/clang/AST/ExprCXX.h +++ b/include/clang/AST/ExprCXX.h @@ -154,6 +154,39 @@ public: 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. diff --git a/include/clang/AST/StmtNodes.def b/include/clang/AST/StmtNodes.def index 33d02703e4..4382fb427b 100644 --- a/include/clang/AST/StmtNodes.def +++ b/include/clang/AST/StmtNodes.def @@ -97,10 +97,11 @@ STMT(63, CXXReinterpretCastExpr , CXXNamedCastExpr) 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) diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index 85129b483b..171f7dbde2 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -417,9 +417,7 @@ Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const { case ObjCPropertyRefExprClass: // FIXME: check if read-only property. return LV_Valid; case PredefinedExprClass: - return (cast(this)->getIdentType() - == PredefinedExpr::CXXThis - ? LV_InvalidExpression : LV_Valid); + return LV_Valid; case VAArgExprClass: return LV_Valid; case CXXDefaultArgExprClass: @@ -439,6 +437,8 @@ Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const { if (cast(this)->getTypeAsWritten()->isReferenceType()) return LV_Valid; break; + case CXXThisExprClass: + return LV_InvalidExpression; default: break; } diff --git a/lib/AST/ExprCXX.cpp b/lib/AST/ExprCXX.cpp index 82626d864a..0eead3ca55 100644 --- a/lib/AST/ExprCXX.cpp +++ b/lib/AST/ExprCXX.cpp @@ -33,6 +33,10 @@ Stmt::child_iterator CXXBoolLiteralExpr::child_end() { 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() { diff --git a/lib/AST/StmtPrinter.cpp b/lib/AST/StmtPrinter.cpp index 51f4f51807..34aefc2c4c 100644 --- a/lib/AST/StmtPrinter.cpp +++ b/lib/AST/StmtPrinter.cpp @@ -839,6 +839,10 @@ void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { OS << (Node->getValue() ? "true" : "false"); } +void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) { + OS << "this"; +} + void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) { if (Node->getSubExpr() == 0) OS << "throw"; diff --git a/lib/AST/StmtSerialization.cpp b/lib/AST/StmtSerialization.cpp index 5f87b68dba..aefaee38a4 100644 --- a/lib/AST/StmtSerialization.cpp +++ b/lib/AST/StmtSerialization.cpp @@ -212,7 +212,10 @@ Stmt* Stmt::Create(Deserializer& D, ASTContext& C) { 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 @@ CXXNamedCastExpr::CreateImpl(Deserializer& D, ASTContext& C, StmtClass SC) { } } +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); diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index 33fc52b71d..e4321f53ff 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -46,7 +46,7 @@ namespace { 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 @@ namespace { 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()); } } diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp index fde28529b7..d012473c25 100644 --- a/lib/Sema/SemaExprCXX.cpp +++ b/lib/Sema/SemaExprCXX.cpp @@ -708,8 +708,7 @@ Action::ExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) { if (CXXMethodDecl *MD = dyn_cast(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); }