Index: include/clang/AST/ExprObjC.h =================================================================== --- include/clang/AST/ExprObjC.h +++ include/clang/AST/ExprObjC.h @@ -1180,6 +1180,13 @@ /// 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; Index: lib/AST/ExprObjC.cpp =================================================================== --- lib/AST/ExprObjC.cpp +++ lib/AST/ExprObjC.cpp @@ -292,6 +292,31 @@ 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: