From: George Karpenkov Date: Fri, 25 Jan 2019 01:23:37 +0000 (+0000) Subject: [AST] Add a method to get a call type from an ObjCMessageExpr X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6b6d467fc080468077e4ae623138118c9d365cc1;p=clang [AST] Add a method to get a call type from an ObjCMessageExpr Due to references, expression type does not always correspond to an expected method return type (e.g. for a method returning int & the expression type of the call would still be int). We have a helper method for getting the expected type on CallExpr, but not on ObjCMessageExpr. Differential Revision: https://reviews.llvm.org/D57204 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@352147 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/ExprObjC.h b/include/clang/AST/ExprObjC.h index debe5acebf..6719169f0e 100644 --- a/include/clang/AST/ExprObjC.h +++ b/include/clang/AST/ExprObjC.h @@ -1180,6 +1180,13 @@ public: /// sent to. ReceiverKind getReceiverKind() const { return (ReceiverKind)Kind; } + /// \return the return type of the message being sent. + /// This is not always the type of the message expression itself because + /// of references (the expression would not have a reference type). + /// It is also not always the declared return type of the method because + /// of `instancetype` (in that case it's an expression type). + QualType getCallReturnType(ASTContext &Ctx) const; + /// Source range of the receiver. SourceRange getReceiverRange() const; diff --git a/lib/AST/ExprObjC.cpp b/lib/AST/ExprObjC.cpp index c31b5de2a5..584aa8c1fb 100644 --- a/lib/AST/ExprObjC.cpp +++ b/lib/AST/ExprObjC.cpp @@ -292,6 +292,31 @@ void ObjCMessageExpr::getSelectorLocs( SelLocs.push_back(getSelectorLoc(i)); } + +QualType ObjCMessageExpr::getCallReturnType(ASTContext &Ctx) const { + if (const ObjCMethodDecl *MD = getMethodDecl()) { + QualType QT = MD->getReturnType(); + if (QT == Ctx.getObjCInstanceType()) { + // instancetype corresponds to expression types. + return getType(); + } + return QT; + } + + // Expression type might be different from an expected call return type, + // as expression type would never be a reference even if call returns a + // reference. Reconstruct the original expression type. + QualType QT = getType(); + switch (getValueKind()) { + case VK_LValue: + return Ctx.getLValueReferenceType(QT); + case VK_XValue: + return Ctx.getRValueReferenceType(QT); + case VK_RValue: + return QT; + } +} + SourceRange ObjCMessageExpr::getReceiverRange() const { switch (getReceiverKind()) { case Instance: