From cd9b46e5442a3ef17f83f75177d8545cb5b3e2b9 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Tue, 4 Nov 2008 14:56:14 +0000 Subject: [PATCH] Add a new expression class, ObjCSuperExpr, to handle the Objective-C 'super'. Remove ObjCThis from PredefinedExpr git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@58698 91177308-0d34-0410-b5e6-96231b3b80d8 --- Driver/RewriteObjC.cpp | 5 ++--- include/clang/AST/Expr.h | 3 +-- include/clang/AST/ExprObjC.h | 24 ++++++++++++++++++++++++ include/clang/AST/StmtNodes.def | 1 + lib/AST/Expr.cpp | 4 ++++ lib/AST/StmtDumper.cpp | 15 +++++++++++++-- lib/AST/StmtPrinter.cpp | 7 ++++--- lib/AST/StmtSerialization.cpp | 14 ++++++++++++++ lib/Analysis/CheckObjCDealloc.cpp | 4 +--- lib/CodeGen/CGExpr.cpp | 11 ++++++++--- lib/CodeGen/CGObjC.cpp | 4 +--- lib/CodeGen/CodeGenFunction.h | 1 + lib/Sema/SemaExpr.cpp | 2 +- lib/Sema/SemaExprObjC.cpp | 3 +-- 14 files changed, 76 insertions(+), 22 deletions(-) diff --git a/Driver/RewriteObjC.cpp b/Driver/RewriteObjC.cpp index d2aa398e04..35d7cc8e1f 100644 --- a/Driver/RewriteObjC.cpp +++ b/Driver/RewriteObjC.cpp @@ -1899,9 +1899,8 @@ ObjCInterfaceDecl *RewriteObjC::isSuperReceiver(Expr *recExpr) { // check if we are sending a message to 'super' if (!CurMethodDef || !CurMethodDef->isInstance()) return 0; - if (PredefinedExpr *PDE = dyn_cast(recExpr)) - if (PDE->getIdentType() == PredefinedExpr::ObjCSuper) { - const PointerType *PT = PDE->getType()->getAsPointerType(); + if (ObjCSuperExpr *Super = dyn_cast(recExpr)) { + const PointerType *PT = Super->getType()->getAsPointerType(); assert(PT); ObjCInterfaceType *IT = cast(PT->getPointeeType()); return IT->getDecl(); diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index bc6c454ca1..2c25fcd123 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -222,8 +222,7 @@ public: enum IdentType { Func, Function, - PrettyFunction, - ObjCSuper // super + PrettyFunction }; private: diff --git a/include/clang/AST/ExprObjC.h b/include/clang/AST/ExprObjC.h index 3b19fe9527..7f519a861c 100644 --- a/include/clang/AST/ExprObjC.h +++ b/include/clang/AST/ExprObjC.h @@ -416,6 +416,30 @@ public: static ObjCMessageExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); }; +/// ObjCSuperExpr - Represents the "super" expression in Objective-C, +/// which refers to the object on which the current method is executing. +class ObjCSuperExpr : public Expr { + SourceLocation Loc; + +public: + ObjCSuperExpr(SourceLocation L, QualType Type) + : Expr(ObjCSuperExprClass, Type), Loc(L) { } + + virtual SourceRange getSourceRange() const { return SourceRange(Loc); } + + static bool classof(const Stmt *T) { + return T->getStmtClass() == ObjCSuperExprClass; + } + static bool classof(const ObjCSuperExpr *) { return true; } + + // Iterators + virtual child_iterator child_begin(); + virtual child_iterator child_end(); + + virtual void EmitImpl(llvm::Serializer& S) const; + static ObjCSuperExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); +}; + } // end namespace clang #endif diff --git a/include/clang/AST/StmtNodes.def b/include/clang/AST/StmtNodes.def index 4382fb427b..7b60fcf675 100644 --- a/include/clang/AST/StmtNodes.def +++ b/include/clang/AST/StmtNodes.def @@ -111,6 +111,7 @@ STMT(83, ObjCSelectorExpr , Expr) STMT(84, ObjCProtocolExpr , Expr) STMT(85, ObjCIvarRefExpr , Expr) STMT(86, ObjCPropertyRefExpr , Expr) +STMT(87, ObjCSuperExpr , Expr) // Clang Extensions. STMT(90, OverloadExpr , Expr) diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index 171f7dbde2..ce0856033d 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -1311,6 +1311,10 @@ Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; } Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; } Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; } +// ObjCSuperExpr +Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); } +Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); } + // PredefinedExpr Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); } Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); } diff --git a/lib/AST/StmtDumper.cpp b/lib/AST/StmtDumper.cpp index 349b6fb6e3..ecfdbf78d2 100644 --- a/lib/AST/StmtDumper.cpp +++ b/lib/AST/StmtDumper.cpp @@ -130,8 +130,9 @@ namespace { // C++ void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node); void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node); + void VisitCXXThisExpr(CXXThisExpr *Node); void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node); - + // ObjC void VisitObjCEncodeExpr(ObjCEncodeExpr *Node); void VisitObjCMessageExpr(ObjCMessageExpr* Node); @@ -139,6 +140,7 @@ namespace { void VisitObjCProtocolExpr(ObjCProtocolExpr *Node); void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node); void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node); + void VisitObjCSuperExpr(ObjCSuperExpr *Node); }; } @@ -312,7 +314,6 @@ void StmtDumper::VisitPredefinedExpr(PredefinedExpr *Node) { case PredefinedExpr::Func: fprintf(F, " __func__"); break; case PredefinedExpr::Function: fprintf(F, " __FUNCTION__"); break; case PredefinedExpr::PrettyFunction: fprintf(F, " __PRETTY_FUNCTION__");break; - case PredefinedExpr::ObjCSuper: fprintf(F, "super"); break; } } @@ -418,6 +419,11 @@ void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { fprintf(F, " %s", Node->getValue() ? "true" : "false"); } +void StmtDumper::VisitCXXThisExpr(CXXThisExpr *Node) { + DumpExpr(Node); + fprintf(F, " this"); +} + void StmtDumper::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) { DumpExpr(Node); fprintf(F, " functional cast to %s", @@ -471,6 +477,11 @@ void StmtDumper::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) { } } +void StmtDumper::VisitObjCSuperExpr(ObjCSuperExpr *Node) { + DumpExpr(Node); + fprintf(F, " super"); +} + //===----------------------------------------------------------------------===// // Stmt method implementations //===----------------------------------------------------------------------===// diff --git a/lib/AST/StmtPrinter.cpp b/lib/AST/StmtPrinter.cpp index 34aefc2c4c..420bdbdee9 100644 --- a/lib/AST/StmtPrinter.cpp +++ b/lib/AST/StmtPrinter.cpp @@ -515,9 +515,6 @@ void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) { case PredefinedExpr::PrettyFunction: OS << "__PRETTY_FUNCTION__"; break; - case PredefinedExpr::ObjCSuper: - OS << "super"; - break; } } @@ -917,6 +914,10 @@ void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { OS << "]"; } +void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) { + OS << "super"; +} + void StmtPrinter::VisitBlockExpr(BlockExpr *Node) { BlockDecl *BD = Node->getBlockDecl(); OS << "^"; diff --git a/lib/AST/StmtSerialization.cpp b/lib/AST/StmtSerialization.cpp index aefaee38a4..09aaceffda 100644 --- a/lib/AST/StmtSerialization.cpp +++ b/lib/AST/StmtSerialization.cpp @@ -191,6 +191,9 @@ Stmt* Stmt::Create(Deserializer& D, ASTContext& C) { case ObjCStringLiteralClass: return ObjCStringLiteral::CreateImpl(D, C); + case ObjCSuperExprClass: + return ObjCSuperExpr::CreateImpl(D, C); + //==--------------------------------------==// // C++ //==--------------------------------------==// @@ -1232,6 +1235,17 @@ ObjCStringLiteral* ObjCStringLiteral::CreateImpl(Deserializer& D, ASTContext& C) return new ObjCStringLiteral(String,T,L); } +void ObjCSuperExpr::EmitImpl(llvm::Serializer& S) const { + S.Emit(getType()); + S.Emit(Loc); +} + +ObjCSuperExpr* ObjCSuperExpr::CreateImpl(llvm::Deserializer& D, ASTContext&) { + QualType Ty = QualType::ReadVal(D); + SourceLocation Loc = SourceLocation::ReadVal(D); + return new ObjCSuperExpr(Loc, Ty); +} + //===----------------------------------------------------------------------===// // Serialization for Clang Extensions. //===----------------------------------------------------------------------===// diff --git a/lib/Analysis/CheckObjCDealloc.cpp b/lib/Analysis/CheckObjCDealloc.cpp index e41c827184..8628ff122c 100644 --- a/lib/Analysis/CheckObjCDealloc.cpp +++ b/lib/Analysis/CheckObjCDealloc.cpp @@ -30,9 +30,7 @@ static bool scan_dealloc(Stmt* S, Selector Dealloc) { if (ME->getSelector() == Dealloc) if(ME->getReceiver()) if (Expr* Receiver = ME->getReceiver()->IgnoreParenCasts()) - if (PredefinedExpr* E = dyn_cast(Receiver)) - if (E->getIdentType() == PredefinedExpr::ObjCSuper) - return true; + return isa(Receiver); // Recurse to children. diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index fa70f47da9..3d8d192a2d 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -130,7 +130,9 @@ LValue CodeGenFunction::EmitLValue(const Expr *E) { return EmitObjCIvarRefLValue(cast(E)); case Expr::ObjCPropertyRefExprClass: return EmitObjCPropertyRefLValue(cast(E)); - + case Expr::ObjCSuperExprClass: + return EmitObjCSuperExpr(cast(E)); + case Expr::UnaryOperatorClass: return EmitUnaryOpLValue(cast(E)); case Expr::ArraySubscriptExprClass: @@ -571,8 +573,6 @@ LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) { case PredefinedExpr::Function: case PredefinedExpr::PrettyFunction: return EmitPredefinedFunctionName(E->getIdentType()); - case PredefinedExpr::ObjCSuper: - return EmitUnsupportedLValue(E, "use of super"); } } @@ -875,6 +875,11 @@ CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) { return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers()); } +LValue +CodeGenFunction::EmitObjCSuperExpr(const ObjCSuperExpr *E) { + return EmitUnsupportedLValue(E, "use of super"); +} + RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, QualType FnType, CallExpr::const_arg_iterator ArgBeg, CallExpr::const_arg_iterator ArgEnd) { diff --git a/lib/CodeGen/CGObjC.cpp b/lib/CodeGen/CGObjC.cpp index c0260ad06b..cb820409d6 100644 --- a/lib/CodeGen/CGObjC.cpp +++ b/lib/CodeGen/CGObjC.cpp @@ -72,9 +72,7 @@ RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) { } isClassMessage = true; - } else if (isa(E->getReceiver())) { - assert(cast(E->getReceiver())->getIdentType() == - PredefinedExpr::ObjCSuper); + } else if (isa(E->getReceiver())) { isSuperMessage = true; Receiver = LoadObjCSelf(); } else { diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h index ccc5008aee..e6636ba27b 100644 --- a/lib/CodeGen/CodeGenFunction.h +++ b/lib/CodeGen/CodeGenFunction.h @@ -402,6 +402,7 @@ public: LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E); LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E); LValue EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E); + LValue EmitObjCSuperExpr(const ObjCSuperExpr *E); //===--------------------------------------------------------------------===// // Scalar Expression Emission diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index f06a04101b..c0abc9e51a 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -365,7 +365,7 @@ Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc, if (SD == 0 && &II == SuperID) { QualType T = Context.getPointerType(Context.getObjCInterfaceType( getCurMethodDecl()->getClassInterface())); - return new PredefinedExpr(Loc, T, PredefinedExpr::ObjCSuper); + return new ObjCSuperExpr(Loc, T); } } if (D == 0) { diff --git a/lib/Sema/SemaExprObjC.cpp b/lib/Sema/SemaExprObjC.cpp index a474b7eee7..bc2f7f2d6f 100644 --- a/lib/Sema/SemaExprObjC.cpp +++ b/lib/Sema/SemaExprObjC.cpp @@ -193,8 +193,7 @@ Sema::ExprResult Sema::ActOnClassMessage( if (getCurMethodDecl()->isInstance()) { QualType superTy = Context.getObjCInterfaceType(ClassDecl); superTy = Context.getPointerType(superTy); - ExprResult ReceiverExpr = new PredefinedExpr(SourceLocation(), superTy, - PredefinedExpr::ObjCSuper); + ExprResult ReceiverExpr = new ObjCSuperExpr(SourceLocation(), superTy); // We are really in an instance method, redirect. return ActOnInstanceMessage(ReceiverExpr.Val, Sel, lbrac, rbrac, Args, NumArgs); -- 2.40.0